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
Related
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.
Ok, first time poster. I teach in High School, first year on job no mentor to help, so if my code is garbage, be gentle.
Problem: : This program is to keep inventory for poptarts being sold. In the XML I have a linear layout, listview, and finally a linear layout. The listview goes through, uses a custom adapter to show the current pop-tarts and has an edit text to update the count. The last linear layout has edit texts to add a new poptart to the arraylist. After I added this code, whenever I select the edit text in the listview (which I forced to be an input_type:"number") the number pad pops up and then disappears to be replaced by the normal qwerty keyboard and the edit text box loses focus. If I jab the edit text box 3-4 times quickly it will keep the number keypad up and maintain focus. I just can't figure out why the focus and keyboard are being changed.
<LinearLayout
...
<TextView
...
<TextView
...
<TextView
...
<TextView
...
<TextView
...
</LinearLayout>
<ListView
android:layout_below="#+id/flavor"
android:id="#+id/simpleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="2dp"
android:layout_weight="1"
android:layout_gravity="top" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="0dp"
android:layout_weight="1">
<EditText
android:id="#+id/newFlavor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Flavor"
android:textSize="10pt"
/>
<CheckBox
android:id="#+id/isSeasonal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:checked="false"
/>
<EditText
android:id="#+id/newCurrent"
android:layout_width="0dp"
android:maxLength="3"
android:layout_height="wrap_content"
android:hint="Current"
android:textSize="10pt"
/>
<EditText
android:id="#+id/newMax"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLength="3"
android:hint="Max"
android:textSize="10pt"
/>
<Button
android:id="#+id/addNewFlavor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="10pt"
android:text="Add"
android:layout_weight="1" />
</LinearLayout>
Inside of list_view_inventory.xml:
...
<EditText
android:id="#+id/edit_text"
android:maxLength="3"
android:paddingLeft="25dp"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:paddingRight="30dp"
android:layout_weight="1" />
...
Finally I'll post MyAdapter:
public class MyAdapter extends ArrayAdapter<PopTart> {
ArrayList<PopTart> popTartList = new ArrayList<>();
Button buttonOne;
CheckBox seasonal;
public MyAdapter(Context context, int textViewResourceId, ArrayList<PopTart> tarts){
super(context, textViewResourceId, tarts);
popTartList = tarts;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view_inventory, null);
TextView nameView = (TextView) v.findViewById(R.id.name);
final TextView countView = (TextView) v.findViewById(R.id.count);
final EditText countUpdate = (EditText) v.findViewById(R.id.edit_text);
CheckBox seasonal = (CheckBox) v.findViewById(R.id.seasonal);
//Tried to force the input type again here programmatically
countUpdate.setInputType(InputType.TYPE_CLASS_NUMBER);
nameView.setText(popTartList.get(position).getName());
countView.setText(String.valueOf(popTartList.get(position).getCount()));
seasonal.setChecked(popTartList.get(position).getmSeasonal());
seasonal.setEnabled(false);
buttonOne = (Button) v.findViewById(R.id.increment);
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (countUpdate.getText().toString().trim().length() == 0)
{
//Making sure the EditText isn't empty
}
else {
String temp2 = countUpdate.getText().toString();
PopTart temp = popTartList.get(position);
temp.setCount(Integer.valueOf(temp2));
popTartList.set(position, temp);
countView.setText(String.valueOf(popTartList.get(position).getCount()));
countUpdate.setText(""); }
}
});
return v;
}
}
Thanks for all you do, Stack Overflow Community! You have taught me so much already!
In Android 7, I did not see this problem but I also having this problem recently at older version of Android.
I solved this problem by adding the following three.
1: Set android:descendantFocusability="afterDescendants" on the listView.
FOCUS_AFTER_DESCENDANTS
added in API level 1
int FOCUS_AFTER_DESCENDANTS
This view will get focus only if none of its descendants want it.
2: Add the following to your EditText
android:focusable="true"
android:focusableInTouchMode="true"
3: Add android:windowSoftInputMode="adjustResize" to activity of your manifest file. So your list can be scrollable even when keyboard is appeared.
PS: If someone facing the same problem, I hope this can help to them.
I think your list_view_inventory.xml should look like as follows, just added android:inputType="number" in your EditText
...
<EditText
android:id="#+id/edit_text"
android:maxLength="3"
android:paddingLeft="25dp"
android:layout_width="75dp"
android:inputType="number"
android:layout_height="wrap_content"
android:paddingRight="30dp"
android:layout_weight="1" >
<requestFocus />
</EditText>
...
Or request focus programmatically.
edittext.requestFocus();
you can even remove code which sets input-type to EditText in getView() method of your adapter.
Quoting Android docs
whenever the ListView needs to display a particular row of data, it requests the associated adapter to provide the view corresponding to that the data at that position through getView() method. This may occur whenever the view needs to be updated on screen (eg. during creation/scroll etc.).
so inputType= "number" is specified whenever getView() is called, and then it falls back to default inputtype and popsup qwerty keyboard.
I think it will solve your problem.
EDIT
So you can use following
android:focusableInTouchMode="true"
To get desired result, remove previous <requestFocus/>
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());
}
});
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.
I have an activity where the user is presented with a list of edit texts. The problem is, as soon as the activity starts, the virtual keyboard is brought up right away for the first edit text. I don't want this to happen as there is a spinner before this edit text, and often I've found that popping up the keyboard like this makes the user forget about that previous spinner.
Here's the relevant code:
<Spinner
android:id="#+id/event_location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30px"
android:prompt="#string/location_prompt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event Name: " />
<EditText
android:id="#+id/event_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity Type: " />
<Spinner
android:id="#+id/event_type"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address: " />
<EditText
android:id="#+id/event_address"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Summary: activity with an EditText starts, and the virtual keyboard automatically pops up. I don't want it to do this unless the user presses on the edit text.
Thanks for any help.
The EditText is automatically set to focus so you need to put android:focusable="false" or editText.setFocusable(false) to prevent the EditText to be focused while the spinner is visible. You could also try editText.setSelected(false).
Try changing your spinner XML to this:
<Spinner
android:layout_marginTop="30px"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/event_location"
android:prompt="#string/location_prompt">
<requestFocus />
</Spinner>
Also try to do for each EditText:
android:focusable="false"
android:focusableInTouchMode="false"
Look at this post: Stop EditText from gaining focus at Activity startup
This is how it works for me:
to make sure the edit text wont get focus on startup:
In xml set the focusable attribute of the EditText to false:
android:focusable="false"
2. in order to make the EditText focusable again after startup:
set the focusable attribute from code in the oncreate() method of the wanted activity after the setContent() method:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main_view);
....
EditText et = (EditText) findViewById(R.id.et_notes);
et.setFocusable(true);
et.setFocusableInTouchMode(true);
...
}