2

I have a constraint layout which displays like an image view but it contains multiple views with the width set as wrap_content. The constraint layout is put inside horizontal scroll bar. My problem is,.. for the small dpi phones, the width of layout becomes big so I have to scroll to see the full layout. But I can fix it using this in the xml of constraint layout by adding

 android:scaleX="0.7"
 android:scaleY="0.7"

But the problem is, for the high dpi phones, the layout becomes very small and there are blank spaces appear around the layout. So i think I need to adjust the scaleX and scaleY to programmatically so that it will become a variable to adjust the layout scale for different screensizes. How to adjust this scaleX and Y in java code programmatically according to the screen dpi?

example,

for the 420dpi phones, the scales should be 0.8

for the 320dpi phones, the scales should be 0.6 or lower, etc...

but using these values with if statement will not accurate for all phones. I want an accurate variable float value of scale to fit the screen for every dpi displays. How to get this float value of scaleX and scaleY from the screen dpi. Please. (if possible, i don't want to use Horizontal scrollview. I want the scale of constraint layout to fit the width of screen only. And also I can't use different layouts in this case.)

enter image description here

This is my xml for this

<androidx.core.widget.NestedScrollView
            android:id="@+id/scrollview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="55dp"
            android:fillViewport="true"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0">

    <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/constraintlayoutprintcard"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="5dp"
                    android:layout_marginStart="5dp"
                    android:layout_marginTop="5dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <HorizontalScrollView
                        android:id="@+id/horizontalscrollview"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:background="#00FFFFFF"
                        android:fillViewport="true"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent">

                             <androidx.constraintlayout.widget.ConstraintLayout
                                  android:id="@+id/constraintlayoutprint"
                                  android:layout_width="500dp"
                                  android:layout_height="wrap_content"
                                  android:layout_gravity="center"
                                  android:background="@color/colorWhite1"
                                  android:scaleX="0.8"
                                  android:scaleY="0.8"
                                  android:visibility="visible"
                                  tools:ignore="ExtraText">
                             </androidx.constraintlayout.widget.ConstraintLayout>

                    </HorizontalScrollView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>
John Smith
  • 97
  • 1
  • 8

2 Answers2

0

Set width of the image to match_parent with margins that you want and height as you wish. This will fit the width of the image according to screen width

Lathesh
  • 340
  • 1
  • 7
  • 1
    I am not using Image. I want the adjust the scale of the constraint layout to fit the screen. Not even changing the width and height of the layout. Just want to adjust the scale X and scale Y of constraint layout to display within the screen size. The layout will have the same width and height but displays full layout within the screen with smaller scale. – John Smith Mar 30 '21 at 12:27
-1

Try creating the layout programmatically. There's an inner class in every layout that deals with setting the parameters.

Try this thread

If for some reason there are no scaleX and scaleY properties in the GridLayout and GridLayout.Params classes, you may have to go through each of the children in the layout and adjust the weight and gravity. It's always possible that the xml version is doing just that behind the scenes.

WTRIX
  • 95
  • 1
  • 8