xopehir780
xopehir780

Reputation: 55

Android EditText use the entire activity height after title

I am developing an android app to support posting articles by the user and have an EditText for the same and below is my layout.

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

    <TextView
        android:id="@+id/article_title_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Write Article"/>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/article_text_field"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </com.google.android.material.textfield.TextInputLayout>
    
</LinearLayout>

The problem is the on clicking on EditText the article title becomes half-cut in the view and the cursor for the EditText is at the bottom which should be at the top.

Does anyone have suggestions on how to fix it?

Upvotes: 1

Views: 76

Answers (3)

G Ganesh
G Ganesh

Reputation: 99

Actually you should use android:inputType="textMultiLine" attribute inside com.google.android.material.textfield.TextInputEditText and also specify how many lines you want to give for the field.
Because the field is an article which means a multiple lined field. And also please specify orientation as vertical.
The code will look like this,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/article_title_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Write Article" />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/article_text_field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.textfield.TextInputEditText
                android:inputType="textMultiLine"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </com.google.android.material.textfield.TextInputLayout>
    </LinearLayout>

Upvotes: 0

Cristi Ionut
Cristi Ionut

Reputation: 63

If you want the TextView and TextInputLayout to be displayed horizontally (based on your LinearLayout attribute android:orientation="vertical") then you can achieve the desired layout with this:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
      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">

      <TextView
        android:id="@+id/article_title_text_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="Write Article"
        android:padding="12dp"/>

      <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/article_text_field"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/article_title_text_view"
        android:layout_alignParentEnd="true"
        app:hintEnabled="false">

        <com.google.android.material.textfield.TextInputEditText
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="top"
          android:gravity="top|start"
          android:padding="12dp"
          tools:text="@tools:sample/lorem/random"/>

      </com.google.android.material.textfield.TextInputLayout>

    </RelativeLayout>

If you want to have the TextView and the TextInputLayout vertically aligned, then go with:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout   
      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">

      <TextView
        android:id="@+id/article_title_text_view"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:padding="12dp"
        android:text="Write Article"
        android:textAlignment="center"/>

      <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/article_text_field"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/article_title_text_view"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        app:hintEnabled="false">

        <com.google.android.material.textfield.TextInputEditText
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="top"
          android:gravity="top|start"
          android:padding="12dp"
          tools:text="@tools:sample/lorem/random"/>

      </com.google.android.material.textfield.TextInputLayout>

    </RelativeLayout>

Upvotes: 0

iknow
iknow

Reputation: 9892

You have to set gravity for TextInputEditText and also delete android:gravity="vertical"

It should look like this:

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

    <TextView
        android:id="@+id/article_title_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Write Article"
        />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/article_text_field"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="top"
            />

    </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

Now, when You will have many lines Your layout will be scrolling while writing and the app bar with the title will start hiding. Which I think is good because the user will see more EditText so it will be easier to write a long text

Upvotes: 1

Related Questions