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
}
});
Related
here' the code
I have two edit Text in a row in my lay out,and when I touch next on keyboard it does not change the focus.
the setOnEditorActionListener does not work till i add extra editText
<EditText
android:id="#+id/firstText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:imeOptions="actionNext"
android:inputType="numberDecimal"
/>
<EditText
android:id="#+id/secondText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
/>
code:
firstText.setOnNextActionListener(new CallBack<KeyEvent>() {
#Override
public void call(KeyEvent data) {
secondText.requestEditTextFocus();
}
});
Give one value into edittext in xml file ..
android:singleLine="true"
You can try these
android:nextFocusDown="#+id/secondText"
android:nextFocusLeft="#+id/secondText"
android:nextFocusRight="#+id/secondText"
android:nextFocusUp="#+id/secondText"
Show below three ways, you can got you want Using any one of them
inputType ="actionNext" attribute
or
android:imeOptions="actionNext"
or
android:singleLine="true" and
android:nextFocusDown="#+id/secondText"
on your .xml you can do this.
If you want to do it programmatically in your activity or fragment then try below:-
firstText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
if (actionId == EditorInfo.IME_ACTION_NEXT) {
secondText.requestEditTextFocus();
return true; // Focus will do whatever you put in the logic.
}
return false; // Focus will change according to the actionId
}
});
I have tested with set Programmtically with changes secondText.requestEditTextFocus(); to secondText.requestFocus(); and its working fine.
Please show below Screenshotes
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("");
}
}
});
Here is my XML
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/barcode"
android:id="#+id/barcode"
android:inputType="number" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/name"
android:id="#+id/name"
android:inputType="text" />
I set error to both fields:
((TextView) findViewById(R.id.barcode)).setError(getString(R.string.at_least_one_field));
((TextView) findViewById(R.id.name)).setError(getString(R.string.at_least_one_field));
When i run the app and change content in barcode field it's error popup is hiding.
And when i change content in name field it's error popup is not hiding. It's hiding only when i tap on Finish button on the keyboard.
Why number and text fields have different behaviour?
I had the same problem with a "text" inputType, and found an explanation in this post https://stackoverflow.com/a/23599214/3005955
textView.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable edt) {
if( playerName.getText().length() > 0) {
playerName.setError(null);
}
}
});
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();
}
}
}