Android : clear focus on edittext when activity starts - android

I've some settings page in my app. Once the activity gets starts directly it focus to edittext and i used following code to clear foucs.
<RelativeLayout
android:id="#+id/RequestFocusLayout"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
and in java code
RelativeLayout focuslayout = (RelativeLayout) findViewById(R.id.RequestFocusLayout);
focuslayout.requestFocus();
The above code is working fine when activity starts at first time and if same activity starts again, automatically edittext get focus.
Can anyone help me to solve this issue.

Actually, the first focusable view in the activity receives initial focus. If that happens to be your EditText, it will be initially focused.
If you don't want that, here's your options:
Focus another view
Programmatically identify what view you do want to give initial focus to.
#Override
protected void onStart() {
super.onStart();
findViewById( R.id.yourOtherViewId ).requestFocus();
}
Make an earlier view in your layout focusable
If you rather it appears as though "no view has initial focus" you could make the parent view group focusable. In the following example, I make my LinearLayout focusable by setting android:focusableInTouchMode="true":
<LinearLayout
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
...

If Come back from other activity edittext get focused.
put these line onStart() or on onReusme()
RelativeLayout focuslayout = (RelativeLayout) findViewById(R.id.RequestFocusLayout);
focuslayout.requestFocus();

If your EditText is a child of your RelativeLayout you can use android:descendantFocusability to request the focus:
<RelativeLayout
android:id="#+id/RequestFocusLayout"
android:focusable="true"
android:layout_width="0px"
android:layout_height="0px"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" />

I used the solution shown up in this page but it didn't work. Add this attribute to your activity tag into AndroidManifest:
android:windowSoftInputMode="stateHidden"
It works perfectly.

Me realy help only android:focusableInTouchMode="true" in my parent view group.

Put the code to remove the focus in your onStart() method and it should work fine.

In the layout XML file, specify an imeOption on your EditText:
android:imeOptions="actionGo"
Next, add an action listener to your EditText in the Activity's java file
mYourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);
return true;
}
return false;
}
});
Where mYourEditText is an EditText object

If on starting the Activity it focuses on EditText on the screen.
use
android:focusableInTouchMode="true"
in the parent containing that EditText. Like if EditText is inside a RelativeLayout use this line in the RelativeLayout.

Related

ClearFocus sets focus on the first view in LinearLayout

When clearFocus() on a LinearLayout is called, it sets focus on the first view in the Linearlayout. I am reading the android View document and it says
Note: When not in touch-mode, the framework will try to give focus to the first focusable View from the top after focus is cleared. Hence, if this View is the first from the top that can take focus, then all callbacks related to clearing focus will be invoked after which the framework will give focus to this view.
enter link description here
So, I checked IsInTouchMode on the LinearLayout before calling clearFocus, but IsInTouchMode is true.
I am wondering what is affecting this behaviour.
Use android:descendantFocusability="beforeDescendants" so that focus will be first on parent layout and then on their child/s.
Like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical" />
Let me know if it worked or not.

AppCompatEditText focus Issue

I have long form (LinearLayout as root) which consist of EditTexts and TextView.
Now Issue i am facing is : After clicking the Edit Text, if i clicking on textviews then the focus of an edit text is not clearing. Hence, screen scroll to that edit text due to focus.
I have tried solution - android:focusableInTouchMode="true" in parent layout but no luck.
I have attached an image showing an issue. I have clicked on consulting doctor field which is a TextView but focus is till on email Edit text.
Please help me with some solution. Thanks.
After clicking the Edit Text, if i clicking on textviews then the focus of an edit text is not clearing. Hence, screen scroll to that edit text due to focus.
Wrap your EditText inside FrameLayout as shown below
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
public static void hideSoftKeyboard (View view)
{
InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
//Call this method on textview click

Issue with Soft Keyboard and Edit Text

I have an Edit Text at the bottom of my app. I want to achieve that always when it gets a Touch Event, the Soft Keyboard appears scrolling the whole window to the top, so that my EditText remains visible.
The problem is that sometimes the window does not scroll at all and the Edit Text remains hidden behind the Soft Keyboard. The weird thing is that, in those moments, if I press anything or interact with other elements in the window, apparently it refreshes or something like that and then it scrolls correctly to the top, allowing me to see the Edit Text.
I have try different things and this is my end code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<my.Custom_Edit_Text
android:id="#+id/my_custom_edit_text"
style="#style/my_custom_edit_text_Style"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:maxLength="#integer/data_title_max_length"
android:text="#string/my_custom_edit_text_default"
android:selectAllOnFocus="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:cursorVisible="true"
android:inputType="text"/>
This is my Custom_Edit_Text code, where I handle the onTouch event:
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
requestFocus();
}
return false;
}
});
I have also set this attribute in the Manifest file:
android:windowSoftInputMode="stateHidden|adjustPan"
use this
android:configChanges="keyboardHidden|orientation"
and
android:windowSoftInputMode="adjustPan"
Let me know using both this are helpful to you or not and also use scrollview in the layout where your virtual keyboard is creating problem it also help a bit
put your whole layout which is distroting in a scrollview
This post gave me the clue to solve my problem https://stackoverflow.com/a/5989385/1382250
I just tried to change my Edit Text for a Text View and now it works perfectly (scrolls the window in all cases).

