Search Box Edit Text loses focus on text input - android

I have list view with a Text & Edit Text as list view item. User need to enter a numeric value corresponding to respective text. On top of list view we have search box to find a particular item in list view. On clicking search box(edit text) the keyboard opens but after entering first alphabet, the view gets distorted (the search box moves up in the screen) and no more input is accepted from keyboard. Below is code snippet :
Activity xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
tools:context="com.belinnov.product.tcbroms.activity.StockProductsActivity">
<ProgressBar
android:id="#+id/stock_layout_progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible" />
<RelativeLayout
android:id="#+id/stock_product_search_layout"
android:layout_width="match_parent"
android:layout_height="#dimen/search_bar_height">
<com.xxx.helper.EditTextBackEvent
android:id="#+id/stock_product_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/app_margin_small"
android:background="#drawable/editext_background"
android:textColor="#android:color/black"
android:imeOptions="actionSearch"
android:textSize="#dimen/app_font_medium"
android:focusable="true"
android:focusableInTouchMode="true"
android:paddingLeft="#dimen/app_padding_large">
</com.xxx.helper.EditTextBackEvent>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/searchsm"
android:layout_marginTop="#dimen/app_margin_regular"
android:layout_marginLeft="#dimen/app_margin_regular"/>
</RelativeLayout>
<ListView
android:id="#+id/stock_product_lv"
android:layout_below="#id/stock_product_search_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/grey"
android:descendantFocusability="beforeDescendants"
android:dividerHeight="#dimen/listview_divider_height">
</ListView>
</RelativeLayout>
The list view item xml :
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/stock_product_list_item"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:orientation="horizontal">
<LinearLayout
android:layout_below="#+id/stock_pr_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".70"
android:layout_marginTop="#dimen/app_padding_small"
android:paddingBottom="#dimen/app_padding_small"
android:orientation="vertical">
<TextView
android:id="#+id/stock_pr_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/app_font_medium"
android:textColor="#android:color/black"
android:text="aa"/>
<TextView
android:id="#+id/stock_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="#string/hello_world"
android:textColor="#android:color/black"
android:textStyle="normal"
android:textSize="#dimen/app_font_medium"/>
</LinearLayout>
<EditText
android:id="#+id/stock_product_qty_edt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".30"
android:inputType="numberDecimal"
android:padding="#dimen/app_margin_regular"
android:cursorVisible="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/app_padding_small"
android:paddingBottom="#dimen/app_padding_small"
android:background="#drawable/editext_background">
<requestFocus/>
</EditText>
</LinearLayout>
We have specified android:inputType="numberDecimal" for list view edit text. Due to this on clicking the edit text the normal keyboard open for a fraction of second and then numeric keyboard appears. Also nothing could be keyed in search box. Changed the code to request focus on search box as below :
searchEditText = (EditTextBackEvent) findViewById(R.id.stock_product_search);
searchEditText.setFocusable(false);
searchEditText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
searchEditText.setFocusableInTouchMode(true);
return false;
}
});
With above change the regular soft keyboard appears on search box click. But as soon as first alphabet is entered, results are filtered and whole view gets distorted (moves up, hiding the search box). On pressing back the view appears to normalcy and then on another alphabet input, same behavior. Have tried few other things too like android:descendantFocusability but none of them seem to be working. Struggling for quite some time now. Please advise if I am missing something.

Related

Scrollview does not shrink when soft keyboard is shown

