How to remove focus from single editText

Android

Android Problem Overview


In my application I have a single EditText together with some TextViews, button and a spinner. My EditText receives focus since it is the only focusable view in this activity, I believe. My EditText shows with an orange border and cursor on the field.

Now I would like to remove the focus from this field (I don't want the cursor and border to show). Is there a way to do this?

I have been able to focus on the button by doing button.seFocusableInTouchMode() and button.requestFocus(). But this highlights the button and is obviously not what I want.

Android Solutions


Solution 1 - Android

new to android.. tried

getWindow().getDecorView().clearFocus();

it works for me..

just to add .. your layout should have:

 android:focusable="true"
 android:focusableInTouchMode="true"

Solution 2 - Android

Did you try to use old good View.clearFocus()

Solution 3 - Android

check this question and the selected answer: https://stackoverflow.com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup It's ugly but it works, and as far as I know there's no better solution.

Solution 4 - Android

I will try to explain how to remove the focus (flashing cursor) from EditText view with some more details and understanding. Usually this line of code should work

editText.clearFocus()

but it could be situation when the editText still has the focus, and this is happening because clearFocus() method is trying to set the focus back to the first focusable view in the activity/fragment layout.

So if you have only one view in the activity which is focusable, and this usually will be your EditText view, then clearFocus() will set the focus again to that view, and for you it will look that clearFocus() is not working. Remember that EditText views are focusable(true) by default so if you have only one EditText view inside your layout it will aways get the focus on the screen. In this case your solution will be to find the parent view(some layout , ex LinearLayout, Framelayout) inside your layout file and set to it this xml code

android:focusable="true"
android:focusableInTouchMode="true"

After that when you execute editText.clearFocus() the parent view inside your layout will accept the focus and your editText will be clear of the focus.

I hope this will help somebody to understand how clearFocus() is working.

Solution 5 - Android

if Edittext parent layout is Linear then add

 android:focusable="true" 
 android:focusableInTouchMode="true"

like below

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:focusable="true"
            android:focusableInTouchMode="true">

           <EditText/>
          ............

when Edittext parent layout is Relative then

  android:descendantFocusability="beforeDescendants"
  android:focusableInTouchMode="true"

like

  <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:descendantFocusability="beforeDescendants"
            android:focusableInTouchMode="true">

           <EditText/>
          ............

Solution 6 - Android

I know is too late, but for somebody whit the same need editText.setFocusable(false) si what you are looking for.

Solution 7 - Android

Use the attached code to give the focus to "someone else", this is OK if you have a view where you simply want to dismiss the keyboard and release the focus, you don't really care about who gets it.

Use like this: FocusHelper.releaseFocus(viewToReleaseFocusFrom)

public class FocusHelper {
	public static void releaseFocus(View view) {
		ViewParent parent = view.getParent();
		ViewGroup group = null;
		View child = null;
		while (parent != null) {
			if (parent instanceof ViewGroup) {
				group = (ViewGroup) parent;
				for (int i = 0; i < group.getChildCount(); i++) {
					child = group.getChildAt(i);
					if(child != view && child.isFocusable())
						child.requestFocus();
				}
			}
			parent = parent.getParent();
		}
	}
}

Doc: The method traverses from the child view and up the view tree and looks for the first child to give focus to.

Edit: You can also use the API for this:

View focusableView = v.focusSearch(View.FOCUS_DOWN);
if(focusableView != null) focusableView.requestFocus();

Solution 8 - Android

i had a similar problem with the editText, which gained focus since the activity was started. this problem i fixed easily like this:

you add this piece of code into the layout that contains the editText in xml:

    android:id="@+id/linearlayout" 
    android:focusableInTouchMode="true"

dont forget the android:id, without it i've got an error.

the other problem i had with the editText is that once it gain the first focus, the focus never disappeared. this is a piece of my code in java, it has an editText and a button that captures the text in the editText:

    editText=(EditText) findViewById(R.id.et1);
	tvhome= (TextView)findViewById(R.id.tv_home);
	etBtn= (Button) findViewById(R.id.btn_homeadd);
	etBtn.setOnClickListener(new View.OnClickListener() 
	{	
		@Override
		public void onClick(View v)
		{
			tvhome.setText( editText.getText().toString() );
			
			//** this code is for hiding the keyboard after pressing the button
			View view = Settings.this.getCurrentFocus();
			if (view != null) 
			{  
			    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
			    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
			}
			//**
			
			editText.getText().clear();//clears the text
			editText.setFocusable(false);//disables the focus of the editText 
			Log.i("onCreate().Button.onClickListener()", "et.isfocused= "+editText.isFocused());
		}
	});
	editText.setOnClickListener(new View.OnClickListener() 
	{
		@Override
		public void onClick(View v) 
		{
			if(v.getId() == R.id.et1)
			{
				v.setFocusableInTouchMode(true);// when the editText is clicked it will gain focus again
				
				//** this code is for enabling the keyboard at the first click on the editText
				if(v.isFocused())//the code is optional, because at the second click the keyboard shows by itself
				{
					InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
			        imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
				}
				//**
				
				Log.i("onCreate().EditText.onClickListener()", "et.isfocused= "+v.isFocused());
			}
			else
				Log.i("onCreate().EditText.onClickListener()", "the listener did'nt consume the event");
		}
	});

hope it will help to some of you!

Solution 9 - Android

For me this worked

Add these attributes to your EditText

android:focusable="true"
android:focusableInTouchMode="true"

After this in your code you can simply write

editText.clearFocus()

Solution 10 - Android

Just find another view and give it focus instead.

var refresher = FindViewById<MvxSwipeRefreshLayout>(Resource.Id.refresher);

refresher.RequestFocus();

Solution 11 - Android

I've tryed much to clear focus of an edit text. clearfocus() and focusable and other things never worked for me. So I came up with the idea of letting a fake edittext gain focus:

<LinearLayout
    ...
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        ...
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <!--here comes your stuff-->

    </LinearLayout>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fake"
        android:textSize="1sp"/>

</LinearLayout>

then in your java code:

View view = Activity.this.getCurrentFocus();
                    if (view != null) {
                        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                        fake.requestFocus();
                    }

it will hide the keyboard and remove the focus of any edittext that has it. and also as you see the fake edittext is out of screen and can't be seen

Solution 12 - Android

Just include this line

android:selectAllOnFocus="false"

in the XML segment corresponding to the EditText layout.

Solution 13 - Android

If I understand your question correctly, this should help you:

TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1 .setFocusable(false);

Solution 14 - Android

You just have to clear the focus from the view as

EditText.clearFocus()

Solution 15 - Android

Since I was in a widget and not in an activity I did:

getRootView().clearFocus();

Solution 16 - Android

You only have to set the ViewGroup with the attribute:

android:focusableInTouchMode="true"

The ViewGroup is the layout that includes every child view.

Solution 17 - Android

Just add this line in your parent layout(e.g., constraint or Linear)

android:focusableInTouchMode="true"

Solution 18 - Android

<EditText android:layout_height="wrap_content" android:background="@android:color/transparent" android:layout_width="match_parent" 
    android:clickable="false"
     android:focusable="false"
     android:textSize="40dp"
     android:textAlignment="center" 
    android:textStyle="bold"  
    android:textAppearance="@style/Base.Theme.AppCompat.Light.DarkActionBar" 
   android:text="AVIATORS"/>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontobbenb3View Question on Stackoverflow
Solution 1 - AndroidZarakiView Answer on Stackoverflow
Solution 2 - AndroidMishaZView Answer on Stackoverflow
Solution 3 - AndroidMaraguesView Answer on Stackoverflow
Solution 4 - AndroidStoycho AndreevView Answer on Stackoverflow
Solution 5 - AndroidOmkarView Answer on Stackoverflow
Solution 6 - Androidlm2aView Answer on Stackoverflow
Solution 7 - AndroidSveinung Kval BakkenView Answer on Stackoverflow
Solution 8 - AndroidLena View Answer on Stackoverflow
Solution 9 - AndroidJoelView Answer on Stackoverflow
Solution 10 - AndroidPmanAceView Answer on Stackoverflow
Solution 11 - AndroidnavidView Answer on Stackoverflow
Solution 12 - AndroidharshvardhanView Answer on Stackoverflow
Solution 13 - AndroidkPieczonkaView Answer on Stackoverflow
Solution 14 - AndroidMayank BhatnagarView Answer on Stackoverflow
Solution 15 - AndroidkingstonView Answer on Stackoverflow
Solution 16 - AndroidBlackGraperView Answer on Stackoverflow
Solution 17 - AndroidkashinathView Answer on Stackoverflow
Solution 18 - AndroidvithikaView Answer on Stackoverflow