adding two end icons to TextInputLayout in android - android

i'm trying to achieve something like this
I wanna create TextinputLayout with two end icons , but it doesn't work , the clear end icon show above the search icon ,here is my code :
<FrameLayout
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/searchLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_32dp"
android:gravity="center"
android:hint="#string/search_for_your_product"
app:boxBackgroundMode="none"
app:endIconMode="clear_text"
app:layout_constraintStart_toEndOf="#id/backIcon"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/sh_border_rounded"
android:inputType="text"
android:maxLength="10"
android:maxLines="1"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<ImageView
android:layout_gravity="end|center_horizontal"
android:src="#drawable/ll_search"
android:layout_width="wrap_content"
android:paddingStart="#dimen/normal_margin"
android:paddingTop="#dimen/normal_margin"
android:paddingEnd="#dimen/normal_margin"
android:layout_height="match_parent"/>
</FrameLayout>

i Used a constraint layout as parent with the background ,
and set the icons at the end of the TextInput .
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/searchLayout"
android:layout_width="match_parent"
android:layout_marginStart="#dimen/margin_32dp"
android:background="#drawable/sh_border_rounded"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="#id/backIcon"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/search"
android:padding="#dimen/margin_8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="#dimen/margin_64dp"
android:inputType="text"
android:maxLength="10"
android:background="#null"
android:maxLines="1"
android:singleLine="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/delete"
android:src="#drawable/ic_cancel"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="wrap_content"
android:paddingEnd="#dimen/margin_8"
android:layout_height="match_parent"
app:layout_constraintStart_toEndOf="#+id/search" />
<ImageView
android:src="#drawable/ic_search"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/delete" />
</androidx.constraintlayout.widget.ConstraintLayout>

Related

why windowSoftInputMode doesn't work with constraint layout?

This is more like the general information question that I am asking I tried to implement windowSoftinputMode="adjustResize" mode in constraint layout but it doesn't work for it at all I tried to search for a constraint layout example but there is no example available on the internet. The only example I am able to find is with relative layout and when I implemented it worked like a charm so please tell me why it works like that
here is my constraint layout code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="#+id/fromtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="140dp"
android:fontFamily="#font/nunito_sans_semibold"
android:text="#string/from"
android:textSize="#dimen/_15sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="#+id/fromSpinner"
android:layout_width="match_parent"
android:layout_height="#dimen/_35sdp"
android:layout_marginHorizontal="#dimen/_20sdp"
android:entries="#array/weight"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/fromtext"
app:layout_constraintVertical_bias="0.085" />
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_15sdp"
android:id="#+id/fromvalue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/fromSpinner"
app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="13dp">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:textAlignment="center"
android:textSize="#dimen/_15sdp" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="#+id/totext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_50sdp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_gravity="center"
android:text="#string/to"
app:layout_constraintTop_toBottomOf="#+id/fromvalue"
android:textSize="#dimen/_20sdp"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="#dimen/_35sdp"
android:layout_margin="#dimen/_15sdp"
android:entries="#array/weight"
app:layout_constraintTop_toBottomOf="#+id/totext"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:textAlignment="center"
android:id="#+id/toSpinner"/>
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/toSpinner"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_margin="#dimen/_15sdp"
>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/tovalue"
android:layout_width="match_parent"
android:focusable="false"
android:textSize="#dimen/_20sdp"
android:textAlignment="center"
android:cursorVisible="false"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
update
Now my app behaves like this
when the keyboard is hidden
when the keyboard is not hidden
i want my screen to be fully resized not just one element

Adjust view height to remove extra spaces in a constraint layout