Android: Force EditText to remove focus? [duplicate]

This question already has answers here:
How to stop EditText from gaining focus when an activity starts in Android?
(54 answers)
Closed 7 years ago.
I would like to be able to remove the focus from the EditText. For example if the Keyboard appears, and the user hides it with the back button, I would like the focus and the cursor to disappear. How can it be done?
You can make cursor and focus disappear by
edittext.clearFocus();
But detect when the key board hide is a hard work.
You can add this to onCreate and it will hide the keyboard every time the Activity starts.
You can also programmatically change the focus to another item.
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Add LinearLayout before EditText in your XML.
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:layout_width="0px"
android:layout_height="0px" />
Or you can do this same thing by adding these lines to view before your 'EditText'.
<Button
android:id="#+id/btnSearch"
android:layout_width="50dp"
android:layout_height="50dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="Quick Search"
android:textColor="#fff"
android:textSize="13sp"
android:textStyle="bold" />
<EditText
android:id="#+id/edtSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:gravity="left"
android:hint="Name"
android:maxLines="1"
android:singleLine="true"
android:textColorHint="#color/blue"
android:textSize="13sp"
android:textStyle="bold" />
Remove focus but remain focusable:
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
EditText will lose focus, but can gain it again on a new touch event.
Add these two properties to your parent layout (ex: Linear Layout, Relative Layout)
android:focusable="true"
android:focusableInTouchMode="true"
It will do the trick :)
remove autofocus edittext android
It's working for me
Edit In the link they suggest to use LinearLayout, but simple View will work
<View
android:id="#+id/focus_thief"
android:layout_width="1dp"
android:layout_height="1dp"
android:focusable="true"
android:focusableInTouchMode="true" />
Then if this "thief" is placed at the top of the layout (to be first focusable item) calls to clearFocus() will work.
You can also include android:windowSoftInputMode="stateAlwaysHidden" in your manifest action section.
This is equivalent to :
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
but in XML way.
FYI, you can also hide the keyboard with codes:
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);
To hide the keyboard when activity starts.. write the following code in onCreate()..
InputMethodManager imm = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
To clear focus and remove cursor from edittext.....
editText.clearFocus();
editText.setCursorVisible(false);
try to use this one on your view
it worked for me:
<View
android:id="#+id/fucused"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"/>
Add to your parent layout where did you put your EditText this android:focusableInTouchMode="true"
you have to remove <requestFocus/>
if you don't use it and still the same problem
user LinearLayout as a parent and set
android:focusable="true"
android:focusableInTouchMode="true"
Hope it's help you.
This is my very first answer on SO, so don't be too harsh on me if there are mistakes. :D
There are few answers floating around the SO, but I feel the urge to post my complete solution cause this drove me nuts. I've grabbed bits and pieces from all around so forgive me if I don't give respective credits to everyone... :)
(I'll simplify my result cause my view has too many elements and I don't wanna spam with that and will try to make it as generic as possible...)
For your layout you need a parent your EditText and parent view defined something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lytContainer"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/etEditor"
android:inputType="number"
android:layout_gravity="center"
android:hint="#string/enter_your_text"
android:textColor="#android:color/darker_gray"
android:textSize="12dp"
android:textAlignment="center"
android:gravity="center"
android:clickable="true"/>
</LinearLayout>
So, I needed a few things here. I needed to have a Placeholder for my EditText - which is that -
android:hint="hint"
Also,
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
made it happen for EditText not to be focused on entering the Activity and later on in the Activity itself when setting it this setting helps so you can set onTouchListener on it to steal the focus away from EditText.
Now, in the Activity:
package com.at.keyboardhide;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnTouchListener{
private EditText getEditText;
private LinearLayout getLinearLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
setContentView(R.layout.keyboardmain);
getEditText = (EditText)findViewById(R.id.etEditor);
getLinearLayout = (LinearLayout)findViewById(R.id.lytContainer);
getLinearLayout.setOnTouchListener(this);
getEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Log.d("EDTA", "text was entered.");
getEditText.clearFocus();
imm.hideSoftInputFromWindow(barcodeNo.getWindowToken(), 0);
return true;
}
return false;
}
});
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if(v==getLinearLayout){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getEditText.getWindowToken(), 0);
getEditText.clearFocus();
return true;
}
return false;
}
}
Few of the answers for bits I found on this question page, and the part with the Activity solution I found on this blog. The rest I missed which I had to figure out myself was clearing focus on the EditText which I added to both inside the setOnEditorActionListener and onTouchLister for the parent view.
Hope this helps someone and saves their time. :)
Cheers,
Z.
In the comments you asked if another view can be focused instead of the EditText. Yes it can. Use .requestFocus() method for the view you want to be focused at the beginning (in onCreate() method)
Also focusing other view will cut out some amount of code. (code for hiding the keyboard for example)
I had the same problem. It made me more than crazy.
I had an extended Dialog with a ScrollView that had a TableLayout with extended LinearLayout that contained a SeekBar and a EditText.
The first EditText had always autofocus after showing the Dialog and after finishing editing the text over the keyboard the EditText still had the focus and the keyboard was still visible.
I tried nearly all solutions of this thread and none worked for me.
So here my simple solution: (text = EditText)
text.setOnEditorActionListener( new OnEditorActionListener( ){
public boolean onEditorAction( TextView v, int actionId, KeyEvent event ){
if( (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) ||
(actionId == EditorInfo.IME_ACTION_DONE) ){
text.clearFocus( );
InputMethodManager iMgr = null;
iMgr = (InputMethodManager)mContext.getSystemService( Context.INPUT_METHOD_SERVICE );
iMgr.hideSoftInputFromWindow( text.getWindowToken(), 0 );
}
return true;
}
});
By the way I didn't used any of the following snippets to solve it:
//setFocusableInTouchMode( true )
//setFocusable( true )
//setDescendantFocusability( ViewGroup.FOCUS_BEFORE_DESCENDANTS )
AND I didn't used a spacer item like a View with width and height of 1dp.
Hopefully it helps someone :D
editText.setFocusableInTouchMode(true)
The EditText will be able to get the focus when the user touch it.
When the main layout (activity, dialog, etc.) becomes visible the EditText doesn't automatically get the focus even though it is the first view in the layout.
You can also include android:windowSoftInputMode="stateAlwaysHidden" in your manifest action section.
This is equivalent to:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
You can avoid any focus on your elements by setting the attribute android:descendantFocusability of the parent element.
Here is an example:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/search__scroller"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ScrollView>
Here, the attribute android:descendantFocusability set to "blocksDescendants" is blocking the focus on the child elements.
You can find more info here.
check your xml file
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" >
**<requestFocus />**
</EditText>
//Remove **<requestFocus />** from xml