I have a simple chat activity, with a message entry box at the top and then a scrollview with the message list. When the soft keyboard is opened, I want the scrollview to shrink so that the last message is not covered by the keyboard. This is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp" >
<TextView
android:id="#+id/chat_with"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Chat with Gordon" />
<RelativeLayout
android:id="#+id/msg_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/chat_with"
android:layout_marginTop="10dp">
<EditText
android:id="#+id/chat_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Type your message" />
<ImageButton
android:id="#+id/chat_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#00000000"
android:src="#+android:drawable/ic_menu_send" />
</RelativeLayout>
<ScrollView
android:id="#+id/chat_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/msg_container"
android:layout_marginTop="15dp"
android:padding="10dp"
android:isScrollContainer="true"
>
<RelativeLayout
android:id="#+id/chat_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/msg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi, how are you?"
android:layout_alignParentRight="true"/>
<TextView
android:id="#+id/msg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hey! All right"
android:layout_alignParentLeft="true"
android:layout_below="#+id/msg1"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I just manually inserted two messages but there will be many of them in the real app. I already tried with the adjustPan|adjustResize solution, but it does not work, when I fire the keyboard the layout is unchanged and the last messages are covered by the keyboard. How can I avoid this?
Thanks for your help!
I solved this problem by using the manifest. In the activity I used:
android:windowSoftInputMode="stateHidden|adjustResize"
This way the keyboard starts out hidden and then when the user clicks in the EditText the keyboard will show up and the rest of the screen gets resized.
Look into the windowSoftInputMode for other settings for keyboard behavior (e.g. just covering up screen instead of resizing).
Edit:
To scroll the scroll view to the bottom after the keyboard shows up I needed to use a slight delay because sometimes the scroll would happen first and then the keyboard showed up and the scroll view was no longer at the bottom. I ended up not keeping this for other reasons, but to delay the scroll a bit one can do this:
// now scroll to the bottom
ScrollView sv = (ScrollView) findViewById(R.id.myScrollView);
sv.post(new Runnable() {
#Override
public void run() {
ScrollView sv = (ScrollView) findViewById(R.id.myScrollView);
sv.scrollTo(0, sv.getBottom());
}
});

Issues focusing EditTexts in a ListView (Android)

