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();
}
}
});
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 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 am trying to set transparency to hint colour of floating edittext by setting its alpha value to 0.3.But its not working.I don't see any alpha change in hint colour of editext.
Below is the code
<android.support.design.widget.TextInputLayout
android:id="#+id/input_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/mobile_email"
android:layout_marginLeft="#dimen/dp15"
android:layout_marginRight="#dimen/dp15">
<EditText
android:id="#+id/number"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textColor="#color/numxtcolor"
android:alpha="0.3"
android:hint="#string/mobilehint"
android:textSize="#dimen/sp16"
android:inputType="number"/>
</android.support.design.widget.TextInputLayout>
mobilenumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mobilenumber.setAlpha(1);
} else {
if (TextUtils.isEmpty(mobilenumber.getText().toString())) {
mobilenumber.setAlpha(0.3f);
}
}
mobilenumber.invalidate();
}
});
I think the problem is that you are calling:
mobilenumber.setAlpha(0.3f);
This way, you are setting alpha for the whole view.. And not for the text.
Maybe, you may change your code as follows:
mobilenumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// Fully opaque WHITE color
v.setTextColor(Color.parseColor("#FFFFFFFF"));
} else {
// 70% transparent color
if (TextUtils.isEmpty(mobilenumber.getText().toString())) {
v.setTextColor(Color.parseColor("#55FFFFFF"));
}
}
v.invalidate();
}
});
This question is a follow up to Show android SearchView EditText by default. My SearchView used to work fine until I added android:iconifiedByDefault="false" to the layout. Now, when I click on the icon or type in the search field, no search takes place. Does anyone know how to fix this problem? here is my SearchView as it exists now.
<SearchView
android:id="#+id/search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:queryHint="#string/search_hint"
android:iconifiedByDefault="false"
android:textSize="16sp" />
I already tried the following:
mSearch.setOnSearchClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
getActivity().onSearchRequested();
//other stuff happen here
}
});
I solved the problem with
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) { ...}
}
i solve this problem by simply adding requestFocus tag in SearchView widget.
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/search"
android:clickable="true"
android:queryHint="Enter Song name.."
android:iconifiedByDefault="false">
<requestFocus/>
</SearchView>
I'm working on an Android app and I've got 2 editviews and a label. The user can enter 2 values and the label shows some calculation using input from the editviews. What I want is the following;
user enters either value with soft-keyboard
user presses "Return" softkey
editview should lose focus
the soft-keyboard should disappear
textview label should be recalculated
Now, the v.clearFocus only seems to works when there is another widget that can can get focus(?), so I've also added a dummie zero-pixel layout that can 'steal' the focus from the first editview. The Return key works now, but when the user switches focus from edit1 to edit2 by simply tapping then HideKeyboard() crashes. I've tried checking if inputMethodManager==null but that didn't help.
This all feels like I'm hacking to trick Android into doing some common UI behaviour, so I can't help but think that I'm overlooking something here.
Any help would be greatly appreciated. Btw I know this is similar to this question: How to lose the focus of a edittext when "done" button in the soft keyboard is pressed?
But I've tried that and it doesn't work.
So my layout xml is this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Dummy control item so that first textview can lose focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<EditText
android:id="#+id/editTest1"
android:layout_width="250px"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:imeOptions="actionDone" >
</EditText>
<EditText
android:id="#+id/editTest2"
android:layout_width="250px"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:imeOptions="actionDone" >
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test123" />
</LinearLayout>
And the source is this:
public class CalcActivity extends Activity implements OnFocusChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2_weight);
EditText testedit = (EditText) findViewById(R.id.editTest1);
testedit.setOnFocusChangeListener(this);
testedit.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//Clear focus here from edittext
Log.d("test app", "v.clearFocus only works when there are other controls that can get focus(?)");
v.clearFocus();
}
return false;
}
});
}
public void hideSoftKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
}
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) {
Log.d("unitconverter", "onFocusChange hasFocus == false");
// update textview label
TextView bla = (TextView) findViewById(R.id.textView1);
bla.setText(String.format("%s + %s", (((EditText) findViewById(R.id.editTest1)).getText()), (((EditText) findViewById(R.id.editTest2)).getText())));
// hide keyboard
hideSoftKeyboard();
}
}
}