Kian Tamar
Kian Tamar

Reputation: 96

How to properly display a multiline text in android XML

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>

Upvotes: 1

Views: 1512

Answers (2)

azertiti
azertiti

Reputation: 3150

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>

Upvotes: 1

Sagar Shah
Sagar Shah

Reputation: 4292

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>

Upvotes: 1

Related Questions