Reputation: 403
I have an EditText, and whether the soft keyboard appears when a user clicks into it seems to vary, and I don't understand why.
Originally, my layout was as shown below, and the keyboard would appear as expected. The user would click on the EditText, the content would be highlighted, the keyboard would show, and some custom code would also run. However, since I'm trying to change the layout, and specifically changing the LinearLayout from horizontal to vertical orientation, this has stopped working. Now, when the user clicks into the EditText, the content is still highlighted but the keyboard isn't displaying.
Here's the XML where the code works. Simply changing the orientation to 'vertical' and modifying height/width and removing weight is enough to stop the keyboard from showing. I originally added focusable/focusableintouchmode to the parent RelativeLayout because I was having the problem of the EditText immediately gaining focus when the Activity started, and this solved the problem.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ocrmain_group_select_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true" android:focusableInTouchMode="true" >
<LinearLayout android:id="@+id/ocrmain_group_select_costholder"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<ImageView android:id="@+id/ocrmain_group_select_item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="15"
android:layout_gravity="left" />
<EditText android:id="@+id/ocrmain_group_select_cost"
android:focusable="true" android:focusableInTouchMode="true"
android:layout_weight="5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:selectAllOnFocus="true"
android:imeOptions="actionDone"
android:inputType="numberDecimal" />
</LinearLayout>
Essentially I'm trying to change it to this:
<LinearLayout android:id="@+id/ocrmain_group_select_costholder"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<ImageView android:id="@+id/ocrmain_group_select_item"
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText android:id="@+id/ocrmain_group_select_cost"
android:layout_gravity="end"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true" android:focusableInTouchMode="true"
android:selectAllOnFocus="true"
android:imeOptions="actionDone"
android:inputType="numberDecimal" />
</LinearLayout>
Here's some of the custom code I have for when the user clicks into it.
private class myCostBoxChangedListener implements EditText.OnEditorActionListener {
@SuppressLint("NewApi")
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//ScrollView sView = (ScrollView) ((RelativeLayout)v.getParent().getParent()).getChildAt(1);
//TableLayout tView = (TableLayout) sView.getChildAt(0);
if (actionId == EditorInfo.IME_ACTION_DONE) {
v.clearFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
calculateCheckedDialogCosts();
}
return false;
}
}
I also have an onFocusListener implemented, but it runs some code whose only role is to parse and potentially modify the contents of the EditText when the 'Done' button is pressed.
All help appreciated!
Upvotes: 0
Views: 474
Reputation: 403
I solved my own problem: I inserted the following code into my onFocusChangeList:
private class myCostBoxFocusListener implements View.OnFocusChangeListener {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);
Now, when the EditText is clicked on, it is both highlighted and the keyboard comes up for immediate editing. Sweet.
Upvotes: 1