0

Suppose if I am setting a horizontal orientation linear layout then what should be the total pixel of match parent so that I could adjust my buttons and text view according to that size.

I tried to place all margins and calculate...

the expected output would depend on screen size.

Nithinlal
  • 4,845
  • 1
  • 29
  • 40

3 Answers3

1

total pixels for match_parent of a view is depend on your parentView type, dimensions and composition of views within the layout, for example if your parentView is LinearLayout of size 100px * 100px then your match_parent for a view within that LinearLayout is 100px * 100px (iff you have only one view within that LinearLayout otherwise it depends on your composition) you can get your view's height and width (in pixels) programmatically to by using below code to any view or Layout

Java

view.post(new Runnable(){
    @Override
    public void run() {
         int height = view.getMeasuredHeight();
         int width = view.getMeasuredWidth();
    }
});

kotlin

view.post {
    val width = view.measuredWidth
    val height = view.measuredHeight
}

after getting height and width of layout you can manage you margin or size according to your logic

0

The total pixels of match parent is the total pixels of the screen size, and if you to implement an auto resize for your views you should either use constraint layoutas a parent layout for your views, either use this great library that uses a new size unit called sdp https://github.com/intuit/sdp

Amine
  • 2,241
  • 2
  • 19
  • 41
0

Don't user pixels, it makes your screen not responsive to all screen sizes. use ConstraintLayout or Relative Layout if you want your screen to be responsive to all screen sizes.

Here is an example of a view that his width is qeual to all of the screen size using ConstraintLayout :

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>



<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53