3

I'm trying to fit the TextInputEditText to the whole screen, but it's not without problems:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:counterEnabled="true"
        app:counterMaxLength="1000"
        app:passwordToggleEnabled="false"
        app:endIconMode="none">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_text"
            android:hint="Type here..."
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:maxLength="1000"
            android:gravity="top"/>

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

</LinearLayout>

Input type:

TextInputEditText tv = findViewById( R.id.edit_text );
tv.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE );

The result is that the counter is gone and hint appears in the middle. If I change to wrap content it's back to normal behavior. Example:

enter image description here

So basically I'll be satisfied with just showing the counter, aligning the hint to the top is secondary.

RonTLV
  • 2,376
  • 2
  • 24
  • 38

5 Answers5

7

Just try weight instead of match_parent. android:layout_weight="1" counter will be visible.

The hint alignment is ok when we tap EditText or when EditText is in focus. As EditText is in full screen request focus will solved the hint alignment problem.

 TextInputEditText tv = findViewById( R.id.edit_text );
 tv.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE );
 tv.requestFocus();

your layout will be as below`

<LinearLayout
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    >

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:counterEnabled="true"
        app:counterMaxLength="1000"
        app:counterTextColor="@color/black"
        app:passwordToggleEnabled="false"
        app:endIconMode="none">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_text"
            android:hint="Type here..."
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:maxLength="1000"
            android:gravity="top"/>

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

</LinearLayout>`
Manish
  • 1,071
  • 2
  • 10
  • 27
  • ```android:layout_weight="1"``` works, but don't you need to set ```android:layout_height="match_parent"``` of ```TextInputEditText``` instead of wrap to fit the whole screen – ljk Feb 11 '20 at 05:12
1

Screenshot

Layout:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:passwordToggleEnabled="false"
        app:endIconMode="none">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_text"
            android:hint="Type here..."
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:maxLength="1000"
            android:gravity="top"/>

    </com.google.android.material.textfield.TextInputLayout>
        <TextView
            android:id="@+id/txtcount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:textColor="@color/colorAccent"
            android:textSize="10sp"
            android:visibility="gone"
            android:text="24"/>
    </RelativeLayout>
</LinearLayout>

Activity

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    if(StringUtils.isNotEmpty(editText.getText().toString().trim())) {
                        txtcount.setVisibility(View.VISIBLE);
                        txtcount.setText(String.valueOf(editText.getText().length()));
                    }else {
                        txtcount.setVisibility(View.GONE);
                    }
                } catch (Exception e) {
                    txtcount.setVisibility(View.GONE);
                    e.printStackTrace();
                }

            }
        });
Sagar
  • 554
  • 5
  • 21
0

I did tested and it't work fine on my device:

<?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:padding="16dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:counterEnabled="true"
        app:counterMaxLength="1000"
        app:passwordToggleEnabled="false"
        >

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/edit_text"
            android:hint="Type here..."
            android:maxLength="1000"
            android:gravity="top|left"/>

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


</LinearLayout>
Hoàng Vũ Anh
  • 647
  • 1
  • 8
  • 24
  • not showing the counter on my device (oneplus 6 android 10) and not on emulator (android 7.1) – RonTLV Feb 04 '20 at 10:45
0

I would switch to EditText instead of TextInputEditText

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:counterEnabled="true"
        app:counterMaxLength="1000"
        app:endIconMode="none"
        app:passwordToggleEnabled="false">

        <EditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:ems="10"
            android:hint="Type here..."
            android:inputType="textPersonName"
            android:maxLength="1000" />

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

</LinearLayout>
Stachu
  • 1,614
  • 1
  • 5
  • 17
0

Just give android:layout_weight="1" to your TextInputEditText. This trick worked for me.

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

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:counterEnabled="true"
        app:counterMaxLength="1000"
        app:counterOverflowTextAppearance="@dimen/_20sdp"
       >

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_text"
            android:hint="Type here..."
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:maxLength="1000"
            android:gravity="top"/>

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

</LinearLayout>

Hope this will help you!!

Nehal Godhasara
  • 787
  • 3
  • 7