I am going to make an application for different screens. As I read, you should avoid using certain numerical values ​​for height and width.

Problem: I have a TextView within which the amount of text changes.

In order not to use numerical values ​​for the height of this TextView , I set layout_height="wrap_content" , but the fact is that from the bottom of this TextView there are as many as 5 Button s, and their position depends on TextView :

If the TextView has a small height, then these Button s seek up, and a lot of space remains at the bottom of the screen (picture1). This means that the TextView should be fairly high, so that these Button s, roughly speaking, stuck to the end of the screen.

In the photo, the TextView (question) has a height of 210dp , but since setting the value is not desirable, an idea appeared to put all the Button-ы at the bottom of the screen, then put the TextView Вопрос over the entire space between the TextView Question Counter and Button '>'(next) .

But how can this be done? Is this solution appropriate?

Please tell me how to solve the problem. Below is the xml code and screenshots:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> <TextView android:layout_width="match_parent" android:gravity="center" android:layout_height="wrap_content" android:textSize="22dp" android:text="Quest Counter" android:id="@+id/questCounterId" android:textColor="#FFFFFF" /> <TextView android:text="Вопрос" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#fff" android:id="@+id/QuestionText" android:background="#000000" android:textSize="24dp" /> <Button android:layout_gravity="center" android:text=">" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/nextButton" android:onClick="ShowNextQuestion" android:background="@drawable/using_nextprevbuttons" android:shadowColor="#FFFFFF" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="4" android:layout_marginBottom="10dp" android:textSize="32dp"/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" > <Button android:onClick="OptionButtonPressed" android:id="@+id/opta" android:text="Ответ А" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@drawable/using_optionshape" android:textColor="#000000" android:shadowColor="#FFFFFF" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:layout_marginRight="10dp" android:layout_marginLeft="5dp"/> <Button android:id="@+id/optb" android:onClick="OptionButtonPressed" android:text="Ответ В" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@drawable/using_optionshape" android:textColor="#000000" android:shadowColor="#FFFFFF" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:layout_marginRight="5dp"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:text="Ответ С" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:background="@drawable/using_optionshape" android:textColor="#000000" android:shadowColor="#FFFFFF" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:layout_marginRight="10dp" android:layout_marginLeft="5dp" android:onClick="OptionButtonPressed" android:id="@+id/optc" /> <Button android:text="Ответ D" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:background="@drawable/using_optionshape" android:textColor="#000000" android:shadowColor="#FFFFFF" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:layout_marginRight="5dp" android:id="@+id/optd" android:onClick="OptionButtonPressed"/> </LinearLayout> </LinearLayout> 

Screen1

Screen2

  • Do you need the inscription Вопрос was in the middle, and the buttons were pressed to the bottom? - post_zeew
  • Yes, that's right. - abay

2 answers 2

Your problem can be solved quite simply, for the second TextView ( QuestionText ) you specify the weight ( layout_weight ). This will indicate that this TextView entire free area, the buttons that are below this TextView will be pressed to the bottom of the screen. The height must be 0dp , since it is calculated on the basis of weight:

 <TextView android:text="Вопрос" android:gravity="center" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textColor="#fff" android:id="@+id/QuestionText" android:background="#000000" android:textSize="24dp" /> 

You can also use RelativeLayout as the parent container. The Answer C and Answer D buttons are tied to the bottom (bottom) of the root container, the other buttons are tied to these two. TextView bind to the upper border (top) and the second TextView to it.
Considering the use of weight in LinearLayout and the nesting of containers in your current layout, the RealativeLayout will be more optimal and preferable, however, if there is a lot of text, then there will be text overlays on the buttons in such markup.

This problem can be solved most simply and efficiently through ConatraintLayout , but it is not recommended to use it in public projects while beta testing is taking place, as any changes can be made, which can lead to an application being inoperative.

    Consider ConstrainLayout . There it is possible to set the position of the view relative to another view .

    • one
      And why in this case need just ConstraintLayout ? In the same RelativeLayout you can position elements relative to each other. - post_zeew
    • Yes, I agree with you. But ConstraintLayout is much more convenient. Yes, and they are better in performance. I personally liked the make-up screen. But the taste and color ... - pavel163
    • ConstraintLayout still in beta, any changes are possible and it is too early to use it in projects to avoid all sorts of problems. And yes, quite promising. - pavlofff
    • We already have screens on ConstraintLayout in production. - pavel163