How to stop EditText from gaining focus when an activity starts in Android?

I have an Activity in Android, with two elements:
EditText
ListView
When my Activity starts, the EditText immediately has the input focus (flashing cursor). I don't want any control to have input focus at startup. I tried:
EditText.setSelected(false);
EditText.setFocusable(false);
No luck. How can I convince the EditText to not select itself when the Activity starts?
Adding the tags android:focusableInTouchMode="true" and android:focusable="true" to the parent layout (e.g. LinearLayout or ConstraintLayout) like in 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"/>
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 on the EditText? I don't really see an issue with the EditText having a focus on the 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).
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 focused 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 is 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.
The only solution I've found is:
Create a LinearLayout (I don't know if other kinds of Layout will work)
Set the attributes android:focusable="true" and android:focusableInTouchMode="true"
And the EditText won't get the focus after starting the activity
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>
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();
}
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.
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.
Late but simplest answer, just add this in the parent layout of the XML.
android:focusable="true"
android:focusableInTouchMode="true"
Upvote if it helped you! Happy Coding :)
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 )
None of these solutions worked for me. The way I fix the autofocus was:
<activity android:name=".android.InviteFriendsActivity"
android:windowSoftInputMode="adjustPan">
<intent-filter >
</intent-filter>
</activity>
Simple solution:
In AndroidManifest in Activity tag use
android:windowSoftInputMode="stateAlwaysHidden"
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...
The following worked for me in Manifest. Write,
<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
I needed to clearly focus on 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 android:windowSoftInputMode="stateAlwaysHidden" in the activity tag of the Manifest.xml file.
Source
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 :)
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 following in onCreate method:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Write this line in your Parent Layout...
android:focusableInTouchMode="true"
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;
}
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).
Yeah, I did the same thing - create a 'dummy' linear layout which gets the initial focus. Furthermore, I set the 'next' focus IDs so the user can't focus it anymore 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
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.
<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"/>
This is the perfect and easiest solution. I always use this in my app.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
The simplest thing I did is to set focus on another view in onCreate:
myView.setFocusableInTouchMode(true);
myView.requestFocus();
This stopped the soft keyboard from coming up and there was no cursor flashing in the EditText.
Write this code inside the 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.project"
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>
The easiest way to hide the keyboard is using setSoftInputMode
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
or you can use InputMethodManager and hide the keyboard like this.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
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();

Categories

Resources