Here, is how my screen looks like
Login screen
I want to remove extra spaces between the submit button and the bottom of the screen.
I researched about this issue in SO. But exactly no one answers it as far I have looked.
Can you suggest some ways to do so?
I have attached the XML layout below for reference.
Login layout
<?xml version="1.0" encoding="utf-8"?>
<layout>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#color/colorAccent"
tools:context=".MainActivity">
<View
android:id="#+id/view_form"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintVertical_weight="0.50"
android:layout_marginTop="#dimen/marginTopLogin"
android:background="#drawable/round_cornered_top_white_bg_drawable"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<EditText
android:id="#+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:autofillHints="name"
android:hint="Username"
android:imeOptions="actionNext"
android:inputType="text"
android:maxLines="1"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="#id/view_form"
app:layout_constraintStart_toStartOf="#id/view_form"
app:layout_constraintTop_toTopOf="#+id/view_form" />
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:autofillHints="password"
android:hint="Password"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:maxLines="1"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="#id/view_form"
app:layout_constraintStart_toStartOf="#id/view_form"
app:layout_constraintTop_toBottomOf="#+id/etUsername"/>
<CheckBox
android:id="#+id/imgTogglePassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp"
android:layout_marginEnd="-5dp"
android:button="#drawable/btn_toggle_password"
android:padding="16dp"
app:layout_constraintBottom_toBottomOf="#id/etPassword"
app:layout_constraintEnd_toEndOf="#id/etPassword"
app:layout_constraintTop_toTopOf="#id/etPassword" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginTop="20dp"
android:background="#color/colorAccent"
app:layout_constraintTop_toBottomOf="#id/etPassword"
app:layout_constraintStart_toStartOf="#id/view_form"
app:layout_constraintEnd_toEndOf="#id/view_form"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
try it
<layout>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#color/colorAccent"
tools:context=".MainActivity">
<View
android:id="#+id/view_form"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="#dimen/marginTopLogin"
android:background="#drawable/round_cornered_top_white_bg_drawable"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="0.50" />
<EditText
android:id="#+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:autofillHints="name"
android:hint="Username"
android:imeOptions="actionNext"
android:inputType="text"
android:maxLines="1"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="#id/view_form"
app:layout_constraintStart_toStartOf="#id/view_form"
app:layout_constraintTop_toTopOf="#+id/view_form" />
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:autofillHints="password"
android:hint="Password"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:maxLines="1"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="#id/view_form"
app:layout_constraintStart_toStartOf="#id/view_form"
app:layout_constraintTop_toBottomOf="#+id/etUsername" />
<CheckBox
android:id="#+id/imgTogglePassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp"
android:layout_marginEnd="-5dp"
android:button="#drawable/btn_toggle_password"
android:padding="16dp"
app:layout_constraintBottom_toBottomOf="#id/etPassword"
app:layout_constraintEnd_toEndOf="#id/etPassword"
app:layout_constraintTop_toTopOf="#id/etPassword" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Layout not scrolling and Views overlapping when Keyboard is shown Android Constraint layout

I have facing an issue, where my login screen is not scrolling and views are overlapping with button when keyboard shows up.
below is the code for Login layout, can somebody help me why views are getting overlapped
i pasted the login layout file here, Linearlayout at the bottom of the layout file is overlapping with Linearlayout defined justabove it , when Keyboard is opened. and the view is not scrolling.
<data>
<!-- databinding data here -->
</data>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/top_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.25" />
<ImageView
android:id="#+id/img_logo_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/top_guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/txt_title_m_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="#+id/top_guideline" />
<TextView
android:id="#+id/txt_signin_proceed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txt_title_m_id" />
<RelativeLayout
android:id="#+id/rl_olk_sign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txt_signin_proceed">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="#+id/txt_olk_sign"
android:src="#drawable/ic_olk" />
<TextView
android:id="#+id/txt_olk_sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"/>
</RelativeLayout>
<TextView
android:id="#+id/txt_no_olk_credentials"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="62dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="62dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rl_olk_sign" />
<TextView
android:id="#+id/txt_term_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="62dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="62dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txt_no_olk_credentials"/>
<TextView
android:id="#+id/tv_signin_username_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
android:layout_marginTop="#dimen/_10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txt_term_login"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:lineHeight="21sp"
android:padding="#dimen/_5dp"/>
</RelativeLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/cl_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="#+id/txt_signin_proceed">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/input_layout_user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/et_input_user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/input_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/input_layout_user_name">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/et_input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:id="#+id/rl_sign_in_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="#dimen/_20dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="42dp"
android:onClick="#{() -> callback.method2()}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/input_layout_password">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
android:gravity="center"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="#+id/rl_copy_right"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="#dimen/_5dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:id="#+id/tv_back_to_signin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:overScrollMode="always"
android:layout_above="#id/tv_copyright_text"/>
<TextView
android:id="#+id/tv_copyright_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:overScrollMode="always"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="visible" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
add this adjustResize to the activity tag in your manifest , then your view will shrink if keyboard opens.
<activity
android:name=".Activities.MyActivity"
android:windowSoftInputMode="adjustResize" ----->this tag
/>
and for your scroll it might help but let me know if it didnt , and post some screenshots of how your view looks.

How to constrain a sibling view to same height as that of TextInputEditText of an TextInputLayout

