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("");
}
}
});
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 ListView contains multi EditText. Sometimes, when I scroll and focus on EditText, the cursor disappears although the keyboard is showing and I can input the character normally. The cursor just appears when I put the first character.
<EditText
android:id="#+id/et_nutri_lo"
android:background="#drawable/edit_text_corner"
android:paddingRight="#dimen/et_corner_radius"
style="#android:style/TextAppearance.Medium"
android:selectAllOnFocus="true"
android:layout_width="wrap_content"
android:layout_height="#dimen/btn_small_h"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:minEms="3"
android:maxEms="6"
android:gravity="right|bottom"
android:inputType="numberDecimal"
android:filterTouchesWhenObscured="false"
/>
Do you know what's wrong?
Thanks
Have you by any chance set an OnFocusChangeListener? If yes, try the following:
view.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
[YOUR CODE HERE]
view.dispatchWindowFocusChanged(hasFocus); // Fix for text selection handle not disappearing
}
});
I have a small function to open the soft keyboard when a EditText is programmatically focused as shown here...
public void getUserName() {
EditText tv = (EditText)findViewById(R.id.user_info_name);
tv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
showDialog("Focused!");
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
}
});
tv.selectAll();
tv.requestFocus();
}
However, the soft keyboard doesn't appear automatically but the dialog DOES show stating focused. In order to get the keyboard to appear I have to click inside the EditText.
My XML is as follows...
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#id/user_info_name"
android:editable="true"
android:hint="#string/user_info_name"
android:inputType="textCapWords|textPersonName"
android:textColor="#color/blue_gray"
android:maxLength="50"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:enabled="true"
android:focusable="true"
android:focusableInTouchMode="true" />
Can someone please advise why it's not working or what I am missing / failing to address.
Thanks in advance as always.
SOLVED: The following change to the function fixed the issue...
public void getUserName() {
EditText tv = (EditText)findViewById(R.id.user_info_name);
tv.selectAll();
tv.requestFocus();
InputMethodManager imm = (InputMethodManager)this.getSystemService(this.INPUT_METHOD_SERVICE);
imm.showSoftInput(tv,InputMethodManager.SHOW_IMPLICIT);
}
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();
}
}
}