4

I have an EditText used for a description.

Below it, I have a TextView showing the number of characters inputted in the EditText.

Example:

enter image description here

The user should be able to see the live character count during the input, but at this moment the characted counter is hidden by the keyboard:

enter image description here

What can I do to correct that ?

Here is my xml code:

    <EditText
        android:id="@+id/MyDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:maxLength="500"
        android:hint="Description" />

    <TextView
        android:id="@+id/MyDescriptionCharCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0/500"
        android:layout_below="@id/MyDescription"
        android:layout_marginTop="5dp"
        android:layout_marginRight="3dp"
        android:layout_marginEnd="3dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

The parent is a RelativeLayout.

Onik
  • 19,396
  • 14
  • 68
  • 91
D. Math
  • 329
  • 6
  • 17

2 Answers2

0

You have to extend the class with TextWatcher and override afterTextChanged(),beforeTextChanged(), onTextChanged().

EditText MyDescription = (EditText)findViewById(R.id. MyDescription);
MyDescription.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // TODO Auto-generated method stub
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
   // s.toString().trim().length();
} 

});

You can try the following layout.

<RelativeLayout
    android:id="@+id/bottom_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="16dp"
        android:background="@android:color/transparent"
        android:inputType="textNoSuggestions|textMultiLine"
        android:maxLength="200"
        android:paddingBottom="8dp"
        android:paddingRight="5dp"
        android:paddingTop="8dp"
        android:textColor="#202020"
        android:textColorHint="#979797"
        android:textSize="14sp"/>


    <TextView
        android:id="@+id/charecter_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/add_comment_edit_text"
        android:layout_gravity="bottom"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:gravity="bottom"
        android:text="0/200"
        android:textColor="#979797"
        android:textSize="14sp"/>
</RelativeLayout>
Alvin Varghese
  • 679
  • 2
  • 9
  • 17
-2

To see Textview of character counter, add below line in activity tag of manifest file.

android:windowSoftInputMode="adjustResize|stateAlwaysHidden"

This will automatically move your activity layout up to fix in available height after adding soft input keyboard.For more detail about this check here.

  • How does the programm know that I want to show the character counter ? There are many others layouts below my character counter – D. Math Feb 17 '17 at 09:45