1

I have a dialog activity which includes a multiline text field and two buttons.
The problem is when the length of the sentence in the multiline text field exceeds the width of the space available and breaks to the next line or when I press enter to go to the next line:
it changes the position of the buttons under it!

This is how it should look like:

How I want my dialogue activity to look like

But after adding some new lines it looks like this:

enter image description here

See the position of the two buttons and the space created under them.
How can I fix this?

Here is the XML code I'm using:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/noteText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:gravity="right"
    android:hint="متن خود را تایپ کنید"
    android:inputType="textMultiLine"
    android:padding="5dp" >

    <requestFocus />
</EditText>

<LinearLayout
    style="?android:attr/buttonBarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="100" >

    <Button
        android:id="@+id/note_clear"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:text="حذف" />

    <Button
        android:id="@+id/note_save"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:text="ذخیره" />
</LinearLayout>

5
  • Try using like 40dp in place of wrap_content Commented Feb 6, 2014 at 16:29
  • Try using a relative layout instead of linear and remove the weights Commented Feb 6, 2014 at 16:30
  • @Rat-a-tat-a-tatRatatouille Could you set an example please. I tried relative layout. but the buttons' position was irrelevant to the text field. Commented Feb 6, 2014 at 17:13
  • 1
    did u get a solution ?? Commented Feb 7, 2014 at 10:31
  • Yes. I used some of the points made here and finally reached the effect I wanted. Commented Feb 9, 2014 at 12:35

2 Answers 2

1

Your EditText has the height defined to wrap_content. That means it will take the size of content, text in your case. The initial space allocated is only for one line so it will start allocating more once you get on second. You can allocate the whole screen for it if that's not a design issue.

<EditText
   android:id="@+id/noteText"
   android:layout_width="fill_parent"
   android:layout_height="match_parent"
   android:ems="10"
   android:gravity="right"
   android:hint="متن خود را تایپ کنید"
   android:inputType="textMultiLine"
   android:padding="5dp" >

   <requestFocus />
</EditText>

I changed only the line:

   android:layout_height="match_parent"

Later edit: In order to take all possible space and don't affect the button you do this:

android:layout_height="0dp"
android:layout_weight="1"

Final edit by the questioner: The correct answer is:

<EditText
    android:id="@+id/noteText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ems="10"
    android:gravity="right"
    android:hint="متن خود را تایپ کنید"
    android:inputType="textMultiLine"
    android:padding="5dp" >

    <requestFocus />
</EditText>
Sign up to request clarification or add additional context in comments.

4 Comments

This seems to work. But it pushed my buttons out of view. Any idea how to fix this?
I've edited my response and added a solution that should work. See the last part of my post.
@azeratiti I added only android:layout_weight="1" to the Editable Text element. And I used android:layout_height="wrap_content" so it finally worked. Please kindly update your answer so I can accept it.
@azertiti, approved the edit by OP, you might want to remove it or just replace your code with his though - since they're almost identical.
1

First of all set in your last Linearlayout:

android:orientation="horizontal" 

Put this in both Buttons:

android:layout_width="0dp"

And make a root layout of ScrollView & put all the above code in Scrollview. So your xml will look something like this:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/noteText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:gravity="right"
            android:hint="متن خود را تایپ کنید"
            android:inputType="textMultiLine"
            android:padding="5dp" >

            <requestFocus />
        </EditText>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="100"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/note_clear"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="50"
                android:text="حذف" />

            <Button
                android:id="@+id/note_save"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="50"
                android:text="ذخیره" />
        </LinearLayout>
    </LinearLayout>

</ScrollView>

2 Comments

I did what you suggested. It didn't make any difference. The problem remains.
Then you can go for Relative layout in root layout. And Give your linear layout : alignparentBottom=true;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.