Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextInputLayout as text area - Android

I'm trying to create something like this in my app(taken from google docs):

enter image description here

enter image description here

Now, I tried to create a TextInputLayout element and try to put border around it but I can't manage to get this to look like the images I posted.

Here is my code for the TextInputLayout:

<android.support.design.widget.TextInputLayout
        android:id="@+id/shipper_layout"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/text_layout_stroke_normal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp">

        <EditText
            android:id="@+id/shipper_field"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="15dp"
            android:background="@null"
            android:gravity="top"
            android:hint="@string/shipper_field"
            android:inputType="textMultiLine"
            android:overScrollMode="always"
            android:scrollbarStyle="insideInset"
            android:scrollbars="vertical" />
    </android.support.design.widget.TextInputLayout>

Here is how it looks: (Before focused)

enter image description here

enter image description here

You can see the border isn't change as expected and the hint is just minimized

like image 251
David Lasry Avatar asked Apr 15 '17 11:04

David Lasry


People also ask

What is the use of TextInputLayout in Android?

The primary use of a TextInputLayout is to act as a wrapper for EditText(or its descendant) and enable floating hint animations. Rule of Thumb : TextInputLayout should wrap TextInputEditText instead of the normal EditText.

How do you make a textbox on Android?

Click TextView in the Component Tree panel and then press the Delete key. In the Palette panel, click Text to show the available text controls. Drag the Plain Text into the design editor and drop it near the top of the layout. This is an EditText widget that accepts plain text input.

What is text fields in Android?

A text field allows the user to type text into your app. It can be either single line or multi-line. Touching a text field places the cursor and automatically displays the keyboard.

How do I remove TextInputLayout padding?

By default, some space is reserved for assistive labels below SfTextInputLayout control. These reserved spaces can be removed by setting ReserveSpaceForAssistiveLabels property as false when helper and error text are empty and ShowCharCount is false.


1 Answers

Try this code:

in Xml:

 <android.support.design.widget.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/shipper_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <EditText
        android:id="@+id/shipper_field"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@drawable/text_layout_stroke_normal"
        android:gravity="top"
        android:hint="HELLO"
        android:inputType="textMultiLine"
        android:overScrollMode="always"
        android:padding="15dp"
        android:scrollbarStyle="insideInset"
        android:scrollbars="vertical" />
</android.support.design.widget.TextInputLayout>

Now in code programetically change border and hint color of Edittext using setOnFocusChangeListener

 edittext.setOnFocusChangeListener(new OnFocusChangeListener() {          
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus) {
                GradientDrawable drawable = (GradientDrawable)edittext.getBackground();
                drawable.setStroke(2, Color.RED);
                edittext.setHintTextColor(Color.RED);
            }
        }
    });
like image 87
rafsanahmad007 Avatar answered Oct 13 '22 10:10

rafsanahmad007