Show label of TextInputEditText when not focused on this - android

I have a layout with two TextInputEditText and when I set a text on this (programmatically or with xml) and set focus on another TextInputEditText, the above label of TextInputEditText was hidden.
I want to show this label even a text was written in this.
My xml code:
<?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:background="#color/whiteGray2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:orientation="horizontal"
android:weightSum="2"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp">
<com.google.android.material.textfield.TextInputLayout
style="#style/CustomBorderTextInputEditText"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:hint="#string/oldCount"
app:hintTextColor="#color/gray_text">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/input_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:inputType="none"
android:imeOptions="actionNext"
android:text="Hi How are you"
android:textColor="#color/gray_text"
android:textColorHint="#color/gray_text" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="#style/CustomBorderTextInputEditText"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:hint="#string/newCount">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/newCount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:textColorHint="#color/gray_text"
android:hint="#string/newCount"
android:textColor="#color/gray_text"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
My style:
<style name="CustomBorderTextInputEditText" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">#color/text_input_box_stroke</item>
<item name="hintTextColor">?attr/colorPrimary</item>
</style>

Your issue is here:
<com.google.android.material.textfield.TextInputEditText
android:textColorHint="#color/gray_text"
Remove android:textColorHint in the TextInputEditText and the use:
<com.google.android.material.textfield.TextInputLayout
app:hintTextColor="#color/colorSecondary"
android:textColorHint="#color/text_input_hint_selector"
The hintTextColor is the color of the label when it is collapsed and the text field is active.
The android:textColorHint is the color of the label in all other text field states (such as resting and disabled).
Just an example:
And this is the selector:
Here the TextInputLayout:
Without text:
Focused:
With text not focused:
Disabled:

Add it in your xml code :
android:focusableInTouchMode="true"
or
Add it in your java code :
EditText newCount = (EditText)findViewById(R.id.newCount);
newCount.setFocusable(true);
may be it usefull to you, just try it.

I found my answer. I should add textColorHint and set a color instead of add hintTextColor attribute. But now I am confused, What is the difference between both of them?

Related

How to change EditText style in AlertDialog?