I want to show the label "Km" with a white background with the same height as that of the edit text field.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayoutInputDist"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="wrap_content"
android:layout_height="42dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/inputDist"
android:layout_width="80dp"
android:layout_height="match_parent"
android:background="#drawable/bg_left_corner_shape"
android:backgroundTint="#android:color/white"
android:fontFamily="#font/roboto_regular"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
android:maxEms="4"
android:padding="4dp"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="#+id/label_km"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="4dp"
android:background="#drawable/bg_right_corner_shape"
android:backgroundTint="#android:color/white"
android:fontFamily="#font/roboto_regular"
android:gravity="center_vertical"
android:text="#string/km"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textInputLayoutInputDist"
app:layout_constraintTop_toTopOf="#+id/textInputLayoutInputDist" />
</androidx.constraintlayout.widget.ConstraintLayout>
But the TextInputLayout is taking some more height that the edit text field, I cant align the edit text and the "Km" label vertically the same levels.
As I understand your question check below answer
Give kmLable top and bottom constraint to edit text. Here I give fix width for a temporary purpose (change according to your requirement).
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayoutInputDist"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/inputDist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/dividerColor"
android:fontFamily="#font/roboto_bold"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
android:maxEms="4"
android:padding="4dp"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="#+id/label_km"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#color/dividerColor"
android:fontFamily="#font/roboto_medium"
android:gravity="center_vertical"
android:text="km"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/textInputLayoutInputDist"
app:layout_constraintStart_toEndOf="#+id/textInputLayoutInputDist"
app:layout_constraintTop_toTopOf="#+id/textInputLayoutInputDist" />
</androidx.constraintlayout.widget.ConstraintLayout>
try
setHintEnabled(false)
in your xml like this:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayoutInputDist"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:hintEnabled="false">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/inputDist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/dividerColor"
android:fontFamily="#font/roboto_bold"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
android:maxEms="4"
android:padding="4dp"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>

TextInputLayout setCounterEnables doesn't work - Android

I have a layout with a TextInputLayouts elements and I want to enable counter of characters. The problem is it's not working and I can't see the counter when I run the app.
Here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:background="#color/colorBackground"
android:paddingLeft="#dimen/create_step_padding"
android:paddingRight="#dimen/create_step_padding"
android:paddingTop="#dimen/create_step_padding">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Shipper Text Area-->
<android.support.design.widget.TextInputLayout
android:id="#+id/shipper_layout"
android:layout_width="0dp"
android:layout_height="110dp"
android:background="#drawable/text_area_background"
android:paddingEnd="#dimen/text_input_layout_side_padding"
android:paddingStart="#dimen/text_input_layout_side_padding"
android:paddingTop="#dimen/text_input_layout_top_padding"
app:counterEnabled="true"
app:hintTextAppearance="#style/HintTextStyle"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="#+id/shipper_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:gravity="top|start"
android:hint="#string/shipper_field"
android:inputType="textMultiLine"
android:lines="5"
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical" />
</android.support.design.widget.TextInputLayout>
<!-- Consignee Text Area-->
<android.support.design.widget.TextInputLayout
android:id="#+id/consignee_layout"
android:layout_width="0dp"
android:layout_height="110dp"
android:layout_marginTop="16dp"
android:background="#drawable/text_area_background"
android:paddingLeft="#dimen/text_input_layout_side_padding"
android:paddingTop="#dimen/text_input_layout_top_padding"
app:counterEnabled="true"
app:hintTextAppearance="#style/HintTextStyle"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/shipper_layout">
<android.support.design.widget.TextInputEditText
android:id="#+id/consignee_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:gravity="top|start"
android:hint="#string/consignee_field"
android:inputType="textMultiLine"
android:lines="5"
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical" />
</android.support.design.widget.TextInputLayout>
<!-- Airport of Departure -->
<!-- Airport of Destination -->
<com.silverfix.dgdeditor.utils.views.AirportAutoCompleteEditText
android:id="#+id/aodep_field"
android:layout_width="217dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintBottom_toBottomOf="#+id/textView11"
app:layout_constraintHorizontal_bias="0.512"
app:layout_constraintLeft_toRightOf="#+id/textView11"
app:layout_constraintRight_toRightOf="parent" />
<com.silverfix.dgdeditor.utils.views.AirportAutoCompleteEditText
android:id="#+id/aodes_field"
android:layout_width="217dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintBottom_toBottomOf="#+id/textView12"
app:layout_constraintLeft_toLeftOf="#+id/aodep_field" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text="#string/aodep_field"
android:textAppearance="#style/DefaultTextStyle"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/consignee_layout" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="#string/aodes_field"
android:textAppearance="#style/DefaultTextStyle"
app:layout_constraintLeft_toLeftOf="#+id/textView11"
app:layout_constraintTop_toBottomOf="#+id/textView11" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
As you can see, I enabled app:counterEnabled in the TextInputLayouts.
Then I try to set the max chars inside my code.
Here is the code:
shipperTextArea = (TextInputLayout) rootView.findViewById(R.id.shipper_layout);
consigneeTextArea = (TextInputLayout) rootView.findViewById(R.id.consignee_layout);
shipperTextArea.setCounterMaxLength(60);
consigneeTextArea.setCounterMaxLength(60);
just set the counter in xml files, add this:
<android.support.design.widget.TextInputLayout
app:counterMaxLength="60"
app:counterEnabled="true"
i think that don't need to set in the java class.

Categories

Resources