I have a ListView with a few EditTexts in each item. I have no issues with a hardware keyboard, but things freak out a bit with the soft keyboard. I have two issues.
When I first click on an EditText, it briefly appears to have focus but then loses focus once the keyboard has shown. I must then click the EditText again to get focus.
When an EditText with focus is scrolled out of view, focus goes to... well... see the screenshot. I'm not sure what's happening.
More details on #1:
When the screen first loads, focus is in the "Gr High School Scale" field, but the keyboard is not shown.
If I immediately click on a desired EditText, its OnFocusChangeListener tells me that it receives focus and then loses focus. Visually, I see the cursor appear in the field, but when the keyboard loads the cursor jumps away (like in the screenshot) and I don't know where focus has gone.
I've played around a bit the Focusable and DescendantFocusability attributes of the ListView, but to no avail. Any advice?
Each time an EditText with focus gets scrolled out of view, another drunk cursor shows up:
UPDATE: Relevant code.
Activity layout with ListView XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/com.NsouthProductions.gradetrackerpro"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.NsouthProductions.gradetrackerpro.Activity_EditCourseGPA" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="Define the GPA and Percent scale for this course." />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginLeft="5dp" >
<LinearLayout
android:id="#+id/linlay_rad_group_existing_scale"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioButton
android:id="#+id/radio0_existing_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:checked="true"
android:text="Existing Scale" />
<Spinner
android:id="#+id/spinner_existing_scales"
android:layout_width="wrap_content"
android:layout_height="33dp"
android:layout_gravity="right"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linlay_rad_group_new_scale"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio1_new_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="New Scale" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:text=" " />
<EditText
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPersonName"
android:maxLines="1"
android:scrollHorizontally="true"
android:text="Gr High School Scale" >
<requestFocus />
</EditText>
</LinearLayout>
</RadioGroup>
<LinearLayout
android:id="#+id/linlay_scale_save_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<Button
android:id="#+id/btn_gpa_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/btn_gpa_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<ListView
android:id="#+id/listview_scale"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/linlay_scale_save_buttons"
android:layout_alignParentLeft="true"
android:layout_below="#id/radioGroup1"
android:focusable="true"
android:focusableInTouchMode="true"
android:headerDividersEnabled="true"
android:scrollingCache="true" >
</ListView>
List Items XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linlay_scale_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/checkbox_scale_item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A-" />
<EditText
android:id="#+id/et_gpa"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="true"
android:inputType="numberDecimal"
android:maxLength="6"
android:text="4.000" />
<EditText
android:id="#+id/et_min"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="true"
android:inputType="numberDecimal"
android:maxLength="6"
android:text="94.75" />
<EditText
android:id="#+id/et_max"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:maxLength="6"
android:text="100.0" />
Setting the view in my adapter:
View v = convertView;
ViewHolder holder;
v = inflater.inflate(R.layout.scale_list_item,
holder = new ViewHolder();
holder.cb = (CheckBox) v.findViewById(R.id.checkbox_scale_item);
holder.et_gpa = (EditText) v.findViewById(R.id.et_gpa);
holder.et_min = (EditText) v.findViewById(R.id.et_min);
holder.et_max = (EditText) v.findViewById(R.id.et_max);
I'm not sure what else to post. I have focus and textChange listeners, but the problem exists even if those are commented out. Let me know if anything else is needed. Thank you.
More detail about how focus is behaving when the EditText is touched:
The EditText is clicked (touched)
EditText Receives focus
EditText loses focus
ListView gains focus (and tries to set child focus view requestChildFocus... doesn't seem to succeed).
ListView loses focus
ListView gains focus (and tries to set child focus again).
The above is based on having listeners for both the EditText and the ListView. Note: with a hardware keyboard, the EditText gets focus and that's that. I think the soft keyboard appearing affects the focus.
FINAL UPDATE: In the end, working with the ListView was too difficult, especially because I wanted to update multiple rows based on changes to an EditText in one row. That's hard to do when the keyboard is up and the ListView only considers a few of the rows to exist at any one time.
I instead made nested LinearLayouts. Each horizontal layout was a "row" and I put them inside of an ArrayList in order to manage them and it was a piece of cake, comparatively (still not easy).
You are facing ListView recycling issue. When you scroll up or down or when keyboard appears ListView again refreshes and lost your EditText's focus. So, first of all read this answer to understand ListView Recycling mechanism and then follow the suggestion from my side the solution of your current scenario in my mind.
I suggest you should use a Button on ListView Item and text of this button should be generic like Enter Student Info or what ever you'd like. After clicking on this Button open AlertDialog and set your xml view (currently your all edittexts like et_gpa, et_min, et_max etc on listview items)
For Example:
btnInfo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showStudentInfoAlert();
}
});
public void showStudentInfoAlert()
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater in = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = in.inflate(R.layout.your_xml_having_edittexts, null);
EditText et_gpa = (EditText) v.findViewById(R.id.et_gpa);
//add all edit texts like this and after that just set view on alert dialog
builder.setTitle("Enter student's info");
builder.setView(v);
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//save all edittexts values and do what erver you want with those values
}
});
dialog.show();
}
first of all when the screen loads,, focus is on the "Gr High School Scale" field, because of this <requestFocus /> in your xml. but this does no gurantee that the keyboard might show.. to make the keyboard to show use getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
also i am thinking that it looses focus immediately because the listview has taken focus away from the edittext
<ListView
android:id="#+id/listview_scale"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/linlay_scale_save_buttons"
android:layout_alignParentLeft="true"
android:layout_below="#id/radioGroup1"
android:focusable="true" //this
android:focusableInTouchMode="true" // and that
android:headerDividersEnabled="true"
android:scrollingCache="true" >
, and since none of your listview edittext is asking for focus,, the focus stays on just the listview not the edittext..so you need to re-surface the focus back to the edittext..also from the android documentation it states By default the user can not move focus to a view;.. and also it states that "if focusableInTouchMode is true for a view, that view can gain focus when clicked on, and can keep focus if another view is clicked on that doesn't have this attribute set to true. "
mostly when you first create an edittext it automatically adds <requestFocus /> to it specifying that it needs to take focus, or it can take focus..but in your case and with this documentaion the edittext will never keep focus because listview is taking it away from him.. so in short try this on all views(including your edittext) you want to take focus like you did on the listview
android:focusable="true"
android:focusableInTouchMode="true"
so try that on the edittext and let me know if it helps..thanks for waiting

EditText doesn't show when I have focus in another

