Android: View.Gone not working for AutoCompleteTextview as ExposedDropdownMenu - android

I have an ExposedDropDownMenu as a Spinner as recommended from Material.IO. My problem is that, View.Gone does not work and leaves an arrow in the view and therefore still occupies space.
Screenshot
XML
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
android:layout_width="0dp"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/calibrate_message_dropdown_menu_TWO"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
tools:ignore="LabelFor" />
</com.google.android.material.textfield.TextInputLayout>
Code
calibrate_message_dropdown_menu_TWO.visibility = View.GONE
I appreciate any help. Thank you!

Can you try hiding the TextInputLayout instead of the contained AutoCompleteTextView. Add an id for the outer TextInputLayout like this:
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
android:id="#+id/dropdown_layout"
android:layout_width="0dp"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/calibrate_message_dropdown_menu_TWO"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
tools:ignore="LabelFor" />
</com.google.android.material.textfield.TextInputLayout>
and then in code:
dropdown_layout.visibility = View.GONE

If you want to remove the dropdown icon just use:
textInputLayout.endIconMode = TextInputLayout.END_ICON_NONE

Related

Text Input layout hint is overlapped with the outlined box

my text input layout's hint text seems overlapped
how can I move the hint text to little right of the outline.
my xml :
<com.google.android.material.textfield.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tilAddress"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:padding="5dp"
app:hintAnimationEnabled="true"
app:hintTextColor="#color/colorPrimaryDark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvBillingText">
<EditText
android:id="#+id/tilAddress1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Address1 *"
android:background="#null"
android:padding="12dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvBillingText"/>
</com.google.android.material.textfield.TextInputLayout>`
any hep is appreciated . thanks
Edit
By removing the margin , i could fix the issue
Have you tried setting the hint on the TextInputLayout instead of the EditText? I think it's also recommended to replace the EditText with a TextInputEditText like in the following example:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/form_username">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
I removed the layout_margin from Edittext and it could solve my issue

Remove dropdown arrow from AutoCompleteTextView

I want to remove dropdown arrow which is created by AutoCompleteTextView when I use TextInputLayout and use Style: ExposedDropdownMenu
Below is my code:
<com.google.android.material.textfield.TextInputLayout
android:layout_marginTop="10dp"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:hint="Marital Status"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:boxStrokeColor="#color/colorPrimaryDark">
<AutoCompleteTextView
android:background="#null"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/edt_marital" />
</com.google.android.material.textfield.TextInputLayout>
If you would like to avoid the dropdown icon just use the app:endIconMode attribute.
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
app:endIconMode="none"
...>
Before and after:
Hello guys, after having searched a lot and found little. I was trying to achieve how I wanted and I wanted to share the experience and result with you.
As the same Material Design documentation says.
A result Image Dropdown Menu similar to a Spinner, following the documentation. Next code:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/txtAnswer"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_top_15dp"
android:layout_marginEnd="10dp"
app:endIconDrawable="#drawable/ic_arrow_down"
app:hintTextAppearance="#style/styleFilterLabelLatoRegular"
app:layout_constraintEnd_toStartOf="#+id/txtAssignedTo"
app:layout_constraintStart_toStartOf="#+id/guideLineBegin"
app:layout_constraintTop_toBottomOf="#+id/txtSearchQuestionnaire">
<AutoCompleteTextView
android:id="#+id/actvTypesAnswer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:hint="#string/text_answer" />
Now a result as expected from an AutoCompleteTextView Image AutoCompleteTextView. Code xml.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/txtSearchQuestionnaire"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_top_15dp"
app:endIconMode="none"
app:hintTextAppearance="#style/styleFilterLabelLatoRegular"
app:layout_constraintEnd_toEndOf="#id/guideLineEnd"
app:layout_constraintStart_toStartOf="#+id/guideLineBegin"
app:layout_constraintTop_toBottomOf="#+id/txtPeriodActivation">
<AutoCompleteTextView
android:id="#+id/actvNameQuestionnaire"
style="#style/textAppearanceSettingInputEdittextFilter.Enabled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/ic_search"
android:hint="#string/text_search_name_questionnaire" />
</com.google.android.material.textfield.TextInputLayout>
The only changes were to add the attribute app: endIconMode = "none" in TextInputLayout and in the AutoCompleteTextView the attribute android: drawableEnd="#drawable/ic_search"
Excuse my level of English!!

Material design Spinner using TextInputLayout.OutlinedBox styling

I am currently using Material Design TextInputLayout OutlinedBox as shown below:
<android.support.design.widget.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title"
android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
I am trying to add a dropdown box Spinner under my TextInputEditText, and would like to keep the same styling: OutlinedBox.
I see that dropdowns seem to be supported in Material Design, Material Design Text Fields. As shown on here for the Area:
I am currently using a Spinner to generate the Dropdown.
<Spinner
style="#style/Widget.AppCompat.Spinner.DropDown"
android:id="#+id/option"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:dropDownWidth="match_parent" />
It doesn't seem possible to add a dropdown following the OutlinedBox design. Is there a library out there that would allow me to make this happen, or is there a better way to implement this within Material Design?
I am assuming you want to have an Exposed drop-down menu inside the TextInputLayout I had the same problem, what you can do is use AutoCompleteTextView inside your TextInputLayout as in the following in the XML. here's an example of how I approached the issue.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingRight="30dp"
android:paddingEnd="30dp"
tools:ignore="RtlSymmetry"
android:layout_margin="5dp">
<ImageView
android:layout_width="30dp"
android:layout_margin="10dp"
android:layout_height="match_parent"
app:srcCompat="#drawable/ic_location_city_black_24dp"
android:layout_gravity="center"
/>
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type"
android:orientation="horizontal"
>
<AutoCompleteTextView
android:id="#+id/filled_exposed_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</LinearLayout>
You will also need an item layout resource to populate the dropdown popup. The example below provides a layout that follows the Material Design guidelines.
res/layout/dropdown_menu_popup_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceSubtitle1"/>
In your class add the following code depending on what you want.
String[] type = new String[] {"Bed-sitter", "Single", "1- Bedroom", "2- Bedroom","3- Bedroom"};
ArrayAdapter<String> adapter =
new ArrayAdapter<>(
this,
R.layout.dropdown_menu_popup_item,
type);
AutoCompleteTextView editTextFilledExposedDropdown =
findViewById(R.id.filled_exposed_dropdown);
editTextFilledExposedDropdown.setAdapter(adapter);
incase this doesn't help kindly check Exposed Dropdown Menus in material design page.
[https://material.io/develop/android/components/menu/][1]
This is my first answer on stack overflow I hope it helps.
Just use the TextInputLayout included in the Material Components Library with the style Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu.
Something like:
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:hint="Hint text"
...>
<AutoCompleteTextView
android:id="#+id/outlined_exposed_dropdown_editable"
.../>
</com.google.android.material.textfield.TextInputLayout>
I believe that this document isn't showing a Spinner at all. I think it's showing a TextInputLayout with a dropdown icon.
In the Anatomy section, at the Icons subsection, it says
5. Dropdown icon
A dropdown arrow indicates that a text field has a nested selection component.
Now, how you provide the "nested selection component" I'm not sure...
From the other answers, "AutoCompleteTextView" is the answer but it does not do the same as a spinner does.
Here is my solution. Just put normal edittext inside TextInputLayout and make this editText disabled for inputs. And put a 0dp,0dp spinner for normal spinner working.
Don't make spinner visibility=gone, because if it's gone, spinner listener does not work
layout.xml
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_10dp"
android:theme="#style/ThemeOverlay.App.TextInputLayout">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/arrow_down_pacific_blue"
android:focusable="false"
android:hint="şehir"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>
<Spinner
android:id="#+id/spinner"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:background="#android:color/transparent"
android:spinnerMode="dialog"
tools:listitem="#layout/general_spinner_item" />
java code
set click listener to edittext for trigger spinner click
findViewById(R.id.editText).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
spinner.performClick();
}
});
in spinner listener, set edittext text from selected item,
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedCity= (City) parent.getAdapter().getItem(position);
editText.setText(selectedCity.getScreenText());
RDALogger.debug("selectedObject " + selectedCity);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
and the result view
I am using the below material libs to get spinner
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
Here is my layout look like
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:hint="#string/select_wifi"
app:hintTextAppearance="#style/hintStyle"
app:startIconDrawable="#drawable/wifi">
<com.google.android.material.textfield.MaterialAutoCompleteTextView
style="#style/textInputEdittext"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>
Check out this image for the spinner
It seems like they actually use a TextInputLayout wrapping up an AutoCompleteTextView. Note that they are already the material Components theme [https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md].
See:
https://material.io/design/components/menus.html#exposed-dropdown-menu
https://material.io/develop/android/components/menu/
I solved my problem using this:
in XML:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/layout"
style="#style/AppTheme.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/my_spinner_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
in code:
layout.keyListener=null
ArrayAdapter(it, android.R.layout.simple_list_item_1, list).also {
adapter ->
layout.setAdapter(adapter)
}
Credits:How to make EditText not editable through XML in Android?
You can check my Medium article where I introduce a custom MaterialSpinner which supports two-way data binding and selection tracking. The resulting layout can look as simple as this:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/input_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/selection_hint"
app:startIconDrawable="#drawable/selection_icon"
app:boxBackgroundMode="outline"
app:endIconMode="#{viewModel.items == null || viewModel.items.size() != 0 ? TextInputLayout.END_ICON_DROPDOWN_MENU : TextInputLayout.END_ICON_NONE}">
<com.example.MaterialSpinner
android:id="#+id/items"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:items="#{viewModel.items}"
app:selectedPosition="#={viewModel.selectedItemPosition}"
app:emptyText="#string/selection_no_item_text" />
</com.google.android.material.textfield.TextInputLayout>
I know its too late to answer this question, but somebody like me stuck in issue like this or similar may find this very useful. Visit this repo its perfect solution as requested. This library support all material TextInputLayout styles. Thanks to "Mamoon Al-hawamdeh" for this amazing library.
All this answers are helpful but the one important note that is if you set app:endIconMode attribute, your Drop down menu not work.
Just change inputType from "text" to "none"
<android.support.design.widget.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title"
android:inputType="none"/>
</android.support.design.widget.TextInputLayout>

Android - TextInputLayout doesn't wrap hint of TextInputEditText

I have a simple TextInputEditText nested in TextTnputLayout.
I am trying to make the of TIL to be as long as the hint given in TIET.
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="This is a hint"/>
</android.support.design.widget.TextInputLayout>
But the problem is, it doesn't show anything
I have to set TIL's width to match_parent or just any other width other than wrap_content. But I want to the width to be dynamic as in, the width of TIL follows the length of the hint provided. Is it possible? If yes, how to do that?
I am using support library version 26.0.2 btw.
Yvette Colomb's answer is close, the TIL wraps the TIET but the fancy animation doesn't work there. It might be because the animation won't start if hints are set in execution?
You can do so, by setting the hint in the java code.
Remove the hint from the xml:
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/inputTxt"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.TextInputLayout>
Declare and initialise the TextInputEditText in the activity and set the hint.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextInputEditText inputTxt = (TextInputEditText) findViewById(R.id.inputTxt);
inputTxt.setHint("This is a hint");
}
This will wrap the text to the width of the hint as you type (not saying this is how it goes for all devices)
Then expand the width when you've entered the text.
To answer your updated question.
How to make the input layout grow as we enter input.
<android.support.design.widget.TextInputLayout
android:id="#+id/txtInputL"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/inputTxt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:maxLines="1"/>
The Java.
final TextInputEditText inputTxt = (TextInputEditText) findViewById(R.id.inputTxt);
inputTxt.setHint("This is a hint");
final TextInputLayout txtInputL = (TextInputLayout) findViewById(R.id.txtInputL);
txtInputL.setMinimumWidth(300);
txtInputL.setHint("This is a hint");
I've added a floating hint:
As we're typing:
If you have any other questions about this, please ask a new question.
You can achieve this from xml instead, just set the hint for both TextInputLayout and TextInputEdittext and it will work then to make the TextInputEdittext grow as you are typing you have to set the it maxlines="1" and the layout_height to wrap_content as below
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="This is edittext">
<android.support.design.widget.TextInputEditText
android:hint="This is edittext
android:maxLines="1"
android:inputType="numberDecimal"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</android.support.design.widget.TextInputLayout>
I hope this help someone
Try this:
<android.support.design.widget.TextInputLayout
android:id="#+id/txt_inputLayout_gify"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/edt_gift_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="This is hint." />
</android.support.design.widget.TextInputLayout>
I found a type of workaround, for me I wanted to show Country Code in a disabled Material Design styled EditText, but same as you I was unable to use the wrap_context inside TextInputLayout.
So as a workaround I added the hint text in both TextInputLayout and TextInputEditText, and since I had a predefined text inside the TextInputEditText, the hint of TextInputEditText never appeared on screen, but made space for the hint of TextInputLayout to be visible.
Here is what I did...
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="16dp">
...
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/country_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Country code"
app:hintTextColor="#737373"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+91"
android:hint="Country Code"
android:enabled="false"
android:textColor="#737373"
.../>
</com.google.android.material.textfield.TextInputLayout>
...
</androidx.constraintlayout.widget.ConstraintLayout>
P.S. You might want to add a few more characters in the TextInputEditText hint, as that is what is controlling the spacing. Also the constraint layout is there just because I had other elements in the activity, you can avoid it and use any of your root view.
Screenshot
set your xml like so:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/text_field_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/input_text_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
/>
</com.google.android.material.textfield.TextInputLayout>
Now, adjust the size of the input_text_field to be BY THE SIZE OF THE HINT, depending on the hint size, like so:
val tf = input_text_field
val layout = text_field_layout
val hint = "Hint"
val measureText = tf.paint.measureText(hint).toInt()
tf.width = tf.paddingLeft + tf.paddingRight + measureText.toInt()
layout.hint = hint
Try doing this.
<android.support.design.widget.TextInputEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="This is a hint"/>

Android TextInputLayout hint overlaps EditText hint

I am facing a weird issue, I have a InputTextLayout and an EditText in it, and I am trying to achieve something like this (shown in image below) (Image from material design guidlines: https://material.io/guidelines/components/text-fields.html#text-fields-layout), where there are two separate hints. I am doing this by adding android:hint to both layouts. This works fine, but when the focus moves away from this, the "label" moves down and overlaps the "placeholder" text. (Only when the user has not given any input and the edit text is empty - both hints overlap). Any pointers on how to fix this?
As a requirement I need both hints to be there, and the "Label" should not move down on focus change. Both hints should remain in their position
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Label">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Placeholder"
android:inputType="textCapWords"
/>
</android.support.design.widget.TextInputLayout>
Now the material components (alpha03) have support for placeholder text:
dependencies {
// ...
implementation 'com.google.android.material:material:1.2.0-alpha03'
// ...
}
<com.google.android.material.textfield.TextInputLayout
...
app:placeholderText="Placeholder text">
Perfect One!!!
xml code
<android.support.design.widget.TextInputLayout
android:id="#+id/inputLayoutPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="Phone Number">
<android.support.design.widget.TextInputEditText
android:id="#+id/edtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="+91"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
Kotlin code
edtPhone.hint=""
edtPhone.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->
inputLayoutPhone.isHintEnabled = hasFocus
if(hasFocus){
edtPhone.hint="+91"
}else{
edtPhone.hint= "Phone Number"
}
}
Java code
edtPhone.setHint("");
edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus){
edtPhone.setHint("+91");
}else{
edtPhone.setHint("Phone Number");
}
}
});
As i know, we can't do as your wish.
Because, TextInputLayout is designed to float the hint once it gets focused So, once it went up nothing will be there in the place holder. We can do your requirement with slight changes as below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:gravity="center"
tools:context="com.stackoverflow.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Label"
android:textColor="#color/colorPrimary" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
app:hintEnabled="false">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:hint="Place Holder"
/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Placeholder">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
/>
</android.support.design.widget.TextInputLayout>
Use the code below. It should work fine.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:textColorHint="#color/white">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Placeholder"
android:inputType="text"
android:textColor="#color/white"
android:textColorHint="#color/white" />
</android.support.design.widget.TextInputLayout>
Remove hint from either AppcompatEditText or TextInputLayout
use android:hint="" only in one either AppcompatEditText or TextInputLayout
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Placeholder"
android:inputType="textCapWords"
/>
</android.support.design.widget.TextInputLayout>
for more information check this link
For me best solution which I found is answered in https://stackoverflow.com/a/69261116/4024146
try to set hint separately for AppCompatEditText and TextInputLayout:
xml code:
<android.support.design.widget.TextInputLayout
android:id="#+id/inputLayoutPhone"
...>
<android.support.design.widget.TextInputEditText
android:id="#+id/edtPhone"
... />
</android.support.design.widget.TextInputLayout>
Kotlin code:
inputLayoutPhone.setHint("Label")
edtPhone.setHint("Placeholder")

Categories

Resources