Android issue with EditText object - android

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);
...
}

Related

TextView selection after EditText has been focused

I have a TextView which has text selectable.
<TextView
android:textIsSelectable="true"
android:layout_marginLeft="#dimen/reading_left_margin"
android:layout_marginRight="#dimen/reading_right_margin"
android:layout_marginTop="#dimen/reading_content_title_margin"
android:textColor="#color/colorText"
android:textSize="#dimen/reading_text_size"
android:lineSpacingMultiplier="1.8"
android:id="#+id/text_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
In the same fragment i have an EditText.
Once I focus into the editText, I am not able to select text on the TextView any more. However, if I minimize the app and reopen, I am able to again select the text on the TextView.
<EditText
android:imeOptions="actionDone"
android:layout_margin="5dp"
android:hint="#string/type_here"
android:textColorHighlight="#00000000"
android:id="#+id/input_comment"
android:textColor="#color/colorCommentsText"
android:padding="5dp"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
I have tried textview.invalidate(), textview.requestFocus() neither seems to work.
EDIT: Forgot to mention I have done edittext.clearFocus() before trying to select text from textView.

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 not accepting an input

I have 2 EditTexts on my screen. The first one works fine, but the second doesn't. I click on the first EditText and can enter text. But click on the second EditText and any input continues to appear in the first EditText.
Looking at other questions on the subject, there is talk of using setFocus and having onClick listeners for the EditText fields. Is this correct?
I have another app which had several screens with multiple EditTexts and never had to do this.
This is my current XML:
<TextView
android:id="#+id/namLabel"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name" />
<EditText
android:id="#+id/namTxt"
android:layout_below="#id/namLabel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:minWidth="200sp" />
<TextView
android:id="#+id/numLabel"
android:layout_alignParentLeft="true"
android:layout_below="#id/namTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20sp"
android:paddingRight="10sp"
android:text="#string/units" />
<EditText
android:id="#+id/numTxt"
android:layout_toRightOf="#id/numLabel"
android:layout_alignBaseline="#id/numLabel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:inputType="numberDecimal"
android:minWidth="50sp" />
This is all the relevant code I have in the onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_game);
namText = (EditText) findViewById(R.id.namTxt);
numText = (EditText) findViewById(R.id.numTxt);
Please don't tell me I have to add a whole load of onClickListeners for these fields?
I found the problem. I just had to add the setFocusableInTouchMode to both EditText's:
android:focusableInTouchMode="true"
I didn't do that in my last app, so no idea why I have to now.
You don't need to add any listeners or setFocus. The only "strange" thing I see is that you are not wrapping your xml in a RelativeLayout. It should work just fine, just tested it.

Can't focus EditText

I have a simple EditText inside my scrollview.
All the other elements are working just fine, however i won't get the usual EditText behaviour on click (Keyboard doesn't appear, I can't write).
Here's my xml for the field.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/enterWishprice"
android:background="#drawable/edit_back"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="0 F€"
android:gravity="center_vertical|center_horizontal"
android:textSize="26dp"
android:inputType="number"
android:layout_margin="10dp" />
As you can see I already tried setting focusable/inTouchMode.
I also tried setting a click listener on the field to manually open the keyboard which works just fine. However I still can't type anything into the field.
After that i tried using requestFocus in the clicklistener without any result.
Furthermore I tried clearFocus on the EditText as well as the surrounding ScrollView - also not working.
Hopefully you can give me a hint cause I'm out of ideas..
Thanks in advance!
The attribute Enabled must be set to true.
<EditText
android:id="#+id/room_message"
android:layout_width="164dp"
android:layout_height="46dp"
android:clickable="true"
android:ems="10"
android:enabled="true"
android:focusableInTouchMode="true"
android:hint="#string/prompt_message"
android:inputType="textPersonName"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="italic"
/>
But i have got another problem where the EditText won't lose focus....
try like this
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/enterWishprice"
android:background="#drawable/edit_back"
android:focusableInTouchMode="true"
android:text="0 F€"
android:gravity="center_vertical|center_horizontal"
android:textSize="26dp"
android:inputType="number"
android:layout_margin="10dp">
<requestFocus />
</EditText>

text keyboard appear even if I have set inputType to number edittext android

I want to display the number keyboard.
Here is my code to do that. (This is an item of my list).
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/checked_list_item_text"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/checked_list_item_quatity"
android:layout_centerVertical="true"
android:textSize="30sp" />
<EditText
android:id="#+id/checked_list_item_quatity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="7dp"
android:gravity="center_vertical"
android:hint="Quantité"
android:focusableInTouchMode="true"
android:inputType="number"
android:maxLength="6"
android:textSize="30sp" />
</RelativeLayout>
When I click on edittext, the keyboard displays the numbers but switches quickly to text and I have to click a second time to get the number keyboard.
This issue occurs only the first time when I open the activity.
Try changing the input mode in your AndroidManifest.xml
I had the same issue when trying to get focus to an edittext field thats inside a listview. Adding android:windowSoftInputMode="adjustPan" in the activity holding the listview solved the problem for me.
<activity android:name=".MyEditTextInListView"
android:label="#string/app_name"
android:windowSoftInputMode="adjustPan">
What you have should work , but it could be something on the code side. Maybe you can force it programmatically:
EditText checkedInput = (EditText) findViewById(R.id.checked_list_item_quatity);
checkedInput.setInputType(InputType.TYPE_CLASS_NUMBER);

Categories

Resources