I have a layout where i have one image view, two edittext and one button,
when i clicked on the first edittext, i can't scroll to the second to fill it too, because of the virtual keyboard, here is my code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:src="#drawable/logo"
android:contentDescription="#string/invokeme" />
<EditText
android:id="#+id/login"
android:layout_height="wrap_content"
android:layout_width="235dip"
android:ems="10"
android:layout_marginTop="25dp"
android:layout_below="#id/logo"
android:hint="#string/login" />
<EditText
android:id="#+id/password"
android:inputType="textPassword"
android:layout_height="wrap_content"
android:layout_width="235dip"
android:ems="10"
android:layout_marginTop="5dp"
android:layout_below="#id/login"
android:hint="#string/password" />
<Button
android:id="#+id/submitLogin"
android:layout_width="235dip"
android:layout_height="wrap_content"
android:ems="12"
android:text="#string/login"
android:layout_below="#id/password"/>
</RelativeLayout>
There might be more than one way around. First one may be that you can keep everything ( the whole relative layout) inside a ScrollView. Second way can be that you may establish a touch listener on your view, excluding the two EditText's. When anything other than those two EditText is touched, you can hide the keyboard from the screen. See How to hide soft keyboard on android after clicking outside EditText? for more. Hope it helps.

When i focus on edit text with numeric input type, standart qwerty keyboard pops up

I have set my EditText field like this:
<EditText
android:inputType="number"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:id="#+id/editText" android:layout_gravity="center"
/>
When I click on that Edittext, correct numeric input keyboard shows up, but then immediately is replaced by the standard keyboard. If I click again, numeric keyboard appears and is not replaced by the standard keyboard. This happens in dynamically generated ListView, if that affects anything, and happens in both the emulator and the phone (samsung galaxy note). How can I prevent the standard keyboard from appearing?
Here is the actual code:
public class Delivery extends Activity {
ListView deliveryTipes;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.delivery_main);
deliveryTipes=(ListView) findViewById(R.id.tasksID);
String[] deliveryTipesList = getResources().getStringArray(R.array.tasks);
deliveryTipes.setAdapter(new ArrayAdapter<String>(this, R.layout.delivery_line,R.id.taskName, deliveryTipesList));
}
}
delivery_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tasksID" android:layout_gravity="center" android:padding="10dp"/>
</LinearLayout>
delivery_line.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingRight="10dp">
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="Task Name"
android:id="#+id/taskName" android:layout_gravity="left|center_vertical" android:textStyle="bold"/>
<Space
android:layout_width="50dp"
android:layout_height="fill_parent"
android:id="#+id/space"/>
<EditText
android:inputType="number"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:id="#+id/editText" android:layout_gravity="center"
/>
</LinearLayout>
This kind of problems are very annoying. The short story is that by default a ListView is meant to simply show items, and optionally select them.
ListView doesn't assume its children to be focusable, it handles the focus by itself and triggers the appropriate listeners. When you start putting focusable widgets in a ListView, things soon get wild. Post the actual code, and you'll have more chances to get help.

Adding an EditText to jfeinstein sliding menu

I am working on an android project and I am making use of jfeinstein sliding menu library, library can be located at https://github.com/jfeinstein10/slidingmenu.
I have got the library working fine and have amended my code to make it work with other components, not just TextViews, such as Switches. I am now trying to add an EditText to the menu so that the user can enter directly in the box without having to switch activities.
I have it so I have a called MenuAdapter which extends ArrayAdapter. This then has a series of constructors which, based on the constructor and what values are passed into, determine which control is added to the sliding menu. The EditText is being added to the sliding menu and the hint is being sent, and when I tap on the edit text the keyboard is displayed but if I type anything, its not shown in the EditText. However, oddly, if I long press on the edit text and click paste, I can view the content that is pasted so its just typing that's not working.
Below is the XML layout, each component is hidden using the visibility set to gone and then is set to be displayed dependent on the constructor that was used.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/row_icon"
android:paddingLeft="25dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="10dp"
android:visibility="gone" />
<TextView
android:id="#+id/row_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="10dp"
android:text="Medium Text"
android:textAppearance="#android:style/TextAppearance.Medium"
android:visibility="gone"
android:textColor="#color/White" />
<Switch
android:id="#+id/row_switch"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="10dp"
android:visibility="gone"
android:textColor="#color/White"/>
<EditText
android:id="#+id/row_edittext"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="10dp"
android:visibility="gone"
android:textColor="#android:color/white" />
</LinearLayout>
Below is the code for how the EditText is being addd to the menu.
case EDITEXT:
EditText editText = (EditText)convertView.findViewById(getItem(position).viewID);
editText.setHint(getItem(position).hint);
editText.setEnabled(true);
editText.setVisibility(View.VISIBLE);
break;
Thanks for any help you can provide.

Categories

Resources