I added Cyanea (a theme engine) into my app and the EditText style changed (see images, color doesn't matter. The first picture is how it looks now, and the second one is what I want the dialog to look like). How can I change old EditText style back?
My code:
dialog_input.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="4dp"
android:paddingTop="16dp">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/md_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/subj_name_input_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Hello, World!"
android:hint="#string/subject_name_dialog_hint"
android:inputType="text"
android:maxLines="1"
android:maxLength="32" />
</com.google.android.material.textfield.TextInputLayout>
</FrameLayout>
Use the Widget.Design.TextInputLayout style:
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.Design.TextInputLayout"

how to click on drawableRight on every TextInputEditText

how to do click on drawableRight edit image of every TextInputEditText? I want to click on the edit icon and then enable to edit each field. How can i do this?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--First name-->
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="#dimen/_64dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/etFirstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:drawableRight="#drawable/icon_edit"
android:hint="#string/label_fname"
android:inputType="textCapWords" />
</com.google.android.material.textfield.TextInputLayout>
<!--Middle name-->
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/activity_registration_tiMiddleName"
android:layout_width="match_parent"
android:layout_height="#dimen/_64dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/etMiddleName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:drawableRight="#drawable/icon_edit"
android:hint="#string/label_mname"
android:inputType="textCapWords"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
so how to each field individually edit using drawbleRight. Please help.
Don't use android:drawableRight in the TextInputEditText.
Use:
<com.google.android.material.textfield.TextInputLayout
...
app:endIconMode="custom"
app:endIconDrawable="#drawable/..."
and:
textInputLayout.setEndIconOnClickListener {
// Respond to end icon presses
}

Android: How to create custom UI of TextInputLayout

I am trying to create custom TextInputLayout. How can I create below custom TextInputLayout?
Any help or guidance will be well appreciated.
Just use the TextInputLayout provided by the Material Components Library.
Something like:
<com.google.android.material.textfield.TextInputLayout
app:boxBackgroundColor="#color/...."
app:boxStrokeColor="#color/..."
android:hint="..."
..>
<com.google.android.material.textfield.TextInputEditText
../>
</com.google.android.material.textfield.TextInputLayout>
About the rounded corners:
The default behaviour for the FilledBox (Widget.MaterialComponents.TextInputLayout.FilledBox) is a rounded box on the top corners (4dp) and a rectangular box on the bottom (0dp) as you can see in the image above.
If you would like a rounded box, I suggest you using the OutlinedBox style:
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
..>
The resul is:
Otherwise you can force a workaround like this (I don't like it since it breaks the material guideline):
<com.google.android.material.textfield.TextInputLayout
app:shapeAppearanceOverlay="#style/Rounded_ShapeAppearanceOverlay.MaterialComponents.TextInputLayout.FilledBox"
app:boxStrokeWidth="0dp"
app:boxStrokeColor="yourActivityBackgroundColor"
..>
where the app:shapeAppearanceOverlay attribute changes the shape of the bottom corners:
<style name="Rounded_ShapeAppearanceOverlay.MaterialComponents.TextInputLayout.FilledBox" parent="">
<item name="cornerSizeBottomLeft">#dimen/mtrl_shape_corner_size_small_component</item>
<item name="cornerSizeBottomRight">#dimen/mtrl_shape_corner_size_small_component</item>
</style>
where mtrl_shape_corner_size_small_component=4dp
try as below
<LinearLayout
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"
tools:context=".Main2Activity"
android:orientation="vertical"
android:background="#d4d4d4"
android:weightSum="1">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="fullname"
android:background="#FFFFFF"
android:layout_margin="10dp"
android:padding="10dp"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Aalina Rodgers"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="fullname"
android:background="#FFFFFF"
android:layout_margin="10dp"
android:padding="10dp"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Aalina Rodgers"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="fullname"
android:background="#FFFFFF"
android:layout_margin="10dp"
android:padding="10dp"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Aalina Rodgers"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

I am trying to make AutoCompleteTextView look like a material Edittext

I am trying to make AutoCompleteTextView look like this image https://ibb.co/ZJxmgK5 but unable to do the same. The outlined box is not visible in the result
Below is my current code. Earlier I tried adding an EditText in TextInputLayout but now I want to add Autocomplete in this.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/input_layout_holiday_destination"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/lbl_destination_big">
<AutoCompleteTextView
android:id="#+id/edittext_holiday_destination"
style="#style/Base.Widget.AppCompat.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:background="#android:color/transparent"
android:hint="#string/lbl_destination"
android:layout_marginLeft="#dimen/margin8"
android:singleLine="true"
android:lines="1"
android:textSize="25sp"
android:nextFocusDown="#+id/txv_holiday_num_of_nights"
android:imeOptions="actionNext"
android:textCursorDrawable="#drawable/edittext_cursor_drawable"/>
/>
</com.google.android.material.textfield.TextInputLayout>
The expected result is the image for which the URL has been given above.
If you would like to have a TextInputLayout in conjuction with an AutoCompleteTextView you should use this style for the TextInputLayout Widget.MaterialComponents.TextInputLayout.*.ExposedDropdownMenu.
In your case:
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
Use something like:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/name_text_input"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HINT TEXT">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"/>
</com.google.android.material.textfield.TextInputLayout>
Check also the doc:
Note: When using an outlined text field with an EditText child that is not a TextInputEditText, make sure to set the EditText's android:background to #null. This allows TextInputLayout to set an outline background on the EditText.
If you would like to avoid the dropdown icon just use the app:endIconMode attribute.
<com.google.android.material.textfield.TextInputLayout
app:endIconMode="none"
...>
Below code solves the problem:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/input_layout_holiday_destination"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/lbl_destination_big">
<androidx.appcompat.widget.AppCompatAutoCompleteTextView
android:id="#+id/edittext_holiday_destination"
style="#style/Widget.MaterialComponents.AutoCompleteTextView.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:singleLine="true"
android:lines="1"
android:nextFocusDown="#+id/txv_holiday_num_of_nights"
android:imeOptions="actionNext"
android:textCursorDrawable="#drawable/edittext_cursor_drawable"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/input_layout_holiday_destination"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/edittext_holiday_destination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Destination"
android:layout_weight="10"
android:singleLine="true"
android:lines="1"
android:textSize="25sp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
tools:text="Hello" />
/>
</com.google.android.material.textfield.TextInputLayout>
This works. The problem was with the background color that you were setting to the AutoCompleteTextView.
finally my answer is this
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/uomLay"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="#id/quantity_lay"
app:layout_constraintStart_toStartOf="#id/start2"
app:layout_constraintEnd_toEndOf="#id/end2"
style="#style/OutlineBoxDropDown"
app:endIconDrawable="#drawable/ic_path_16692"
app:shapeAppearance="#style/Rounded"
app:hintTextAppearance="#style/hintTextAppearence"
android:layout_marginTop="#dimen/_13sdp"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/uom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/graphikregular"
android:hint="UOM"
style="#style/Widget.MaterialComponents.AutoCompleteTextView.OutlinedBox"
android:background="#null"
android:paddingVertical="#dimen/_7sdp"
android:textColor="#color/desc_color"
android:textSize="#dimen/_11ssp"
tools:text="kg" />
</com.google.android.material.textfield.TextInputLayout>

Hint not visible with TextInputLayout on non focus state

I'm using the new TextInputLayout from the Android design library,
When the EditText is focused, everything is okay, and I can see the hint above the EditText:
But when the EditText loses focus, the hint totally disappears:
I expect that the hint will return to the EditText, what am I missing?
xml code below.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Try me!"/>
</android.support.design.widget.TextInputLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Try me 2"/>
</LinearLayout>
This issue is fixed as of 22.2.1, update your Android SDK.
my SDK is updated so the accepted answer didnt work for me.
the fix in my case was to set a custom style OR to override some props:
give color values to android:textColorHint, app:boxStrokeColor and app:hintTextColor
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/input_layout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Edit Text"
android:textColorHint="#color/some_color"
app:boxStrokeColor="#color/some_color"
app:hintTextColor="#color/some_color"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/input_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>

Categories

Resources