I want to tell user about the pattern in which they should type location like below
So I have used AutocompleteTextView inside TextInputLayout like code below
<android.support.design.widget.TextInputLayout
android:id="#+id/profile_youraddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/profile_emailaddress"
android:layout_marginTop="#dimen/margin_10"
android:hint="#string/your_address"
android:textColor="#color/home_primary_color">
<AutoCompleteTextView
android:id="#+id/profile_addresstext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Malviya Nagar, New Delhi, Delhi"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
The problem is that when the TextInputLayout is not in focus it two hints overlap on each other like below
How can I override TextInputLayout's hint on the AutocompleteTextView's hint in non focusable state? Any help will be appreciated.
What I am looking For :
When not in focus, the hint should be "Your Address" ("Malviya Nagar, New Delhi, Delhi" should be hidden) and when in focus hint should be "Malviya Nagar, New Delhi, Delhi".
Do it using OnFocusChangeListener:
TextInputLayout text;
AutoCompleteTextView auto;
View.OnFocusChangeListener listener;
text = (TextInputLayout) findViewById(R.id.profile_youraddress);
auto = (AutoCompleteTextView) findViewById(R.id.profile_addresstext);
auto.setFocusable(true);
listener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
auto.setHint("Malviya Nagar, New Delhi, Delhi");
text.setHint("");
}else {
auto.setHint("");
text.setHint("Your address");
}
}
};
auto.setOnFocusChangeListener(listener);
Related
I am using the new TextInputLayout from material design library.
I only need to uppercase the Lebel(when positioned up), not the hint.
This is how should looks like
Here is my XML for my TextInputLayout and EditText
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/gap5"
android:hint="#string/user"
>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/input_user_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textWebEmailAddress"
android:autofillHints="emailAddress"/>
</com.google.android.material.textfield.TextInputLayout>
I don't think you can do this through the XML, I think you can kind of do it programmatically though.
Try something like this:
inputEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
inputLayout.setHint(getResources().getString(R.string.your_string_name).toUpperCase());
} else {
inputLayout.setHint(getResources().getString(R.string.your_string_name));
}
}
});
I am trying to do a validation + warning when a user leaves a field.
I am using textInputLayout + TextInputEditText and listening on onFocusChanged - pretty basic :-)
My problem is that the hint stays in the top when I use the onFocusChangeListener and I want it to act normal and go to the editText field.
Any ideas?
XML
<android.support.design.widget.TextInputLayout
android:id="#+id/usernameWrapper"
errorEnabled="true"
android:layout_marginBottom="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.design.widget.TextInputEditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:hint="#string/login_name_hint"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
Code
findViewById(R.id.username).setOnFocusChangeListener(new
View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
setUsernameError("Indtast e-mail (eller brugernavn)");
}
}
});
Edit:
To clarify: The problem occurs when I leave the EditText. Then I expect the hint to move from label to EditText. It does not. It stays in the label. This only occurs when I set an onFocusChangeListener on the EditText
Its behaving as expected and has no relation between setting the onFocusChangedListener
By default framework tries to give focus to the first focusable View from the top. Since the TextInputEditText is the only focusable view, it will be focused and your hint will stay in top when screen is launched. If you don't want this behavior, there are two options:
Make your parent view focusable by setting android:focusable and android:focusableInTouchMode as follows:
<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:focusable="true"
android:focusableInTouchMode="true">
<android.support.design.widget.TextInputLayout
android:id="#+id/usernameWrapper"
errorEnabled="true"
android:layout_marginBottom="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.design.widget.TextInputEditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:hint="#string/tv2login_name_hint"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Whenever you are done editing, call editText.clearFocus()
Don't use TextInputLayout but rather use simple EditText
I realise this is quite an old thread but I came up against this same issue today, resetting the hint animation fixed it for me;
findViewById(R.id.username).setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
findViewById(R.id.usernameWrapper).setHintAnimationEnabled(hasFocus);
if (!hasFocus) {
setUsernameError("Indtast e-mail (eller brugernavn)");
}
}
});
First you have to get the internal EditText with getEditText() method and then call the setOnFocusChangeListener.
The code would look like this:
private TextInputLayout textInputEmail = findViewById(R.id.text_input_email);
textInputEmail.getEditText().setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Toast.makeText(getApplicationContext(), "Got the focus", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Lost the focus", Toast.LENGTH_LONG).show();
}
}
});
First you have to get the internal EditText with getEditText() method and then call the setOnFocusChangeListener.
The code would look like this:
private TextInputLayout textInputEmail = findViewById(R.id.text_input_email);
textInputEmail.getEditText().setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Toast.makeText(getApplicationContext(), "Got the focus", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Lost the focus", Toast.LENGTH_LONG).show();
}
}
});
I have a EditText with longer multiple lines hint and when I focus on this EditText, hint disappears but lefts empty space around new text which I don't want. I have already tried to fix it with android:gravity="bottom", but empty space still remains.
Here is the code:
<EditText
android:id="#+id/addextra_basedon_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:hint="#string/placeholder_acting_on"
android:gravity="bottom" />
And pictures:
before
after
You can implement the behaviour of the EditText when focused like this:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
editText.setHint("");
editText.setText("");
}
}
});
I have three floating labels for three EditTexts that work fine on a UI screen.
The fourth EditText does not float the hint until after the listener launches and closes (dismisses) a dialogfragment. I am trying to have the hint float right after the EditText gains focus, and before the dialogfragment is launched, so the user can see the completed hint floated before getting additional information from the dialogfragment. The fourth EditText has the "hasFocus" code shown below whereas the first three EditTexts do not so something with the hasFocus is preventing the label from floating right away, although it eventually does after the diaglogfragment is closed. Please advise.
Activity.java
....
private ListenerEditText fListenerEditText;
fListenerEditText = (ListenerEditText) findViewById(R.id.FEditText);
fListenerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, final boolean hasFocus) {
if(hasFocus) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
}
}); ...
layout file.xml
...
<android.support.design.widget.TextInputLayout
android:id="#+id/DueDate_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="0">
<com.example.jdw.thirdscreen.ListenerEditText
android:id="#+id/FEditText"
android:hint="Due Date"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="fill_horizontal|start"
android:inputType="text|textCapSentences|textNoSuggestions"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:singleLine="true"
android:maxLength="51"
android:imeOptions="actionNext|flagNoExtractUi"
android:layout_marginBottom="8dp" >
</com.example.jdw.thirdscreen.ListenerEditText>
</android.support.design.widget.TextInputLayout>
I have two EditTexts for a username and password, each having the text "ENTER A USERNAME" and "ENTER A PASSWORD" respectively, as their default text.
Their text colors are a light gray. Now when they get focused they're supposed to change text colors as well as the password edittext's inputType to a password type (on top of other things but that works).
It successfully changes the text color to black at first but fails to change back and the inputType never changes.
Please suggest me where I am lacking, below is my code:
OnFocusChangeListener pwListener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
//if it has focus then clear the text and make text black
if(((EditText) v).getText().toString().compareTo(getString(R.string.passwordField)) == 0) {
((EditText) v).setTextColor(R.color.Black);
((EditText) v).setText(R.string.blank);
((EditText) v).setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
} else if(!hasFocus) {
//if it loses focus and nothing was inputed then change back to default
if(isEmpty(((EditText) v).getText().toString())) {
((EditText) v).setTextColor(R.color.TextInput);
((EditText) v).setText(R.string.passwordField);
((EditText) v).setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
}
}
}
};
Thats the passwords edit text and this is its XML:
<EditText
android:id="#+id/passwordInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_marginTop="5dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:paddingTop="15dip"
android:paddingBottom="15dip"
android:text="#string/passwordField"
android:textColor="#color/TextInput"
android:textSize="25sp"
android:gravity="center_vertical"
android:ems="10" >
</EditText>
Why you dont set the ENTER A USERNAME" and "ENTER A PASSWORD" as hint text in Edittext using
android:hint="Enter User Name"
It will automatically disappear when user clicked on Edittext.
In your Xml use Hint property to set values as default.
like:
android:hint="#string/username"
android:hint="#string/password"
Instead
android:text="#string/username"
android:text="#string/password"
Use
android:hint="#string/passwordField"
instead of
android:text="#string/passwordField"
and do same in username