0

I have a dialog, which has a layout xml. Controls are vertically aligned, and in one "row" there is a textview and a seekbar. Textview has weight=1, and seekbar has weight=2.

Now the problem is that if textview cannot fit, it is wrapped, but the height of the textview is not adjusted, and the second line is not visible.

Layout xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

  <LinearLayout 
    android:id="@+id/dialog_settings_layout_main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minWidth="240dp"
    android:padding="10dp"
    android:orientation="vertical" >

    ....other controls.....

    <LinearLayout 
    android:id="@+id/dialog_settings_layout_volume"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView_dialog_settings"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="aaaaaaaaa bbbbbbbbbb cccccccccc"
        android:layout_gravity="center_vertical"
        android:paddingRight="20dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <SeekBar
        android:id="@+id/seekbar_dialog_settings"
        android:layout_width="0px"
        android:layout_weight="2"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:max="8"
        android:progress="1" />

    </LinearLayout>

    ....other controls.....

double-beep
  • 5,031
  • 17
  • 33
  • 41
Zoli
  • 841
  • 8
  • 31
  • 2
    You have to show what is around LinearLayout because probably there is a problem. – Damian Petla Aug 21 '14 at 14:28
  • Remove this line and check `android:layout_weight="1"` its working fine if you use `LinearLayout` – Srihari Aug 21 '14 at 14:28
  • The parent LinearLayout height is not adjusted on the TextView height change. Quick fix: make the TextView single line (also reduce its textSize a bit). – Phantômaxx Aug 21 '14 at 14:30
  • em surprised, how this works android:layout_width="0px" ? :P – Mohsin Aug 21 '14 at 14:35
  • @Loop: I edited the layout XML to see what is around the LinerLayout – Zoli Aug 21 '14 at 14:52
  • @Frank N. Stein: I don't want to reduce the testSize, and if it's single line the text will not fit – Zoli Aug 21 '14 at 14:54
  • If you use layout_weight in horizontal layout then layout_width is not used. Setting to 0dp is an optimisation. – Damian Petla Aug 21 '14 at 14:55
  • Is vertical LinearLayout containing only other horizontal LinearLayouts? – Damian Petla Aug 21 '14 at 14:57
  • So, try to force the TextView lines to 2. – Phantômaxx Aug 21 '14 at 14:58
  • @Loop: yes, the outmost vertical LinearLayout contains several controls and Horizontal LinearLayouts, similar to the above one. – Zoli Aug 21 '14 at 15:00
  • @Frank N. Stein: I do not use hardcoded line numbers, as for different languages it could be 1-2-3 lines. – Zoli Aug 21 '14 at 15:01
  • 1
    You **should**. So the TextView won't change its height. `android:lines="3"` should do the trick. Just make the TextView gravity vertically centered. – Phantômaxx Aug 21 '14 at 15:02
  • That's not good, as if the text on X language occupies 1 line only, it looks very ugly, 3 lines of space if used for 1 line of text. – Zoli Aug 21 '14 at 15:04
  • Do you have layout_weight specified for children of vertical LinearLayout? I just created simple example with similar layout and setting parameters you showed and it works fine. So there must be something with layout parts that are not shown. – Damian Petla Aug 21 '14 at 15:08

1 Answers1

0

OK, I think I have solution.

Set match_parent for ScrollView width and height and horizontal LinearLayout width.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

  <LinearLayout 
    android:id="@+id/dialog_settings_layout_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minWidth="240dp"
    android:padding="10dp"
    android:orientation="vertical" >

I have tried to reproduce it and everything worked till I wrapped layout with ScrollView which I missed at the beginning. Then I had exactly the same problem, adjusting mentioned values solved it.

Update

To make it work inside Dialog, create one using AlertDialog.Builder

AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.dialog_layout, null);
builder.setView(view);
AlertDialog dialog = builder.create();

dialog.show();
Damian Petla
  • 8,963
  • 5
  • 44
  • 47
  • I changed the XML, and in Eclipse/Graphical Layout it displayed correctly. Hurray! But then I tried on my device, and it still not ok. Please note, this layout is used in a dialog, not in an activity. Code: `final Dialog dialogSettings = new Dialog(this);` `dialogSettings.requestWindowFeature(Window.FEATURE_NO_TITLE);` `dialogSettings.setContentView(R.layout.dialog_settings);` `...` `((TextView)dialogSettings.findViewById(R.id.textView_dialog_settings)).setText(....)` `dialogSettings.show();` Also! Maybe that could be the problem? The textview text is changed after setContentView. – Zoli Aug 21 '14 at 15:27
  • No, I removed the `setText()` after the `setContentView` to just use something from the XML, but still the same on the device/dialog. Probably then the Dialog ruins it? – Zoli Aug 21 '14 at 15:37
  • Creating dialogs the way you do is not recommended. Read this http://developer.android.com/guide/topics/ui/dialogs.html. I have used AlertDialog.Builder and it works now like a charm. Even more preferable is using DialogFragment. – Damian Petla Aug 21 '14 at 18:29
  • I replaced the Dialog with AlertDialog, and now it appears just as I wanted. Thanks – Zoli Aug 22 '14 at 10:50