Stop EditText from gaining focus at Activity startup
I have an Activity
in Android, with two elements:
EditText
ListView
When my Activity
starts, the EditText
immediately has input focus (flashing cursor). I don't want any control to have input focus at startup. I tried:
EditText.setSelected(false);
No luck. How can I convince the EditText
to not select itself when the Activity
starts?
android listview android-edittext
|
show 3 more comments
I have an Activity
in Android, with two elements:
EditText
ListView
When my Activity
starts, the EditText
immediately has input focus (flashing cursor). I don't want any control to have input focus at startup. I tried:
EditText.setSelected(false);
No luck. How can I convince the EditText
to not select itself when the Activity
starts?
android listview android-edittext
112
Before you use any of the upvoted hacks below, check if you don't have the<requestFocus />
line in your XML layout (more details in @floydaddict's answer). Eclipse's WYSIWYG editor adds it automatically. After removing this line, the problem disappeared for me.
– DzinX
Jun 15 '12 at 10:31
removing just<requestFocus />
did not work on Asus Nexus 7 with KitKat 4.4.4 but @Joe's answer works
– user13267
Jan 29 '15 at 8:33
87
I just use android:focusableInTouchMode="true" on my parent layout and works for me.
– Herman
Feb 18 '15 at 16:50
The only one that worked for me was rallat's solution
– vivoconunxino
Sep 26 '15 at 18:48
@Herman your simple solution worked for me. I was using multiple widgets including radio buttons, check boxes, buttons and EditText. Setting thefocusableInTouchMode="true"
attribute for the parent view (in my case a LinearLayout) resolved the issue. Thank you.
– prasad_
Jan 5 '18 at 5:23
|
show 3 more comments
I have an Activity
in Android, with two elements:
EditText
ListView
When my Activity
starts, the EditText
immediately has input focus (flashing cursor). I don't want any control to have input focus at startup. I tried:
EditText.setSelected(false);
No luck. How can I convince the EditText
to not select itself when the Activity
starts?
android listview android-edittext
I have an Activity
in Android, with two elements:
EditText
ListView
When my Activity
starts, the EditText
immediately has input focus (flashing cursor). I don't want any control to have input focus at startup. I tried:
EditText.setSelected(false);
No luck. How can I convince the EditText
to not select itself when the Activity
starts?
android listview android-edittext
android listview android-edittext
edited Dec 25 '17 at 9:49
Pankaj Lilan
2,18911634
2,18911634
asked Oct 12 '09 at 15:03
MarkMark
16.6k143445
16.6k143445
112
Before you use any of the upvoted hacks below, check if you don't have the<requestFocus />
line in your XML layout (more details in @floydaddict's answer). Eclipse's WYSIWYG editor adds it automatically. After removing this line, the problem disappeared for me.
– DzinX
Jun 15 '12 at 10:31
removing just<requestFocus />
did not work on Asus Nexus 7 with KitKat 4.4.4 but @Joe's answer works
– user13267
Jan 29 '15 at 8:33
87
I just use android:focusableInTouchMode="true" on my parent layout and works for me.
– Herman
Feb 18 '15 at 16:50
The only one that worked for me was rallat's solution
– vivoconunxino
Sep 26 '15 at 18:48
@Herman your simple solution worked for me. I was using multiple widgets including radio buttons, check boxes, buttons and EditText. Setting thefocusableInTouchMode="true"
attribute for the parent view (in my case a LinearLayout) resolved the issue. Thank you.
– prasad_
Jan 5 '18 at 5:23
|
show 3 more comments
112
Before you use any of the upvoted hacks below, check if you don't have the<requestFocus />
line in your XML layout (more details in @floydaddict's answer). Eclipse's WYSIWYG editor adds it automatically. After removing this line, the problem disappeared for me.
– DzinX
Jun 15 '12 at 10:31
removing just<requestFocus />
did not work on Asus Nexus 7 with KitKat 4.4.4 but @Joe's answer works
– user13267
Jan 29 '15 at 8:33
87
I just use android:focusableInTouchMode="true" on my parent layout and works for me.
– Herman
Feb 18 '15 at 16:50
The only one that worked for me was rallat's solution
– vivoconunxino
Sep 26 '15 at 18:48
@Herman your simple solution worked for me. I was using multiple widgets including radio buttons, check boxes, buttons and EditText. Setting thefocusableInTouchMode="true"
attribute for the parent view (in my case a LinearLayout) resolved the issue. Thank you.
– prasad_
Jan 5 '18 at 5:23
112
112
Before you use any of the upvoted hacks below, check if you don't have the
<requestFocus />
line in your XML layout (more details in @floydaddict's answer). Eclipse's WYSIWYG editor adds it automatically. After removing this line, the problem disappeared for me.– DzinX
Jun 15 '12 at 10:31
Before you use any of the upvoted hacks below, check if you don't have the
<requestFocus />
line in your XML layout (more details in @floydaddict's answer). Eclipse's WYSIWYG editor adds it automatically. After removing this line, the problem disappeared for me.– DzinX
Jun 15 '12 at 10:31
removing just
<requestFocus />
did not work on Asus Nexus 7 with KitKat 4.4.4 but @Joe's answer works– user13267
Jan 29 '15 at 8:33
removing just
<requestFocus />
did not work on Asus Nexus 7 with KitKat 4.4.4 but @Joe's answer works– user13267
Jan 29 '15 at 8:33
87
87
I just use android:focusableInTouchMode="true" on my parent layout and works for me.
– Herman
Feb 18 '15 at 16:50
I just use android:focusableInTouchMode="true" on my parent layout and works for me.
– Herman
Feb 18 '15 at 16:50
The only one that worked for me was rallat's solution
– vivoconunxino
Sep 26 '15 at 18:48
The only one that worked for me was rallat's solution
– vivoconunxino
Sep 26 '15 at 18:48
@Herman your simple solution worked for me. I was using multiple widgets including radio buttons, check boxes, buttons and EditText. Setting the
focusableInTouchMode="true"
attribute for the parent view (in my case a LinearLayout) resolved the issue. Thank you.– prasad_
Jan 5 '18 at 5:23
@Herman your simple solution worked for me. I was using multiple widgets including radio buttons, check boxes, buttons and EditText. Setting the
focusableInTouchMode="true"
attribute for the parent view (in my case a LinearLayout) resolved the issue. Thank you.– prasad_
Jan 5 '18 at 5:23
|
show 3 more comments
48 Answers
48
active
oldest
votes
1 2
next
Excellent answers from Luc and Mark however a good code sample is missing. Adding the tag android:focusableInTouchMode="true"
to <LinearLayout>
like the following example will fix the problem.
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>
647
What about setting the parent layout toandroid:focusableInTouchMode="true"
!
– Muhammad Babar
Mar 21 '14 at 5:58
22
Perfect! Thanks... One thing to note is that the dummy item has to be PLACED RIGHT BEFORE the focusable element!!! I had a TextView between my dummy and the EditText and the method did not work!
– maddob
Jul 10 '14 at 9:12
23
setting android:focusableInTouchMode="true" to parent layout was the good approach thanks @Muhammad Babar
– sujith s
Mar 24 '15 at 10:15
19
can someone explain why this works?
– Pietro Rea
Apr 23 '15 at 23:10
13
@PietroRea Android has annoying auto-focus behaviors that are built into the core behavior of its app structure. If you tell it that an edit text is releasing or losing focus, it automatically makes decision on where the focus goes. Without a new object for the focus to transfer to, it picks the first focusable element in the layout even when that element is the one that just cleared its focus! It's kinda nuts and frustrating, but maybe there's a valid reason for this type of behavior.
– Weston Wedding
Mar 8 '16 at 16:47
|
show 13 more comments
Is the actual problem that you just don't want it to have focus at all? Or you don't want it to show the virtual keyboard as a result of focusing the EditText
? I don't really see an issue with the EditText
having focus on start, but it's definitely a problem to have the softInput window open when the user did not explicitly request to focus on the EditText
(and open the keyboard as a result).
If it's the problem of the virtual keyboard, see the AndroidManifest.xml
<activity> element documentation.
android:windowSoftInputMode="stateHidden"
- always hide it when entering the activity.
or android:windowSoftInputMode="stateUnchanged"
- don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).
22
yet still the cursor is in the first EditText in the layout - even though the keyboard is not shown
– martyglaubitz
May 20 '14 at 8:17
36
@Anderson: Nothing about the answer implied that it would prevent theEditText
from obtaining focus. It actually clearly states that this is how you prevent the software keyboard IME from opening automatically on focus; because it is more likely that the bigger concern is the soft keyboard popping up unexpectedly, not the focus itself. If your issue is with theEditText
actually having focus at all, then use someone else's answer.
– Joe
Jul 8 '14 at 23:38
1
set it programmatically: stackoverflow.com/questions/6138330/…
– Nick Franceschina
Oct 8 '14 at 18:26
3
Don't do this on an activity with a camera -- it breaks the timing of the flash (don't ask me why)
– Daniel Wilson
Jun 16 '15 at 11:54
One reason to not have focus at startup is that if the imeOptions are changed after the view has focus and the keyboard shows, the imeOptions will be those set before the change.
– n_b
Apr 27 '16 at 21:57
|
show 3 more comments
A simpler solution exists. Set these attributes in your parent layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
And now, when the activity starts this main layout will get focus by default.
Also, we can remove focus from child views at runtime (e.g., after finishing child editing) by giving the focus to the main layout again, like this:
findViewById(R.id.mainLayout).requestFocus();
Good comment from Guillaume Perrot:
android:descendantFocusability="beforeDescendants"
seems to be the
default (integer value is 0). It works just by adding
android:focusableInTouchMode="true"
.
Really, we can see that the beforeDescendants
set as default in the ViewGroup.initViewGroup()
method (Android 2.2.2). But not equal to 0. ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;
Thanks to Guillaume.
1
It's important to set these attributes to the direct parent of the view which gains focus on activity creation, otherwise it won't work.
– tomrozb
Feb 9 '15 at 14:10
29
Note this doesn't seem to work if the parent is a ScrollView
– Daniel Wilson
Jun 16 '15 at 13:52
4
Just a perfect solution. Works for both: 1.Disabling autofocus, and 2. Disable auto-opening of soft keyboard
– Meet
Mar 14 '16 at 10:38
Nice. It does not need to be a direct parent. I set it at root level of layout and it seems work fine.
– Kurotsuki
Jan 21 '17 at 17:58
@DanielWilson when would you want a scrollview as direct parent of an edittext?
– Mark Buikema
Apr 24 '18 at 10:37
|
show 1 more comment
The only solution I've found is:
- Create a LinearLayout (I dunno if other kinds of Layout's will work)
- Set the attributes
android:focusable="true"
andandroid:focusableInTouchMode="true"
And the EditText
won't get the focus after starting the activity
Simple!! I initially added these properties to the parent view, which failed, then I set these to another layout and worked fine. Thanks!!
– Rino
Nov 15 '17 at 5:31
1
It works with a RelativeLayout as well.
– Brian Reinhold
Jan 19 '18 at 14:40
add a comment |
The problem seems to come from a property that I can only see in the XML form
of the layout.
Make sure to remove this line at the end of the declaration within the EditText
XML tags:
<requestFocus />
That should give something like that :
<EditText
android:id="@+id/emailField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress">
//<requestFocus /> /* <-- without this line */
</EditText>
add a comment |
using the information provided by other posters, I used the following solution:
in the layout XML
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:id="@+id/linearLayout_focus"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- AUTOCOMPLETE -->
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:inputType="textNoSuggestions|textVisiblePassword"/>
in onCreate()
private AutoCompleteTextView mAutoCompleteTextView;
private LinearLayout mLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
//get references to UI components
mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
}
and finally, in onResume()
@Override
protected void onResume() {
super.onResume();
//do not give the editbox focus automatically when activity starts
mAutoCompleteTextView.clearFocus();
mLinearLayout.requestFocus();
}
add a comment |
The following will stop edittext from taking focus when created, but grab it when you touch them.
<EditText
android:id="@+id/et_bonus_custom"
android:focusable="false" />
So you set focusable to false in the xml, but the key is in the java, which you add the following listener:
etBonus.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}
});
Because you are returning false, i.e. not consuming the event, the focusing behavior will proceed like normal.
5
But this will never gain focus in future, how to just stop focus only for initial(activity start) ??
– Jayesh
Mar 8 '13 at 7:46
1
The onTouchListener is called before other touch actions. So by enabling focusable on touch the standard focus happens on the first touch. The keyboard will come up and everything.
– MinceMan
Mar 9 '13 at 16:36
1
I think this is the best way to do it. Also, the mumbo-jumbo xml magic dummy code above did NOT work for a complex set of edit texts...If this works, I will definetely vote up.
– Radu
Jul 29 '13 at 9:02
But if you are having multiple edit texts it will get focus on next edit text and open up keyboard so it is better to add android:windowSoftInputMode="stateAlwaysHidden" in manifest for particular activity.
– Karan sharma
Apr 3 '18 at 11:09
Note: on Android P the system will no longer grab a default focus. This means that this hack is not necessary starting with P.
– MinceMan
May 16 '18 at 18:35
add a comment |
Try clearFocus() instead of setSelected(false)
. Every view in Android has both focusability and selectability, and I think that you want to just clear the focus.
That sounds promising, but at what point in the Activity lifecycle should it be called? If I call it in onCreate(), the EditText still has focus. Should it be called in onResume() or some other location? Thanks!
– Mark
Oct 12 '09 at 23:36
8
I combined the accepted answer with this answer. I calledmyEditText.clearFocus(); myDummyLinearLayout.requestFocus();
in theonResume
of the Activity. This ensured the EditText didn't keep the focus when the phone was rotated.
– teedyay
Oct 14 '10 at 21:02
add a comment |
I had tried several answers individually but the focus is still at the EditText. I only managed to solve it by using two of the below solution together.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
( Reference from Silver https://stackoverflow.com/a/8639921/15695 )
and remove
<requestFocus />
at EditText
( Reference from floydaddict https://stackoverflow.com/a/9681809 )
1
I had to add edittext.clearFocus() in addition to the above to get it working :)
– Nav
Sep 9 '14 at 13:12
add a comment |
None of this solutions worked for me. The way I fix the autofocus was:
<activity android:name=".android.InviteFriendsActivity" android:windowSoftInputMode="adjustPan">
<intent-filter >
</intent-filter>
</activity>
2
android:windowSoftInputMode="adjustPan" on any activity in the Android Manifest
– rallat
Jun 7 '12 at 8:33
add a comment |
Simple solution:
In AndroidManifest
in Activity
tag use
android:windowSoftInputMode="stateAlwaysHidden"
6
Strictly speaking, this does not solve the issue. the OP said: "I don't want any control to have input focus at startup." Your solution only hides the keyboard, theres a sublte difference.
– katzenhut
Sep 4 '14 at 10:04
@katzenhut yep, thats my issue with this answer exactly. Focusing on my edittext opens up a PlaceAutoComplete activity, so this answer is incomplete
– Zach
Jan 15 '17 at 6:48
This answer would be complete if the question was: How do I always ensure my activity never shows a keyboard. Which is not.
– Martin Marconcini
Mar 21 '18 at 0:13
add a comment |
You can just set "focusable" and "focusable in touch mode" to value true on the first TextView
of the layout
. In this way when the activity starts the TextView
will be focused but , due to its nature, you will see nothing focused on the screen and ,of course, there will be no keyboard displayed...
Does not work at all!
– kpsfoo
Jun 5 '14 at 17:00
works perfectly for me, only i set the value on the outmost layout in my activity and not to the first textview
– Maverick1st
Jan 13 '15 at 17:45
add a comment |
The following worked for me in Manifest
. Write ,
<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
Simplest answer, just add this in parent layout of the XML.
android:focusable="true"
android:focusableInTouchMode="true"
worked perfectly for me. one more thing to note is, dont add these lines to scroll view. It wont work in scroll view. But worked perfectly with linear layout.
– Karthic Srinivasan
Dec 29 '18 at 14:24
add a comment |
I needed to clear focus from all fields programmatically. I just added the following two statements to my main layout definition.
myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);
That's it. Fixed my problem instantly. Thanks, Silver, for pointing me in the right direction.
add a comment |
Add android:windowSoftInputMode="stateAlwaysHidden"
in the activity tag of the Manifest.xml
file.
Source
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
If you have another view on your activity like a ListView
, you can also do:
ListView.requestFocus();
in your onResume()
to grab focus from the editText
.
I know this question has been answered but just providing an alternative solution that worked for me :)
add a comment |
Try this before your first editable field:
<TextView
android:id="@+id/dummyfocus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/foo"
/>
----
findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();
add a comment |
Add following in onCreate
method:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
Being that I don't like to pollute the XML with something that is related to functionality, I created this method that "transparently" steals the focus from the first focusable view and then makes sure to remove itself when necessary!
public static View preventInitialFocus(final Activity activity)
{
final ViewGroup content = (ViewGroup)activity.findViewById(android.R.id.content);
final View root = content.getChildAt(0);
if (root == null) return null;
final View focusDummy = new View(activity);
final View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View view, boolean b)
{
view.setOnFocusChangeListener(null);
content.removeView(focusDummy);
}
};
focusDummy.setFocusable(true);
focusDummy.setFocusableInTouchMode(true);
content.addView(focusDummy, 0, new LinearLayout.LayoutParams(0, 0));
if (root instanceof ViewGroup)
{
final ViewGroup _root = (ViewGroup)root;
for (int i = 1, children = _root.getChildCount(); i < children; i++)
{
final View child = _root.getChildAt(i);
if (child.isFocusable() || child.isFocusableInTouchMode())
{
child.setOnFocusChangeListener(onFocusChangeListener);
break;
}
}
}
else if (root.isFocusable() || root.isFocusableInTouchMode())
root.setOnFocusChangeListener(onFocusChangeListener);
return focusDummy;
}
add a comment |
Late, but maybe helpful. Create a dummy EditText at the top of your layout then call myDummyEditText.requestFocus()
in onCreate()
<EditText android:id="@+id/dummyEditTextFocus"
android:layout_width="0px"
android:layout_height="0px" />
That seems to behave as I expect. No need to handle configuration changes, etc. I needed this for an Activity with a lengthy TextView (instructions).
Why not just do this with the root view itself?
– CJBS
Dec 1 '15 at 18:56
add a comment |
Yeah I did the same thing - create a 'dummy' linear layout which gets initial focus. Furthermore, I set the 'next' focus IDs so the user can't focus it any more after scrolling once:
<LinearLayout 'dummy'>
<EditText et>
dummy.setNextFocusDownId(et.getId());
dummy.setNextFocusUpId(et.getId());
et.setNextFocusUpId(et.getId());
a lot of work just to get rid of focus on a view..
Thanks
add a comment |
Write this line in your Parent Layout...
android:focusableInTouchMode="true"
add a comment |
For me, what worked on all devices is this:
<!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->
<View
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
Just put this as a view before the problematic focused view, and that's it.
add a comment |
This is the perfect and most easiest solution.I always use this in my app.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:07
add a comment |
The simplest thing I did is to set focus on another view in onCreate:
myView.setFocusableInTouchMode(true);
myView.requestFocus();
This stopped the soft keyboard coming up and there was no cursor flashing in the EditText.
add a comment |
Write this code inside Manifest
file in the Activity
where you do not want to open the keyboard.
android:windowSoftInputMode="stateHidden"
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projectt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="24" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
**android:windowSoftInputMode="stateHidden"**
android:label="@string/app_name" >
</activity>
</application>
</manifest>
2
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:06
add a comment |
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
style="@android:style/Widget.EditText"/>
add a comment |
At onCreate
of your Activity, just add use clearFocus()
on your EditText element.
For example,
edittext = (EditText) findViewById(R.id.edittext);
edittext.clearFocus();
And if you want to divert the focus to another element, use requestFocus()
on that.
For example,
button = (Button) findViewById(R.id.button);
button.requestFocus();
I tried this in oncreate, doesn't work. api 26.
– Jeffrey Liu
Aug 11 '17 at 17:57
add a comment |
You can achieve this by creating a dummy EditText
with layout width and height set to 0dp
, and request focus to that view.
Add the following code snippet in your xml layout:
<EditText
android:id="@+id/editText0"
android:layout_width="0dp"
android:layout_height="0dp"
android:hint="@string/dummy"
android:ems="10"
>
<requestFocus />
</EditText>
add a comment |
1 2
next
protected by Community♦ May 24 '13 at 12:27
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
48 Answers
48
active
oldest
votes
48 Answers
48
active
oldest
votes
active
oldest
votes
active
oldest
votes
1 2
next
Excellent answers from Luc and Mark however a good code sample is missing. Adding the tag android:focusableInTouchMode="true"
to <LinearLayout>
like the following example will fix the problem.
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>
647
What about setting the parent layout toandroid:focusableInTouchMode="true"
!
– Muhammad Babar
Mar 21 '14 at 5:58
22
Perfect! Thanks... One thing to note is that the dummy item has to be PLACED RIGHT BEFORE the focusable element!!! I had a TextView between my dummy and the EditText and the method did not work!
– maddob
Jul 10 '14 at 9:12
23
setting android:focusableInTouchMode="true" to parent layout was the good approach thanks @Muhammad Babar
– sujith s
Mar 24 '15 at 10:15
19
can someone explain why this works?
– Pietro Rea
Apr 23 '15 at 23:10
13
@PietroRea Android has annoying auto-focus behaviors that are built into the core behavior of its app structure. If you tell it that an edit text is releasing or losing focus, it automatically makes decision on where the focus goes. Without a new object for the focus to transfer to, it picks the first focusable element in the layout even when that element is the one that just cleared its focus! It's kinda nuts and frustrating, but maybe there's a valid reason for this type of behavior.
– Weston Wedding
Mar 8 '16 at 16:47
|
show 13 more comments
Excellent answers from Luc and Mark however a good code sample is missing. Adding the tag android:focusableInTouchMode="true"
to <LinearLayout>
like the following example will fix the problem.
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>
647
What about setting the parent layout toandroid:focusableInTouchMode="true"
!
– Muhammad Babar
Mar 21 '14 at 5:58
22
Perfect! Thanks... One thing to note is that the dummy item has to be PLACED RIGHT BEFORE the focusable element!!! I had a TextView between my dummy and the EditText and the method did not work!
– maddob
Jul 10 '14 at 9:12
23
setting android:focusableInTouchMode="true" to parent layout was the good approach thanks @Muhammad Babar
– sujith s
Mar 24 '15 at 10:15
19
can someone explain why this works?
– Pietro Rea
Apr 23 '15 at 23:10
13
@PietroRea Android has annoying auto-focus behaviors that are built into the core behavior of its app structure. If you tell it that an edit text is releasing or losing focus, it automatically makes decision on where the focus goes. Without a new object for the focus to transfer to, it picks the first focusable element in the layout even when that element is the one that just cleared its focus! It's kinda nuts and frustrating, but maybe there's a valid reason for this type of behavior.
– Weston Wedding
Mar 8 '16 at 16:47
|
show 13 more comments
Excellent answers from Luc and Mark however a good code sample is missing. Adding the tag android:focusableInTouchMode="true"
to <LinearLayout>
like the following example will fix the problem.
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>
Excellent answers from Luc and Mark however a good code sample is missing. Adding the tag android:focusableInTouchMode="true"
to <LinearLayout>
like the following example will fix the problem.
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>
edited Jul 27 '18 at 21:33
thechaoticpanda
599
599
answered Nov 2 '09 at 15:49
Morgan ChristianssonMorgan Christiansson
23.5k11410
23.5k11410
647
What about setting the parent layout toandroid:focusableInTouchMode="true"
!
– Muhammad Babar
Mar 21 '14 at 5:58
22
Perfect! Thanks... One thing to note is that the dummy item has to be PLACED RIGHT BEFORE the focusable element!!! I had a TextView between my dummy and the EditText and the method did not work!
– maddob
Jul 10 '14 at 9:12
23
setting android:focusableInTouchMode="true" to parent layout was the good approach thanks @Muhammad Babar
– sujith s
Mar 24 '15 at 10:15
19
can someone explain why this works?
– Pietro Rea
Apr 23 '15 at 23:10
13
@PietroRea Android has annoying auto-focus behaviors that are built into the core behavior of its app structure. If you tell it that an edit text is releasing or losing focus, it automatically makes decision on where the focus goes. Without a new object for the focus to transfer to, it picks the first focusable element in the layout even when that element is the one that just cleared its focus! It's kinda nuts and frustrating, but maybe there's a valid reason for this type of behavior.
– Weston Wedding
Mar 8 '16 at 16:47
|
show 13 more comments
647
What about setting the parent layout toandroid:focusableInTouchMode="true"
!
– Muhammad Babar
Mar 21 '14 at 5:58
22
Perfect! Thanks... One thing to note is that the dummy item has to be PLACED RIGHT BEFORE the focusable element!!! I had a TextView between my dummy and the EditText and the method did not work!
– maddob
Jul 10 '14 at 9:12
23
setting android:focusableInTouchMode="true" to parent layout was the good approach thanks @Muhammad Babar
– sujith s
Mar 24 '15 at 10:15
19
can someone explain why this works?
– Pietro Rea
Apr 23 '15 at 23:10
13
@PietroRea Android has annoying auto-focus behaviors that are built into the core behavior of its app structure. If you tell it that an edit text is releasing or losing focus, it automatically makes decision on where the focus goes. Without a new object for the focus to transfer to, it picks the first focusable element in the layout even when that element is the one that just cleared its focus! It's kinda nuts and frustrating, but maybe there's a valid reason for this type of behavior.
– Weston Wedding
Mar 8 '16 at 16:47
647
647
What about setting the parent layout to
android:focusableInTouchMode="true"
!– Muhammad Babar
Mar 21 '14 at 5:58
What about setting the parent layout to
android:focusableInTouchMode="true"
!– Muhammad Babar
Mar 21 '14 at 5:58
22
22
Perfect! Thanks... One thing to note is that the dummy item has to be PLACED RIGHT BEFORE the focusable element!!! I had a TextView between my dummy and the EditText and the method did not work!
– maddob
Jul 10 '14 at 9:12
Perfect! Thanks... One thing to note is that the dummy item has to be PLACED RIGHT BEFORE the focusable element!!! I had a TextView between my dummy and the EditText and the method did not work!
– maddob
Jul 10 '14 at 9:12
23
23
setting android:focusableInTouchMode="true" to parent layout was the good approach thanks @Muhammad Babar
– sujith s
Mar 24 '15 at 10:15
setting android:focusableInTouchMode="true" to parent layout was the good approach thanks @Muhammad Babar
– sujith s
Mar 24 '15 at 10:15
19
19
can someone explain why this works?
– Pietro Rea
Apr 23 '15 at 23:10
can someone explain why this works?
– Pietro Rea
Apr 23 '15 at 23:10
13
13
@PietroRea Android has annoying auto-focus behaviors that are built into the core behavior of its app structure. If you tell it that an edit text is releasing or losing focus, it automatically makes decision on where the focus goes. Without a new object for the focus to transfer to, it picks the first focusable element in the layout even when that element is the one that just cleared its focus! It's kinda nuts and frustrating, but maybe there's a valid reason for this type of behavior.
– Weston Wedding
Mar 8 '16 at 16:47
@PietroRea Android has annoying auto-focus behaviors that are built into the core behavior of its app structure. If you tell it that an edit text is releasing or losing focus, it automatically makes decision on where the focus goes. Without a new object for the focus to transfer to, it picks the first focusable element in the layout even when that element is the one that just cleared its focus! It's kinda nuts and frustrating, but maybe there's a valid reason for this type of behavior.
– Weston Wedding
Mar 8 '16 at 16:47
|
show 13 more comments
Is the actual problem that you just don't want it to have focus at all? Or you don't want it to show the virtual keyboard as a result of focusing the EditText
? I don't really see an issue with the EditText
having focus on start, but it's definitely a problem to have the softInput window open when the user did not explicitly request to focus on the EditText
(and open the keyboard as a result).
If it's the problem of the virtual keyboard, see the AndroidManifest.xml
<activity> element documentation.
android:windowSoftInputMode="stateHidden"
- always hide it when entering the activity.
or android:windowSoftInputMode="stateUnchanged"
- don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).
22
yet still the cursor is in the first EditText in the layout - even though the keyboard is not shown
– martyglaubitz
May 20 '14 at 8:17
36
@Anderson: Nothing about the answer implied that it would prevent theEditText
from obtaining focus. It actually clearly states that this is how you prevent the software keyboard IME from opening automatically on focus; because it is more likely that the bigger concern is the soft keyboard popping up unexpectedly, not the focus itself. If your issue is with theEditText
actually having focus at all, then use someone else's answer.
– Joe
Jul 8 '14 at 23:38
1
set it programmatically: stackoverflow.com/questions/6138330/…
– Nick Franceschina
Oct 8 '14 at 18:26
3
Don't do this on an activity with a camera -- it breaks the timing of the flash (don't ask me why)
– Daniel Wilson
Jun 16 '15 at 11:54
One reason to not have focus at startup is that if the imeOptions are changed after the view has focus and the keyboard shows, the imeOptions will be those set before the change.
– n_b
Apr 27 '16 at 21:57
|
show 3 more comments
Is the actual problem that you just don't want it to have focus at all? Or you don't want it to show the virtual keyboard as a result of focusing the EditText
? I don't really see an issue with the EditText
having focus on start, but it's definitely a problem to have the softInput window open when the user did not explicitly request to focus on the EditText
(and open the keyboard as a result).
If it's the problem of the virtual keyboard, see the AndroidManifest.xml
<activity> element documentation.
android:windowSoftInputMode="stateHidden"
- always hide it when entering the activity.
or android:windowSoftInputMode="stateUnchanged"
- don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).
22
yet still the cursor is in the first EditText in the layout - even though the keyboard is not shown
– martyglaubitz
May 20 '14 at 8:17
36
@Anderson: Nothing about the answer implied that it would prevent theEditText
from obtaining focus. It actually clearly states that this is how you prevent the software keyboard IME from opening automatically on focus; because it is more likely that the bigger concern is the soft keyboard popping up unexpectedly, not the focus itself. If your issue is with theEditText
actually having focus at all, then use someone else's answer.
– Joe
Jul 8 '14 at 23:38
1
set it programmatically: stackoverflow.com/questions/6138330/…
– Nick Franceschina
Oct 8 '14 at 18:26
3
Don't do this on an activity with a camera -- it breaks the timing of the flash (don't ask me why)
– Daniel Wilson
Jun 16 '15 at 11:54
One reason to not have focus at startup is that if the imeOptions are changed after the view has focus and the keyboard shows, the imeOptions will be those set before the change.
– n_b
Apr 27 '16 at 21:57
|
show 3 more comments
Is the actual problem that you just don't want it to have focus at all? Or you don't want it to show the virtual keyboard as a result of focusing the EditText
? I don't really see an issue with the EditText
having focus on start, but it's definitely a problem to have the softInput window open when the user did not explicitly request to focus on the EditText
(and open the keyboard as a result).
If it's the problem of the virtual keyboard, see the AndroidManifest.xml
<activity> element documentation.
android:windowSoftInputMode="stateHidden"
- always hide it when entering the activity.
or android:windowSoftInputMode="stateUnchanged"
- don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).
Is the actual problem that you just don't want it to have focus at all? Or you don't want it to show the virtual keyboard as a result of focusing the EditText
? I don't really see an issue with the EditText
having focus on start, but it's definitely a problem to have the softInput window open when the user did not explicitly request to focus on the EditText
(and open the keyboard as a result).
If it's the problem of the virtual keyboard, see the AndroidManifest.xml
<activity> element documentation.
android:windowSoftInputMode="stateHidden"
- always hide it when entering the activity.
or android:windowSoftInputMode="stateUnchanged"
- don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).
edited Aug 11 '17 at 17:02
Alexander Abakumov
4,88854773
4,88854773
answered Apr 9 '10 at 21:22
JoeJoe
38.4k113857
38.4k113857
22
yet still the cursor is in the first EditText in the layout - even though the keyboard is not shown
– martyglaubitz
May 20 '14 at 8:17
36
@Anderson: Nothing about the answer implied that it would prevent theEditText
from obtaining focus. It actually clearly states that this is how you prevent the software keyboard IME from opening automatically on focus; because it is more likely that the bigger concern is the soft keyboard popping up unexpectedly, not the focus itself. If your issue is with theEditText
actually having focus at all, then use someone else's answer.
– Joe
Jul 8 '14 at 23:38
1
set it programmatically: stackoverflow.com/questions/6138330/…
– Nick Franceschina
Oct 8 '14 at 18:26
3
Don't do this on an activity with a camera -- it breaks the timing of the flash (don't ask me why)
– Daniel Wilson
Jun 16 '15 at 11:54
One reason to not have focus at startup is that if the imeOptions are changed after the view has focus and the keyboard shows, the imeOptions will be those set before the change.
– n_b
Apr 27 '16 at 21:57
|
show 3 more comments
22
yet still the cursor is in the first EditText in the layout - even though the keyboard is not shown
– martyglaubitz
May 20 '14 at 8:17
36
@Anderson: Nothing about the answer implied that it would prevent theEditText
from obtaining focus. It actually clearly states that this is how you prevent the software keyboard IME from opening automatically on focus; because it is more likely that the bigger concern is the soft keyboard popping up unexpectedly, not the focus itself. If your issue is with theEditText
actually having focus at all, then use someone else's answer.
– Joe
Jul 8 '14 at 23:38
1
set it programmatically: stackoverflow.com/questions/6138330/…
– Nick Franceschina
Oct 8 '14 at 18:26
3
Don't do this on an activity with a camera -- it breaks the timing of the flash (don't ask me why)
– Daniel Wilson
Jun 16 '15 at 11:54
One reason to not have focus at startup is that if the imeOptions are changed after the view has focus and the keyboard shows, the imeOptions will be those set before the change.
– n_b
Apr 27 '16 at 21:57
22
22
yet still the cursor is in the first EditText in the layout - even though the keyboard is not shown
– martyglaubitz
May 20 '14 at 8:17
yet still the cursor is in the first EditText in the layout - even though the keyboard is not shown
– martyglaubitz
May 20 '14 at 8:17
36
36
@Anderson: Nothing about the answer implied that it would prevent the
EditText
from obtaining focus. It actually clearly states that this is how you prevent the software keyboard IME from opening automatically on focus; because it is more likely that the bigger concern is the soft keyboard popping up unexpectedly, not the focus itself. If your issue is with the EditText
actually having focus at all, then use someone else's answer.– Joe
Jul 8 '14 at 23:38
@Anderson: Nothing about the answer implied that it would prevent the
EditText
from obtaining focus. It actually clearly states that this is how you prevent the software keyboard IME from opening automatically on focus; because it is more likely that the bigger concern is the soft keyboard popping up unexpectedly, not the focus itself. If your issue is with the EditText
actually having focus at all, then use someone else's answer.– Joe
Jul 8 '14 at 23:38
1
1
set it programmatically: stackoverflow.com/questions/6138330/…
– Nick Franceschina
Oct 8 '14 at 18:26
set it programmatically: stackoverflow.com/questions/6138330/…
– Nick Franceschina
Oct 8 '14 at 18:26
3
3
Don't do this on an activity with a camera -- it breaks the timing of the flash (don't ask me why)
– Daniel Wilson
Jun 16 '15 at 11:54
Don't do this on an activity with a camera -- it breaks the timing of the flash (don't ask me why)
– Daniel Wilson
Jun 16 '15 at 11:54
One reason to not have focus at startup is that if the imeOptions are changed after the view has focus and the keyboard shows, the imeOptions will be those set before the change.
– n_b
Apr 27 '16 at 21:57
One reason to not have focus at startup is that if the imeOptions are changed after the view has focus and the keyboard shows, the imeOptions will be those set before the change.
– n_b
Apr 27 '16 at 21:57
|
show 3 more comments
A simpler solution exists. Set these attributes in your parent layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
And now, when the activity starts this main layout will get focus by default.
Also, we can remove focus from child views at runtime (e.g., after finishing child editing) by giving the focus to the main layout again, like this:
findViewById(R.id.mainLayout).requestFocus();
Good comment from Guillaume Perrot:
android:descendantFocusability="beforeDescendants"
seems to be the
default (integer value is 0). It works just by adding
android:focusableInTouchMode="true"
.
Really, we can see that the beforeDescendants
set as default in the ViewGroup.initViewGroup()
method (Android 2.2.2). But not equal to 0. ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;
Thanks to Guillaume.
1
It's important to set these attributes to the direct parent of the view which gains focus on activity creation, otherwise it won't work.
– tomrozb
Feb 9 '15 at 14:10
29
Note this doesn't seem to work if the parent is a ScrollView
– Daniel Wilson
Jun 16 '15 at 13:52
4
Just a perfect solution. Works for both: 1.Disabling autofocus, and 2. Disable auto-opening of soft keyboard
– Meet
Mar 14 '16 at 10:38
Nice. It does not need to be a direct parent. I set it at root level of layout and it seems work fine.
– Kurotsuki
Jan 21 '17 at 17:58
@DanielWilson when would you want a scrollview as direct parent of an edittext?
– Mark Buikema
Apr 24 '18 at 10:37
|
show 1 more comment
A simpler solution exists. Set these attributes in your parent layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
And now, when the activity starts this main layout will get focus by default.
Also, we can remove focus from child views at runtime (e.g., after finishing child editing) by giving the focus to the main layout again, like this:
findViewById(R.id.mainLayout).requestFocus();
Good comment from Guillaume Perrot:
android:descendantFocusability="beforeDescendants"
seems to be the
default (integer value is 0). It works just by adding
android:focusableInTouchMode="true"
.
Really, we can see that the beforeDescendants
set as default in the ViewGroup.initViewGroup()
method (Android 2.2.2). But not equal to 0. ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;
Thanks to Guillaume.
1
It's important to set these attributes to the direct parent of the view which gains focus on activity creation, otherwise it won't work.
– tomrozb
Feb 9 '15 at 14:10
29
Note this doesn't seem to work if the parent is a ScrollView
– Daniel Wilson
Jun 16 '15 at 13:52
4
Just a perfect solution. Works for both: 1.Disabling autofocus, and 2. Disable auto-opening of soft keyboard
– Meet
Mar 14 '16 at 10:38
Nice. It does not need to be a direct parent. I set it at root level of layout and it seems work fine.
– Kurotsuki
Jan 21 '17 at 17:58
@DanielWilson when would you want a scrollview as direct parent of an edittext?
– Mark Buikema
Apr 24 '18 at 10:37
|
show 1 more comment
A simpler solution exists. Set these attributes in your parent layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
And now, when the activity starts this main layout will get focus by default.
Also, we can remove focus from child views at runtime (e.g., after finishing child editing) by giving the focus to the main layout again, like this:
findViewById(R.id.mainLayout).requestFocus();
Good comment from Guillaume Perrot:
android:descendantFocusability="beforeDescendants"
seems to be the
default (integer value is 0). It works just by adding
android:focusableInTouchMode="true"
.
Really, we can see that the beforeDescendants
set as default in the ViewGroup.initViewGroup()
method (Android 2.2.2). But not equal to 0. ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;
Thanks to Guillaume.
A simpler solution exists. Set these attributes in your parent layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
And now, when the activity starts this main layout will get focus by default.
Also, we can remove focus from child views at runtime (e.g., after finishing child editing) by giving the focus to the main layout again, like this:
findViewById(R.id.mainLayout).requestFocus();
Good comment from Guillaume Perrot:
android:descendantFocusability="beforeDescendants"
seems to be the
default (integer value is 0). It works just by adding
android:focusableInTouchMode="true"
.
Really, we can see that the beforeDescendants
set as default in the ViewGroup.initViewGroup()
method (Android 2.2.2). But not equal to 0. ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;
Thanks to Guillaume.
edited Aug 14 '17 at 19:13
Jon
4,93853151
4,93853151
answered Dec 26 '11 at 23:35
SilverSilver
11.1k21011
11.1k21011
1
It's important to set these attributes to the direct parent of the view which gains focus on activity creation, otherwise it won't work.
– tomrozb
Feb 9 '15 at 14:10
29
Note this doesn't seem to work if the parent is a ScrollView
– Daniel Wilson
Jun 16 '15 at 13:52
4
Just a perfect solution. Works for both: 1.Disabling autofocus, and 2. Disable auto-opening of soft keyboard
– Meet
Mar 14 '16 at 10:38
Nice. It does not need to be a direct parent. I set it at root level of layout and it seems work fine.
– Kurotsuki
Jan 21 '17 at 17:58
@DanielWilson when would you want a scrollview as direct parent of an edittext?
– Mark Buikema
Apr 24 '18 at 10:37
|
show 1 more comment
1
It's important to set these attributes to the direct parent of the view which gains focus on activity creation, otherwise it won't work.
– tomrozb
Feb 9 '15 at 14:10
29
Note this doesn't seem to work if the parent is a ScrollView
– Daniel Wilson
Jun 16 '15 at 13:52
4
Just a perfect solution. Works for both: 1.Disabling autofocus, and 2. Disable auto-opening of soft keyboard
– Meet
Mar 14 '16 at 10:38
Nice. It does not need to be a direct parent. I set it at root level of layout and it seems work fine.
– Kurotsuki
Jan 21 '17 at 17:58
@DanielWilson when would you want a scrollview as direct parent of an edittext?
– Mark Buikema
Apr 24 '18 at 10:37
1
1
It's important to set these attributes to the direct parent of the view which gains focus on activity creation, otherwise it won't work.
– tomrozb
Feb 9 '15 at 14:10
It's important to set these attributes to the direct parent of the view which gains focus on activity creation, otherwise it won't work.
– tomrozb
Feb 9 '15 at 14:10
29
29
Note this doesn't seem to work if the parent is a ScrollView
– Daniel Wilson
Jun 16 '15 at 13:52
Note this doesn't seem to work if the parent is a ScrollView
– Daniel Wilson
Jun 16 '15 at 13:52
4
4
Just a perfect solution. Works for both: 1.Disabling autofocus, and 2. Disable auto-opening of soft keyboard
– Meet
Mar 14 '16 at 10:38
Just a perfect solution. Works for both: 1.Disabling autofocus, and 2. Disable auto-opening of soft keyboard
– Meet
Mar 14 '16 at 10:38
Nice. It does not need to be a direct parent. I set it at root level of layout and it seems work fine.
– Kurotsuki
Jan 21 '17 at 17:58
Nice. It does not need to be a direct parent. I set it at root level of layout and it seems work fine.
– Kurotsuki
Jan 21 '17 at 17:58
@DanielWilson when would you want a scrollview as direct parent of an edittext?
– Mark Buikema
Apr 24 '18 at 10:37
@DanielWilson when would you want a scrollview as direct parent of an edittext?
– Mark Buikema
Apr 24 '18 at 10:37
|
show 1 more comment
The only solution I've found is:
- Create a LinearLayout (I dunno if other kinds of Layout's will work)
- Set the attributes
android:focusable="true"
andandroid:focusableInTouchMode="true"
And the EditText
won't get the focus after starting the activity
Simple!! I initially added these properties to the parent view, which failed, then I set these to another layout and worked fine. Thanks!!
– Rino
Nov 15 '17 at 5:31
1
It works with a RelativeLayout as well.
– Brian Reinhold
Jan 19 '18 at 14:40
add a comment |
The only solution I've found is:
- Create a LinearLayout (I dunno if other kinds of Layout's will work)
- Set the attributes
android:focusable="true"
andandroid:focusableInTouchMode="true"
And the EditText
won't get the focus after starting the activity
Simple!! I initially added these properties to the parent view, which failed, then I set these to another layout and worked fine. Thanks!!
– Rino
Nov 15 '17 at 5:31
1
It works with a RelativeLayout as well.
– Brian Reinhold
Jan 19 '18 at 14:40
add a comment |
The only solution I've found is:
- Create a LinearLayout (I dunno if other kinds of Layout's will work)
- Set the attributes
android:focusable="true"
andandroid:focusableInTouchMode="true"
And the EditText
won't get the focus after starting the activity
The only solution I've found is:
- Create a LinearLayout (I dunno if other kinds of Layout's will work)
- Set the attributes
android:focusable="true"
andandroid:focusableInTouchMode="true"
And the EditText
won't get the focus after starting the activity
edited Oct 21 '17 at 22:30
Keet Sugathadasa
717621
717621
answered Oct 23 '09 at 8:12
LucLuc
4,0391103
4,0391103
Simple!! I initially added these properties to the parent view, which failed, then I set these to another layout and worked fine. Thanks!!
– Rino
Nov 15 '17 at 5:31
1
It works with a RelativeLayout as well.
– Brian Reinhold
Jan 19 '18 at 14:40
add a comment |
Simple!! I initially added these properties to the parent view, which failed, then I set these to another layout and worked fine. Thanks!!
– Rino
Nov 15 '17 at 5:31
1
It works with a RelativeLayout as well.
– Brian Reinhold
Jan 19 '18 at 14:40
Simple!! I initially added these properties to the parent view, which failed, then I set these to another layout and worked fine. Thanks!!
– Rino
Nov 15 '17 at 5:31
Simple!! I initially added these properties to the parent view, which failed, then I set these to another layout and worked fine. Thanks!!
– Rino
Nov 15 '17 at 5:31
1
1
It works with a RelativeLayout as well.
– Brian Reinhold
Jan 19 '18 at 14:40
It works with a RelativeLayout as well.
– Brian Reinhold
Jan 19 '18 at 14:40
add a comment |
The problem seems to come from a property that I can only see in the XML form
of the layout.
Make sure to remove this line at the end of the declaration within the EditText
XML tags:
<requestFocus />
That should give something like that :
<EditText
android:id="@+id/emailField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress">
//<requestFocus /> /* <-- without this line */
</EditText>
add a comment |
The problem seems to come from a property that I can only see in the XML form
of the layout.
Make sure to remove this line at the end of the declaration within the EditText
XML tags:
<requestFocus />
That should give something like that :
<EditText
android:id="@+id/emailField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress">
//<requestFocus /> /* <-- without this line */
</EditText>
add a comment |
The problem seems to come from a property that I can only see in the XML form
of the layout.
Make sure to remove this line at the end of the declaration within the EditText
XML tags:
<requestFocus />
That should give something like that :
<EditText
android:id="@+id/emailField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress">
//<requestFocus /> /* <-- without this line */
</EditText>
The problem seems to come from a property that I can only see in the XML form
of the layout.
Make sure to remove this line at the end of the declaration within the EditText
XML tags:
<requestFocus />
That should give something like that :
<EditText
android:id="@+id/emailField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress">
//<requestFocus /> /* <-- without this line */
</EditText>
edited Oct 21 '17 at 21:17
Keet Sugathadasa
717621
717621
answered Mar 13 '12 at 9:57
floydaddictfloydaddict
85264
85264
add a comment |
add a comment |
using the information provided by other posters, I used the following solution:
in the layout XML
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:id="@+id/linearLayout_focus"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- AUTOCOMPLETE -->
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:inputType="textNoSuggestions|textVisiblePassword"/>
in onCreate()
private AutoCompleteTextView mAutoCompleteTextView;
private LinearLayout mLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
//get references to UI components
mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
}
and finally, in onResume()
@Override
protected void onResume() {
super.onResume();
//do not give the editbox focus automatically when activity starts
mAutoCompleteTextView.clearFocus();
mLinearLayout.requestFocus();
}
add a comment |
using the information provided by other posters, I used the following solution:
in the layout XML
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:id="@+id/linearLayout_focus"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- AUTOCOMPLETE -->
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:inputType="textNoSuggestions|textVisiblePassword"/>
in onCreate()
private AutoCompleteTextView mAutoCompleteTextView;
private LinearLayout mLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
//get references to UI components
mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
}
and finally, in onResume()
@Override
protected void onResume() {
super.onResume();
//do not give the editbox focus automatically when activity starts
mAutoCompleteTextView.clearFocus();
mLinearLayout.requestFocus();
}
add a comment |
using the information provided by other posters, I used the following solution:
in the layout XML
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:id="@+id/linearLayout_focus"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- AUTOCOMPLETE -->
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:inputType="textNoSuggestions|textVisiblePassword"/>
in onCreate()
private AutoCompleteTextView mAutoCompleteTextView;
private LinearLayout mLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
//get references to UI components
mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
}
and finally, in onResume()
@Override
protected void onResume() {
super.onResume();
//do not give the editbox focus automatically when activity starts
mAutoCompleteTextView.clearFocus();
mLinearLayout.requestFocus();
}
using the information provided by other posters, I used the following solution:
in the layout XML
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:id="@+id/linearLayout_focus"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- AUTOCOMPLETE -->
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:inputType="textNoSuggestions|textVisiblePassword"/>
in onCreate()
private AutoCompleteTextView mAutoCompleteTextView;
private LinearLayout mLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
//get references to UI components
mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
}
and finally, in onResume()
@Override
protected void onResume() {
super.onResume();
//do not give the editbox focus automatically when activity starts
mAutoCompleteTextView.clearFocus();
mLinearLayout.requestFocus();
}
edited Apr 24 '15 at 22:21
Ziem
4,07074274
4,07074274
answered May 26 '11 at 21:07
Someone SomewhereSomeone Somewhere
18.9k1096141
18.9k1096141
add a comment |
add a comment |
The following will stop edittext from taking focus when created, but grab it when you touch them.
<EditText
android:id="@+id/et_bonus_custom"
android:focusable="false" />
So you set focusable to false in the xml, but the key is in the java, which you add the following listener:
etBonus.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}
});
Because you are returning false, i.e. not consuming the event, the focusing behavior will proceed like normal.
5
But this will never gain focus in future, how to just stop focus only for initial(activity start) ??
– Jayesh
Mar 8 '13 at 7:46
1
The onTouchListener is called before other touch actions. So by enabling focusable on touch the standard focus happens on the first touch. The keyboard will come up and everything.
– MinceMan
Mar 9 '13 at 16:36
1
I think this is the best way to do it. Also, the mumbo-jumbo xml magic dummy code above did NOT work for a complex set of edit texts...If this works, I will definetely vote up.
– Radu
Jul 29 '13 at 9:02
But if you are having multiple edit texts it will get focus on next edit text and open up keyboard so it is better to add android:windowSoftInputMode="stateAlwaysHidden" in manifest for particular activity.
– Karan sharma
Apr 3 '18 at 11:09
Note: on Android P the system will no longer grab a default focus. This means that this hack is not necessary starting with P.
– MinceMan
May 16 '18 at 18:35
add a comment |
The following will stop edittext from taking focus when created, but grab it when you touch them.
<EditText
android:id="@+id/et_bonus_custom"
android:focusable="false" />
So you set focusable to false in the xml, but the key is in the java, which you add the following listener:
etBonus.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}
});
Because you are returning false, i.e. not consuming the event, the focusing behavior will proceed like normal.
5
But this will never gain focus in future, how to just stop focus only for initial(activity start) ??
– Jayesh
Mar 8 '13 at 7:46
1
The onTouchListener is called before other touch actions. So by enabling focusable on touch the standard focus happens on the first touch. The keyboard will come up and everything.
– MinceMan
Mar 9 '13 at 16:36
1
I think this is the best way to do it. Also, the mumbo-jumbo xml magic dummy code above did NOT work for a complex set of edit texts...If this works, I will definetely vote up.
– Radu
Jul 29 '13 at 9:02
But if you are having multiple edit texts it will get focus on next edit text and open up keyboard so it is better to add android:windowSoftInputMode="stateAlwaysHidden" in manifest for particular activity.
– Karan sharma
Apr 3 '18 at 11:09
Note: on Android P the system will no longer grab a default focus. This means that this hack is not necessary starting with P.
– MinceMan
May 16 '18 at 18:35
add a comment |
The following will stop edittext from taking focus when created, but grab it when you touch them.
<EditText
android:id="@+id/et_bonus_custom"
android:focusable="false" />
So you set focusable to false in the xml, but the key is in the java, which you add the following listener:
etBonus.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}
});
Because you are returning false, i.e. not consuming the event, the focusing behavior will proceed like normal.
The following will stop edittext from taking focus when created, but grab it when you touch them.
<EditText
android:id="@+id/et_bonus_custom"
android:focusable="false" />
So you set focusable to false in the xml, but the key is in the java, which you add the following listener:
etBonus.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}
});
Because you are returning false, i.e. not consuming the event, the focusing behavior will proceed like normal.
edited Apr 24 '15 at 21:43
Ziem
4,07074274
4,07074274
answered Jun 15 '12 at 4:45
MinceManMinceMan
5,8802735
5,8802735
5
But this will never gain focus in future, how to just stop focus only for initial(activity start) ??
– Jayesh
Mar 8 '13 at 7:46
1
The onTouchListener is called before other touch actions. So by enabling focusable on touch the standard focus happens on the first touch. The keyboard will come up and everything.
– MinceMan
Mar 9 '13 at 16:36
1
I think this is the best way to do it. Also, the mumbo-jumbo xml magic dummy code above did NOT work for a complex set of edit texts...If this works, I will definetely vote up.
– Radu
Jul 29 '13 at 9:02
But if you are having multiple edit texts it will get focus on next edit text and open up keyboard so it is better to add android:windowSoftInputMode="stateAlwaysHidden" in manifest for particular activity.
– Karan sharma
Apr 3 '18 at 11:09
Note: on Android P the system will no longer grab a default focus. This means that this hack is not necessary starting with P.
– MinceMan
May 16 '18 at 18:35
add a comment |
5
But this will never gain focus in future, how to just stop focus only for initial(activity start) ??
– Jayesh
Mar 8 '13 at 7:46
1
The onTouchListener is called before other touch actions. So by enabling focusable on touch the standard focus happens on the first touch. The keyboard will come up and everything.
– MinceMan
Mar 9 '13 at 16:36
1
I think this is the best way to do it. Also, the mumbo-jumbo xml magic dummy code above did NOT work for a complex set of edit texts...If this works, I will definetely vote up.
– Radu
Jul 29 '13 at 9:02
But if you are having multiple edit texts it will get focus on next edit text and open up keyboard so it is better to add android:windowSoftInputMode="stateAlwaysHidden" in manifest for particular activity.
– Karan sharma
Apr 3 '18 at 11:09
Note: on Android P the system will no longer grab a default focus. This means that this hack is not necessary starting with P.
– MinceMan
May 16 '18 at 18:35
5
5
But this will never gain focus in future, how to just stop focus only for initial(activity start) ??
– Jayesh
Mar 8 '13 at 7:46
But this will never gain focus in future, how to just stop focus only for initial(activity start) ??
– Jayesh
Mar 8 '13 at 7:46
1
1
The onTouchListener is called before other touch actions. So by enabling focusable on touch the standard focus happens on the first touch. The keyboard will come up and everything.
– MinceMan
Mar 9 '13 at 16:36
The onTouchListener is called before other touch actions. So by enabling focusable on touch the standard focus happens on the first touch. The keyboard will come up and everything.
– MinceMan
Mar 9 '13 at 16:36
1
1
I think this is the best way to do it. Also, the mumbo-jumbo xml magic dummy code above did NOT work for a complex set of edit texts...If this works, I will definetely vote up.
– Radu
Jul 29 '13 at 9:02
I think this is the best way to do it. Also, the mumbo-jumbo xml magic dummy code above did NOT work for a complex set of edit texts...If this works, I will definetely vote up.
– Radu
Jul 29 '13 at 9:02
But if you are having multiple edit texts it will get focus on next edit text and open up keyboard so it is better to add android:windowSoftInputMode="stateAlwaysHidden" in manifest for particular activity.
– Karan sharma
Apr 3 '18 at 11:09
But if you are having multiple edit texts it will get focus on next edit text and open up keyboard so it is better to add android:windowSoftInputMode="stateAlwaysHidden" in manifest for particular activity.
– Karan sharma
Apr 3 '18 at 11:09
Note: on Android P the system will no longer grab a default focus. This means that this hack is not necessary starting with P.
– MinceMan
May 16 '18 at 18:35
Note: on Android P the system will no longer grab a default focus. This means that this hack is not necessary starting with P.
– MinceMan
May 16 '18 at 18:35
add a comment |
Try clearFocus() instead of setSelected(false)
. Every view in Android has both focusability and selectability, and I think that you want to just clear the focus.
That sounds promising, but at what point in the Activity lifecycle should it be called? If I call it in onCreate(), the EditText still has focus. Should it be called in onResume() or some other location? Thanks!
– Mark
Oct 12 '09 at 23:36
8
I combined the accepted answer with this answer. I calledmyEditText.clearFocus(); myDummyLinearLayout.requestFocus();
in theonResume
of the Activity. This ensured the EditText didn't keep the focus when the phone was rotated.
– teedyay
Oct 14 '10 at 21:02
add a comment |
Try clearFocus() instead of setSelected(false)
. Every view in Android has both focusability and selectability, and I think that you want to just clear the focus.
That sounds promising, but at what point in the Activity lifecycle should it be called? If I call it in onCreate(), the EditText still has focus. Should it be called in onResume() or some other location? Thanks!
– Mark
Oct 12 '09 at 23:36
8
I combined the accepted answer with this answer. I calledmyEditText.clearFocus(); myDummyLinearLayout.requestFocus();
in theonResume
of the Activity. This ensured the EditText didn't keep the focus when the phone was rotated.
– teedyay
Oct 14 '10 at 21:02
add a comment |
Try clearFocus() instead of setSelected(false)
. Every view in Android has both focusability and selectability, and I think that you want to just clear the focus.
Try clearFocus() instead of setSelected(false)
. Every view in Android has both focusability and selectability, and I think that you want to just clear the focus.
edited Oct 22 '17 at 5:09
Keet Sugathadasa
717621
717621
answered Oct 12 '09 at 19:02
KonkloneKonklone
2,7901926
2,7901926
That sounds promising, but at what point in the Activity lifecycle should it be called? If I call it in onCreate(), the EditText still has focus. Should it be called in onResume() or some other location? Thanks!
– Mark
Oct 12 '09 at 23:36
8
I combined the accepted answer with this answer. I calledmyEditText.clearFocus(); myDummyLinearLayout.requestFocus();
in theonResume
of the Activity. This ensured the EditText didn't keep the focus when the phone was rotated.
– teedyay
Oct 14 '10 at 21:02
add a comment |
That sounds promising, but at what point in the Activity lifecycle should it be called? If I call it in onCreate(), the EditText still has focus. Should it be called in onResume() or some other location? Thanks!
– Mark
Oct 12 '09 at 23:36
8
I combined the accepted answer with this answer. I calledmyEditText.clearFocus(); myDummyLinearLayout.requestFocus();
in theonResume
of the Activity. This ensured the EditText didn't keep the focus when the phone was rotated.
– teedyay
Oct 14 '10 at 21:02
That sounds promising, but at what point in the Activity lifecycle should it be called? If I call it in onCreate(), the EditText still has focus. Should it be called in onResume() or some other location? Thanks!
– Mark
Oct 12 '09 at 23:36
That sounds promising, but at what point in the Activity lifecycle should it be called? If I call it in onCreate(), the EditText still has focus. Should it be called in onResume() or some other location? Thanks!
– Mark
Oct 12 '09 at 23:36
8
8
I combined the accepted answer with this answer. I called
myEditText.clearFocus(); myDummyLinearLayout.requestFocus();
in the onResume
of the Activity. This ensured the EditText didn't keep the focus when the phone was rotated.– teedyay
Oct 14 '10 at 21:02
I combined the accepted answer with this answer. I called
myEditText.clearFocus(); myDummyLinearLayout.requestFocus();
in the onResume
of the Activity. This ensured the EditText didn't keep the focus when the phone was rotated.– teedyay
Oct 14 '10 at 21:02
add a comment |
I had tried several answers individually but the focus is still at the EditText. I only managed to solve it by using two of the below solution together.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
( Reference from Silver https://stackoverflow.com/a/8639921/15695 )
and remove
<requestFocus />
at EditText
( Reference from floydaddict https://stackoverflow.com/a/9681809 )
1
I had to add edittext.clearFocus() in addition to the above to get it working :)
– Nav
Sep 9 '14 at 13:12
add a comment |
I had tried several answers individually but the focus is still at the EditText. I only managed to solve it by using two of the below solution together.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
( Reference from Silver https://stackoverflow.com/a/8639921/15695 )
and remove
<requestFocus />
at EditText
( Reference from floydaddict https://stackoverflow.com/a/9681809 )
1
I had to add edittext.clearFocus() in addition to the above to get it working :)
– Nav
Sep 9 '14 at 13:12
add a comment |
I had tried several answers individually but the focus is still at the EditText. I only managed to solve it by using two of the below solution together.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
( Reference from Silver https://stackoverflow.com/a/8639921/15695 )
and remove
<requestFocus />
at EditText
( Reference from floydaddict https://stackoverflow.com/a/9681809 )
I had tried several answers individually but the focus is still at the EditText. I only managed to solve it by using two of the below solution together.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
( Reference from Silver https://stackoverflow.com/a/8639921/15695 )
and remove
<requestFocus />
at EditText
( Reference from floydaddict https://stackoverflow.com/a/9681809 )
edited Oct 10 '18 at 5:49
FullStackDeveloper
556626
556626
answered Nov 28 '12 at 10:32
Lee Yi HongLee Yi Hong
9601017
9601017
1
I had to add edittext.clearFocus() in addition to the above to get it working :)
– Nav
Sep 9 '14 at 13:12
add a comment |
1
I had to add edittext.clearFocus() in addition to the above to get it working :)
– Nav
Sep 9 '14 at 13:12
1
1
I had to add edittext.clearFocus() in addition to the above to get it working :)
– Nav
Sep 9 '14 at 13:12
I had to add edittext.clearFocus() in addition to the above to get it working :)
– Nav
Sep 9 '14 at 13:12
add a comment |
None of this solutions worked for me. The way I fix the autofocus was:
<activity android:name=".android.InviteFriendsActivity" android:windowSoftInputMode="adjustPan">
<intent-filter >
</intent-filter>
</activity>
2
android:windowSoftInputMode="adjustPan" on any activity in the Android Manifest
– rallat
Jun 7 '12 at 8:33
add a comment |
None of this solutions worked for me. The way I fix the autofocus was:
<activity android:name=".android.InviteFriendsActivity" android:windowSoftInputMode="adjustPan">
<intent-filter >
</intent-filter>
</activity>
2
android:windowSoftInputMode="adjustPan" on any activity in the Android Manifest
– rallat
Jun 7 '12 at 8:33
add a comment |
None of this solutions worked for me. The way I fix the autofocus was:
<activity android:name=".android.InviteFriendsActivity" android:windowSoftInputMode="adjustPan">
<intent-filter >
</intent-filter>
</activity>
None of this solutions worked for me. The way I fix the autofocus was:
<activity android:name=".android.InviteFriendsActivity" android:windowSoftInputMode="adjustPan">
<intent-filter >
</intent-filter>
</activity>
answered Nov 30 '11 at 16:37
rallatrallat
1,0751916
1,0751916
2
android:windowSoftInputMode="adjustPan" on any activity in the Android Manifest
– rallat
Jun 7 '12 at 8:33
add a comment |
2
android:windowSoftInputMode="adjustPan" on any activity in the Android Manifest
– rallat
Jun 7 '12 at 8:33
2
2
android:windowSoftInputMode="adjustPan" on any activity in the Android Manifest
– rallat
Jun 7 '12 at 8:33
android:windowSoftInputMode="adjustPan" on any activity in the Android Manifest
– rallat
Jun 7 '12 at 8:33
add a comment |
Simple solution:
In AndroidManifest
in Activity
tag use
android:windowSoftInputMode="stateAlwaysHidden"
6
Strictly speaking, this does not solve the issue. the OP said: "I don't want any control to have input focus at startup." Your solution only hides the keyboard, theres a sublte difference.
– katzenhut
Sep 4 '14 at 10:04
@katzenhut yep, thats my issue with this answer exactly. Focusing on my edittext opens up a PlaceAutoComplete activity, so this answer is incomplete
– Zach
Jan 15 '17 at 6:48
This answer would be complete if the question was: How do I always ensure my activity never shows a keyboard. Which is not.
– Martin Marconcini
Mar 21 '18 at 0:13
add a comment |
Simple solution:
In AndroidManifest
in Activity
tag use
android:windowSoftInputMode="stateAlwaysHidden"
6
Strictly speaking, this does not solve the issue. the OP said: "I don't want any control to have input focus at startup." Your solution only hides the keyboard, theres a sublte difference.
– katzenhut
Sep 4 '14 at 10:04
@katzenhut yep, thats my issue with this answer exactly. Focusing on my edittext opens up a PlaceAutoComplete activity, so this answer is incomplete
– Zach
Jan 15 '17 at 6:48
This answer would be complete if the question was: How do I always ensure my activity never shows a keyboard. Which is not.
– Martin Marconcini
Mar 21 '18 at 0:13
add a comment |
Simple solution:
In AndroidManifest
in Activity
tag use
android:windowSoftInputMode="stateAlwaysHidden"
Simple solution:
In AndroidManifest
in Activity
tag use
android:windowSoftInputMode="stateAlwaysHidden"
edited Sep 27 '16 at 10:05
Piyush
21.9k63167
21.9k63167
answered Mar 17 '14 at 15:42
Sergey ShelegSergey Sheleg
57546
57546
6
Strictly speaking, this does not solve the issue. the OP said: "I don't want any control to have input focus at startup." Your solution only hides the keyboard, theres a sublte difference.
– katzenhut
Sep 4 '14 at 10:04
@katzenhut yep, thats my issue with this answer exactly. Focusing on my edittext opens up a PlaceAutoComplete activity, so this answer is incomplete
– Zach
Jan 15 '17 at 6:48
This answer would be complete if the question was: How do I always ensure my activity never shows a keyboard. Which is not.
– Martin Marconcini
Mar 21 '18 at 0:13
add a comment |
6
Strictly speaking, this does not solve the issue. the OP said: "I don't want any control to have input focus at startup." Your solution only hides the keyboard, theres a sublte difference.
– katzenhut
Sep 4 '14 at 10:04
@katzenhut yep, thats my issue with this answer exactly. Focusing on my edittext opens up a PlaceAutoComplete activity, so this answer is incomplete
– Zach
Jan 15 '17 at 6:48
This answer would be complete if the question was: How do I always ensure my activity never shows a keyboard. Which is not.
– Martin Marconcini
Mar 21 '18 at 0:13
6
6
Strictly speaking, this does not solve the issue. the OP said: "I don't want any control to have input focus at startup." Your solution only hides the keyboard, theres a sublte difference.
– katzenhut
Sep 4 '14 at 10:04
Strictly speaking, this does not solve the issue. the OP said: "I don't want any control to have input focus at startup." Your solution only hides the keyboard, theres a sublte difference.
– katzenhut
Sep 4 '14 at 10:04
@katzenhut yep, thats my issue with this answer exactly. Focusing on my edittext opens up a PlaceAutoComplete activity, so this answer is incomplete
– Zach
Jan 15 '17 at 6:48
@katzenhut yep, thats my issue with this answer exactly. Focusing on my edittext opens up a PlaceAutoComplete activity, so this answer is incomplete
– Zach
Jan 15 '17 at 6:48
This answer would be complete if the question was: How do I always ensure my activity never shows a keyboard. Which is not.
– Martin Marconcini
Mar 21 '18 at 0:13
This answer would be complete if the question was: How do I always ensure my activity never shows a keyboard. Which is not.
– Martin Marconcini
Mar 21 '18 at 0:13
add a comment |
You can just set "focusable" and "focusable in touch mode" to value true on the first TextView
of the layout
. In this way when the activity starts the TextView
will be focused but , due to its nature, you will see nothing focused on the screen and ,of course, there will be no keyboard displayed...
Does not work at all!
– kpsfoo
Jun 5 '14 at 17:00
works perfectly for me, only i set the value on the outmost layout in my activity and not to the first textview
– Maverick1st
Jan 13 '15 at 17:45
add a comment |
You can just set "focusable" and "focusable in touch mode" to value true on the first TextView
of the layout
. In this way when the activity starts the TextView
will be focused but , due to its nature, you will see nothing focused on the screen and ,of course, there will be no keyboard displayed...
Does not work at all!
– kpsfoo
Jun 5 '14 at 17:00
works perfectly for me, only i set the value on the outmost layout in my activity and not to the first textview
– Maverick1st
Jan 13 '15 at 17:45
add a comment |
You can just set "focusable" and "focusable in touch mode" to value true on the first TextView
of the layout
. In this way when the activity starts the TextView
will be focused but , due to its nature, you will see nothing focused on the screen and ,of course, there will be no keyboard displayed...
You can just set "focusable" and "focusable in touch mode" to value true on the first TextView
of the layout
. In this way when the activity starts the TextView
will be focused but , due to its nature, you will see nothing focused on the screen and ,of course, there will be no keyboard displayed...
edited Sep 27 '13 at 18:13
bofredo
2,06952845
2,06952845
answered Nov 6 '11 at 16:49
ZeusZeus
32132
32132
Does not work at all!
– kpsfoo
Jun 5 '14 at 17:00
works perfectly for me, only i set the value on the outmost layout in my activity and not to the first textview
– Maverick1st
Jan 13 '15 at 17:45
add a comment |
Does not work at all!
– kpsfoo
Jun 5 '14 at 17:00
works perfectly for me, only i set the value on the outmost layout in my activity and not to the first textview
– Maverick1st
Jan 13 '15 at 17:45
Does not work at all!
– kpsfoo
Jun 5 '14 at 17:00
Does not work at all!
– kpsfoo
Jun 5 '14 at 17:00
works perfectly for me, only i set the value on the outmost layout in my activity and not to the first textview
– Maverick1st
Jan 13 '15 at 17:45
works perfectly for me, only i set the value on the outmost layout in my activity and not to the first textview
– Maverick1st
Jan 13 '15 at 17:45
add a comment |
The following worked for me in Manifest
. Write ,
<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
The following worked for me in Manifest
. Write ,
<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
The following worked for me in Manifest
. Write ,
<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
The following worked for me in Manifest
. Write ,
<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
edited Sep 27 '16 at 10:05
Piyush
21.9k63167
21.9k63167
answered Apr 8 '14 at 11:48
Babar SanahBabar Sanah
428513
428513
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
1
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
Simplest answer, just add this in parent layout of the XML.
android:focusable="true"
android:focusableInTouchMode="true"
worked perfectly for me. one more thing to note is, dont add these lines to scroll view. It wont work in scroll view. But worked perfectly with linear layout.
– Karthic Srinivasan
Dec 29 '18 at 14:24
add a comment |
Simplest answer, just add this in parent layout of the XML.
android:focusable="true"
android:focusableInTouchMode="true"
worked perfectly for me. one more thing to note is, dont add these lines to scroll view. It wont work in scroll view. But worked perfectly with linear layout.
– Karthic Srinivasan
Dec 29 '18 at 14:24
add a comment |
Simplest answer, just add this in parent layout of the XML.
android:focusable="true"
android:focusableInTouchMode="true"
Simplest answer, just add this in parent layout of the XML.
android:focusable="true"
android:focusableInTouchMode="true"
answered Apr 18 '18 at 6:14
Rishabh SaxenaRishabh Saxena
555821
555821
worked perfectly for me. one more thing to note is, dont add these lines to scroll view. It wont work in scroll view. But worked perfectly with linear layout.
– Karthic Srinivasan
Dec 29 '18 at 14:24
add a comment |
worked perfectly for me. one more thing to note is, dont add these lines to scroll view. It wont work in scroll view. But worked perfectly with linear layout.
– Karthic Srinivasan
Dec 29 '18 at 14:24
worked perfectly for me. one more thing to note is, dont add these lines to scroll view. It wont work in scroll view. But worked perfectly with linear layout.
– Karthic Srinivasan
Dec 29 '18 at 14:24
worked perfectly for me. one more thing to note is, dont add these lines to scroll view. It wont work in scroll view. But worked perfectly with linear layout.
– Karthic Srinivasan
Dec 29 '18 at 14:24
add a comment |
I needed to clear focus from all fields programmatically. I just added the following two statements to my main layout definition.
myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);
That's it. Fixed my problem instantly. Thanks, Silver, for pointing me in the right direction.
add a comment |
I needed to clear focus from all fields programmatically. I just added the following two statements to my main layout definition.
myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);
That's it. Fixed my problem instantly. Thanks, Silver, for pointing me in the right direction.
add a comment |
I needed to clear focus from all fields programmatically. I just added the following two statements to my main layout definition.
myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);
That's it. Fixed my problem instantly. Thanks, Silver, for pointing me in the right direction.
I needed to clear focus from all fields programmatically. I just added the following two statements to my main layout definition.
myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);
That's it. Fixed my problem instantly. Thanks, Silver, for pointing me in the right direction.
answered Sep 27 '12 at 23:35
jakeneffjakeneff
36846
36846
add a comment |
add a comment |
Add android:windowSoftInputMode="stateAlwaysHidden"
in the activity tag of the Manifest.xml
file.
Source
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
Add android:windowSoftInputMode="stateAlwaysHidden"
in the activity tag of the Manifest.xml
file.
Source
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
Add android:windowSoftInputMode="stateAlwaysHidden"
in the activity tag of the Manifest.xml
file.
Source
Add android:windowSoftInputMode="stateAlwaysHidden"
in the activity tag of the Manifest.xml
file.
Source
edited Sep 27 '16 at 10:04
Piyush
21.9k63167
21.9k63167
answered Jun 24 '15 at 9:19
prgmrDevprgmrDev
5631617
5631617
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
1
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
If you have another view on your activity like a ListView
, you can also do:
ListView.requestFocus();
in your onResume()
to grab focus from the editText
.
I know this question has been answered but just providing an alternative solution that worked for me :)
add a comment |
If you have another view on your activity like a ListView
, you can also do:
ListView.requestFocus();
in your onResume()
to grab focus from the editText
.
I know this question has been answered but just providing an alternative solution that worked for me :)
add a comment |
If you have another view on your activity like a ListView
, you can also do:
ListView.requestFocus();
in your onResume()
to grab focus from the editText
.
I know this question has been answered but just providing an alternative solution that worked for me :)
If you have another view on your activity like a ListView
, you can also do:
ListView.requestFocus();
in your onResume()
to grab focus from the editText
.
I know this question has been answered but just providing an alternative solution that worked for me :)
edited Sep 27 '13 at 18:11
bofredo
2,06952845
2,06952845
answered Mar 7 '11 at 19:37
SidSid
8,82953157
8,82953157
add a comment |
add a comment |
Try this before your first editable field:
<TextView
android:id="@+id/dummyfocus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/foo"
/>
----
findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();
add a comment |
Try this before your first editable field:
<TextView
android:id="@+id/dummyfocus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/foo"
/>
----
findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();
add a comment |
Try this before your first editable field:
<TextView
android:id="@+id/dummyfocus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/foo"
/>
----
findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();
Try this before your first editable field:
<TextView
android:id="@+id/dummyfocus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/foo"
/>
----
findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();
edited Jun 12 '11 at 0:41
user557219
answered Jun 11 '11 at 21:17
Jack SlaterJack Slater
18113
18113
add a comment |
add a comment |
Add following in onCreate
method:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
Add following in onCreate
method:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
Add following in onCreate
method:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Add following in onCreate
method:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
edited Sep 27 '16 at 10:04
Piyush
21.9k63167
21.9k63167
answered Dec 21 '15 at 15:33
Vishal RajVishal Raj
1,42911232
1,42911232
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
1
1
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:05
add a comment |
Being that I don't like to pollute the XML with something that is related to functionality, I created this method that "transparently" steals the focus from the first focusable view and then makes sure to remove itself when necessary!
public static View preventInitialFocus(final Activity activity)
{
final ViewGroup content = (ViewGroup)activity.findViewById(android.R.id.content);
final View root = content.getChildAt(0);
if (root == null) return null;
final View focusDummy = new View(activity);
final View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View view, boolean b)
{
view.setOnFocusChangeListener(null);
content.removeView(focusDummy);
}
};
focusDummy.setFocusable(true);
focusDummy.setFocusableInTouchMode(true);
content.addView(focusDummy, 0, new LinearLayout.LayoutParams(0, 0));
if (root instanceof ViewGroup)
{
final ViewGroup _root = (ViewGroup)root;
for (int i = 1, children = _root.getChildCount(); i < children; i++)
{
final View child = _root.getChildAt(i);
if (child.isFocusable() || child.isFocusableInTouchMode())
{
child.setOnFocusChangeListener(onFocusChangeListener);
break;
}
}
}
else if (root.isFocusable() || root.isFocusableInTouchMode())
root.setOnFocusChangeListener(onFocusChangeListener);
return focusDummy;
}
add a comment |
Being that I don't like to pollute the XML with something that is related to functionality, I created this method that "transparently" steals the focus from the first focusable view and then makes sure to remove itself when necessary!
public static View preventInitialFocus(final Activity activity)
{
final ViewGroup content = (ViewGroup)activity.findViewById(android.R.id.content);
final View root = content.getChildAt(0);
if (root == null) return null;
final View focusDummy = new View(activity);
final View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View view, boolean b)
{
view.setOnFocusChangeListener(null);
content.removeView(focusDummy);
}
};
focusDummy.setFocusable(true);
focusDummy.setFocusableInTouchMode(true);
content.addView(focusDummy, 0, new LinearLayout.LayoutParams(0, 0));
if (root instanceof ViewGroup)
{
final ViewGroup _root = (ViewGroup)root;
for (int i = 1, children = _root.getChildCount(); i < children; i++)
{
final View child = _root.getChildAt(i);
if (child.isFocusable() || child.isFocusableInTouchMode())
{
child.setOnFocusChangeListener(onFocusChangeListener);
break;
}
}
}
else if (root.isFocusable() || root.isFocusableInTouchMode())
root.setOnFocusChangeListener(onFocusChangeListener);
return focusDummy;
}
add a comment |
Being that I don't like to pollute the XML with something that is related to functionality, I created this method that "transparently" steals the focus from the first focusable view and then makes sure to remove itself when necessary!
public static View preventInitialFocus(final Activity activity)
{
final ViewGroup content = (ViewGroup)activity.findViewById(android.R.id.content);
final View root = content.getChildAt(0);
if (root == null) return null;
final View focusDummy = new View(activity);
final View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View view, boolean b)
{
view.setOnFocusChangeListener(null);
content.removeView(focusDummy);
}
};
focusDummy.setFocusable(true);
focusDummy.setFocusableInTouchMode(true);
content.addView(focusDummy, 0, new LinearLayout.LayoutParams(0, 0));
if (root instanceof ViewGroup)
{
final ViewGroup _root = (ViewGroup)root;
for (int i = 1, children = _root.getChildCount(); i < children; i++)
{
final View child = _root.getChildAt(i);
if (child.isFocusable() || child.isFocusableInTouchMode())
{
child.setOnFocusChangeListener(onFocusChangeListener);
break;
}
}
}
else if (root.isFocusable() || root.isFocusableInTouchMode())
root.setOnFocusChangeListener(onFocusChangeListener);
return focusDummy;
}
Being that I don't like to pollute the XML with something that is related to functionality, I created this method that "transparently" steals the focus from the first focusable view and then makes sure to remove itself when necessary!
public static View preventInitialFocus(final Activity activity)
{
final ViewGroup content = (ViewGroup)activity.findViewById(android.R.id.content);
final View root = content.getChildAt(0);
if (root == null) return null;
final View focusDummy = new View(activity);
final View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View view, boolean b)
{
view.setOnFocusChangeListener(null);
content.removeView(focusDummy);
}
};
focusDummy.setFocusable(true);
focusDummy.setFocusableInTouchMode(true);
content.addView(focusDummy, 0, new LinearLayout.LayoutParams(0, 0));
if (root instanceof ViewGroup)
{
final ViewGroup _root = (ViewGroup)root;
for (int i = 1, children = _root.getChildCount(); i < children; i++)
{
final View child = _root.getChildAt(i);
if (child.isFocusable() || child.isFocusableInTouchMode())
{
child.setOnFocusChangeListener(onFocusChangeListener);
break;
}
}
}
else if (root.isFocusable() || root.isFocusableInTouchMode())
root.setOnFocusChangeListener(onFocusChangeListener);
return focusDummy;
}
answered Apr 24 '13 at 13:26
TakhionTakhion
2,2221828
2,2221828
add a comment |
add a comment |
Late, but maybe helpful. Create a dummy EditText at the top of your layout then call myDummyEditText.requestFocus()
in onCreate()
<EditText android:id="@+id/dummyEditTextFocus"
android:layout_width="0px"
android:layout_height="0px" />
That seems to behave as I expect. No need to handle configuration changes, etc. I needed this for an Activity with a lengthy TextView (instructions).
Why not just do this with the root view itself?
– CJBS
Dec 1 '15 at 18:56
add a comment |
Late, but maybe helpful. Create a dummy EditText at the top of your layout then call myDummyEditText.requestFocus()
in onCreate()
<EditText android:id="@+id/dummyEditTextFocus"
android:layout_width="0px"
android:layout_height="0px" />
That seems to behave as I expect. No need to handle configuration changes, etc. I needed this for an Activity with a lengthy TextView (instructions).
Why not just do this with the root view itself?
– CJBS
Dec 1 '15 at 18:56
add a comment |
Late, but maybe helpful. Create a dummy EditText at the top of your layout then call myDummyEditText.requestFocus()
in onCreate()
<EditText android:id="@+id/dummyEditTextFocus"
android:layout_width="0px"
android:layout_height="0px" />
That seems to behave as I expect. No need to handle configuration changes, etc. I needed this for an Activity with a lengthy TextView (instructions).
Late, but maybe helpful. Create a dummy EditText at the top of your layout then call myDummyEditText.requestFocus()
in onCreate()
<EditText android:id="@+id/dummyEditTextFocus"
android:layout_width="0px"
android:layout_height="0px" />
That seems to behave as I expect. No need to handle configuration changes, etc. I needed this for an Activity with a lengthy TextView (instructions).
answered Feb 13 '11 at 1:58
JimJim
13912
13912
Why not just do this with the root view itself?
– CJBS
Dec 1 '15 at 18:56
add a comment |
Why not just do this with the root view itself?
– CJBS
Dec 1 '15 at 18:56
Why not just do this with the root view itself?
– CJBS
Dec 1 '15 at 18:56
Why not just do this with the root view itself?
– CJBS
Dec 1 '15 at 18:56
add a comment |
Yeah I did the same thing - create a 'dummy' linear layout which gets initial focus. Furthermore, I set the 'next' focus IDs so the user can't focus it any more after scrolling once:
<LinearLayout 'dummy'>
<EditText et>
dummy.setNextFocusDownId(et.getId());
dummy.setNextFocusUpId(et.getId());
et.setNextFocusUpId(et.getId());
a lot of work just to get rid of focus on a view..
Thanks
add a comment |
Yeah I did the same thing - create a 'dummy' linear layout which gets initial focus. Furthermore, I set the 'next' focus IDs so the user can't focus it any more after scrolling once:
<LinearLayout 'dummy'>
<EditText et>
dummy.setNextFocusDownId(et.getId());
dummy.setNextFocusUpId(et.getId());
et.setNextFocusUpId(et.getId());
a lot of work just to get rid of focus on a view..
Thanks
add a comment |
Yeah I did the same thing - create a 'dummy' linear layout which gets initial focus. Furthermore, I set the 'next' focus IDs so the user can't focus it any more after scrolling once:
<LinearLayout 'dummy'>
<EditText et>
dummy.setNextFocusDownId(et.getId());
dummy.setNextFocusUpId(et.getId());
et.setNextFocusUpId(et.getId());
a lot of work just to get rid of focus on a view..
Thanks
Yeah I did the same thing - create a 'dummy' linear layout which gets initial focus. Furthermore, I set the 'next' focus IDs so the user can't focus it any more after scrolling once:
<LinearLayout 'dummy'>
<EditText et>
dummy.setNextFocusDownId(et.getId());
dummy.setNextFocusUpId(et.getId());
et.setNextFocusUpId(et.getId());
a lot of work just to get rid of focus on a view..
Thanks
edited Sep 27 '13 at 18:13
bofredo
2,06952845
2,06952845
answered Oct 23 '09 at 14:23
markmark
8373913
8373913
add a comment |
add a comment |
Write this line in your Parent Layout...
android:focusableInTouchMode="true"
add a comment |
Write this line in your Parent Layout...
android:focusableInTouchMode="true"
add a comment |
Write this line in your Parent Layout...
android:focusableInTouchMode="true"
Write this line in your Parent Layout...
android:focusableInTouchMode="true"
answered Jul 26 '17 at 9:52
Vishal VaishnavVishal Vaishnav
2,0212932
2,0212932
add a comment |
add a comment |
For me, what worked on all devices is this:
<!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->
<View
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
Just put this as a view before the problematic focused view, and that's it.
add a comment |
For me, what worked on all devices is this:
<!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->
<View
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
Just put this as a view before the problematic focused view, and that's it.
add a comment |
For me, what worked on all devices is this:
<!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->
<View
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
Just put this as a view before the problematic focused view, and that's it.
For me, what worked on all devices is this:
<!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->
<View
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
Just put this as a view before the problematic focused view, and that's it.
answered May 14 '14 at 8:02
android developerandroid developer
54.4k101472880
54.4k101472880
add a comment |
add a comment |
This is the perfect and most easiest solution.I always use this in my app.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:07
add a comment |
This is the perfect and most easiest solution.I always use this in my app.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:07
add a comment |
This is the perfect and most easiest solution.I always use this in my app.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
This is the perfect and most easiest solution.I always use this in my app.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
answered Mar 31 '15 at 9:27
user4728480
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:07
add a comment |
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:07
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:07
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:07
add a comment |
The simplest thing I did is to set focus on another view in onCreate:
myView.setFocusableInTouchMode(true);
myView.requestFocus();
This stopped the soft keyboard coming up and there was no cursor flashing in the EditText.
add a comment |
The simplest thing I did is to set focus on another view in onCreate:
myView.setFocusableInTouchMode(true);
myView.requestFocus();
This stopped the soft keyboard coming up and there was no cursor flashing in the EditText.
add a comment |
The simplest thing I did is to set focus on another view in onCreate:
myView.setFocusableInTouchMode(true);
myView.requestFocus();
This stopped the soft keyboard coming up and there was no cursor flashing in the EditText.
The simplest thing I did is to set focus on another view in onCreate:
myView.setFocusableInTouchMode(true);
myView.requestFocus();
This stopped the soft keyboard coming up and there was no cursor flashing in the EditText.
answered Jan 3 '13 at 6:57
LumisLumis
18.6k75462
18.6k75462
add a comment |
add a comment |
Write this code inside Manifest
file in the Activity
where you do not want to open the keyboard.
android:windowSoftInputMode="stateHidden"
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projectt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="24" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
**android:windowSoftInputMode="stateHidden"**
android:label="@string/app_name" >
</activity>
</application>
</manifest>
2
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:06
add a comment |
Write this code inside Manifest
file in the Activity
where you do not want to open the keyboard.
android:windowSoftInputMode="stateHidden"
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projectt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="24" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
**android:windowSoftInputMode="stateHidden"**
android:label="@string/app_name" >
</activity>
</application>
</manifest>
2
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:06
add a comment |
Write this code inside Manifest
file in the Activity
where you do not want to open the keyboard.
android:windowSoftInputMode="stateHidden"
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projectt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="24" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
**android:windowSoftInputMode="stateHidden"**
android:label="@string/app_name" >
</activity>
</application>
</manifest>
Write this code inside Manifest
file in the Activity
where you do not want to open the keyboard.
android:windowSoftInputMode="stateHidden"
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projectt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="24" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
**android:windowSoftInputMode="stateHidden"**
android:label="@string/app_name" >
</activity>
</application>
</manifest>
edited Mar 15 '17 at 7:21
Satan Pandeya
2,48531634
2,48531634
answered Sep 22 '16 at 14:04
Tarit RayTarit Ray
386418
386418
2
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:06
add a comment |
2
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:06
2
2
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:06
This doesn't un-focus the text field: it merely hides the keyboard. You'll still get a hint that's pushed out of the field, and any color state selectors will display the "focused=true" state.
– A. Rager
Nov 1 '16 at 18:06
add a comment |
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
style="@android:style/Widget.EditText"/>
add a comment |
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
style="@android:style/Widget.EditText"/>
add a comment |
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
style="@android:style/Widget.EditText"/>
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
style="@android:style/Widget.EditText"/>
edited Mar 31 '17 at 18:41
Satan Pandeya
2,48531634
2,48531634
answered Feb 14 '12 at 4:56
atulatul
7911
7911
add a comment |
add a comment |
At onCreate
of your Activity, just add use clearFocus()
on your EditText element.
For example,
edittext = (EditText) findViewById(R.id.edittext);
edittext.clearFocus();
And if you want to divert the focus to another element, use requestFocus()
on that.
For example,
button = (Button) findViewById(R.id.button);
button.requestFocus();
I tried this in oncreate, doesn't work. api 26.
– Jeffrey Liu
Aug 11 '17 at 17:57
add a comment |
At onCreate
of your Activity, just add use clearFocus()
on your EditText element.
For example,
edittext = (EditText) findViewById(R.id.edittext);
edittext.clearFocus();
And if you want to divert the focus to another element, use requestFocus()
on that.
For example,
button = (Button) findViewById(R.id.button);
button.requestFocus();
I tried this in oncreate, doesn't work. api 26.
– Jeffrey Liu
Aug 11 '17 at 17:57
add a comment |
At onCreate
of your Activity, just add use clearFocus()
on your EditText element.
For example,
edittext = (EditText) findViewById(R.id.edittext);
edittext.clearFocus();
And if you want to divert the focus to another element, use requestFocus()
on that.
For example,
button = (Button) findViewById(R.id.button);
button.requestFocus();
At onCreate
of your Activity, just add use clearFocus()
on your EditText element.
For example,
edittext = (EditText) findViewById(R.id.edittext);
edittext.clearFocus();
And if you want to divert the focus to another element, use requestFocus()
on that.
For example,
button = (Button) findViewById(R.id.button);
button.requestFocus();
answered May 23 '13 at 10:42
Compaq LE2202xCompaq LE2202x
96662854
96662854
I tried this in oncreate, doesn't work. api 26.
– Jeffrey Liu
Aug 11 '17 at 17:57
add a comment |
I tried this in oncreate, doesn't work. api 26.
– Jeffrey Liu
Aug 11 '17 at 17:57
I tried this in oncreate, doesn't work. api 26.
– Jeffrey Liu
Aug 11 '17 at 17:57
I tried this in oncreate, doesn't work. api 26.
– Jeffrey Liu
Aug 11 '17 at 17:57
add a comment |
You can achieve this by creating a dummy EditText
with layout width and height set to 0dp
, and request focus to that view.
Add the following code snippet in your xml layout:
<EditText
android:id="@+id/editText0"
android:layout_width="0dp"
android:layout_height="0dp"
android:hint="@string/dummy"
android:ems="10"
>
<requestFocus />
</EditText>
add a comment |
You can achieve this by creating a dummy EditText
with layout width and height set to 0dp
, and request focus to that view.
Add the following code snippet in your xml layout:
<EditText
android:id="@+id/editText0"
android:layout_width="0dp"
android:layout_height="0dp"
android:hint="@string/dummy"
android:ems="10"
>
<requestFocus />
</EditText>
add a comment |
You can achieve this by creating a dummy EditText
with layout width and height set to 0dp
, and request focus to that view.
Add the following code snippet in your xml layout:
<EditText
android:id="@+id/editText0"
android:layout_width="0dp"
android:layout_height="0dp"
android:hint="@string/dummy"
android:ems="10"
>
<requestFocus />
</EditText>
You can achieve this by creating a dummy EditText
with layout width and height set to 0dp
, and request focus to that view.
Add the following code snippet in your xml layout:
<EditText
android:id="@+id/editText0"
android:layout_width="0dp"
android:layout_height="0dp"
android:hint="@string/dummy"
android:ems="10"
>
<requestFocus />
</EditText>
edited Sep 27 '16 at 10:04
Piyush
21.9k63167
21.9k63167
answered Jun 13 '15 at 21:50
IshIsh
138212
138212
add a comment |
add a comment |
1 2
next
protected by Community♦ May 24 '13 at 12:27
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
112
Before you use any of the upvoted hacks below, check if you don't have the
<requestFocus />
line in your XML layout (more details in @floydaddict's answer). Eclipse's WYSIWYG editor adds it automatically. After removing this line, the problem disappeared for me.– DzinX
Jun 15 '12 at 10:31
removing just
<requestFocus />
did not work on Asus Nexus 7 with KitKat 4.4.4 but @Joe's answer works– user13267
Jan 29 '15 at 8:33
87
I just use android:focusableInTouchMode="true" on my parent layout and works for me.
– Herman
Feb 18 '15 at 16:50
The only one that worked for me was rallat's solution
– vivoconunxino
Sep 26 '15 at 18:48
@Herman your simple solution worked for me. I was using multiple widgets including radio buttons, check boxes, buttons and EditText. Setting the
focusableInTouchMode="true"
attribute for the parent view (in my case a LinearLayout) resolved the issue. Thank you.– prasad_
Jan 5 '18 at 5:23