How to make EditText not focused when creating Activity
Asked Answered
A

15

70

I've read the other questions discussing this, and all of them work for my layouts, except for the very first one created.

At the moment, this is at the top of my onCreate method:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

^ That makes it so at least the keyboard doesn't pop up on startup, but the EditText is still focused on.

This is the XML for my EditText:

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/changePass"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="167dp"
    android:ems="10"
    android:imeOptions="flagNoExtractUi"
    android:inputType="textPassword"
    android:maxLength="30" >
</EditText>

This is what it looks like when I bring up my activity:

enter image description here

The problem is that with some phones, when an EditText is focused like this, they can't write in it. I want it to not focus.

What I've done works for the next layouts brought up in that it the EditTexts are not focused on and would look more like this:

enter image description here

Notice that it's the same layout, though. This is the screen after the user has been brought back to this screen, which would indicate nothing wrong with the XML because this is the same XML, but the problem is that the EditText is only focused on when the activity is created.

I've done research and all of the other questions don't help me with this (they did however help the keyboard not show up, thankfully). How can I make it so the EditText on startup will look like the second screenshot, rather than the first?

Angadresma answered 18/8, 2013 at 4:50 Comment(3)
Have you tried android:focusable="false" in EditText ?Gibbet
@PratikButani that makes it so it can never be focused on, though.Angadresma
Adding android:focusableInTouchMode="true" on my parent layout works for me.Merger
G
303

You can set property of Layout like android:descendantFocusability="beforeDescendants" and android:focusableInTouchMode="true"

Example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/mainLayout"
  android:descendantFocusability="beforeDescendants"
  android:focusableInTouchMode="true" >

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/changePass"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="167dp"
        android:ems="10"
        android:imeOptions="flagNoExtractUi"
        android:inputType="textPassword"
        android:maxLength="30" >
    </EditText>

</RelativeLayout>

May this one helpful ;)

Gibbet answered 18/8, 2013 at 5:12 Comment(0)
O
19

XML code:

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/changePass"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="167dp"
    android:ems="10"
    android:focusable="false"
    android:imeOptions="flagNoExtractUi"
    android:inputType="textPassword"
    android:maxLength="30" >
</EditText>

Java code:

EditText edPwd = (EditText)findViewById(R.id.password);
edtPwd.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            v.setFocusable(true);
            v.setFocusableInTouchMode(true);
            return false;
        }
    });

set focusable false in xml and set it true via the code

Olethea answered 18/8, 2013 at 5:14 Comment(2)
Good solution ... the answer marked correct results in the IME option actionNext failing completely.Vega
I tried it first it didn't work, my bad I was testing on emulator with no touch screen, On real device its working like a charm, Thanks @OletheaFruitarian
S
12

In your main_layout add this 2 lines:

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

example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"> *YOUR LAYOUT CODE* </RelativeLayout>
Steel answered 8/2, 2016 at 9:21 Comment(1)
Edit Text is not focused but the keyboard is visible.Demit
M
3

the best solution is here: https://mcmap.net/q/240057/-disable-auto-focus-on-edit-text

the correct and simple solution is to setFocusable false and setFocusableInTouchMode true . so the EditText gain focus only when user touch that EditText

android:focusable="false"
android:focusableInTouchMode="true"
Motheaten answered 17/7, 2017 at 8:18 Comment(0)
O
2

Add this in onCreate()

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

or in onCreateView()

 getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Ostrom answered 16/3, 2017 at 5:41 Comment(0)
L
2

Whenever I'm trying to open Editext after selection item of Spinner, so on that time not able to open keyboard & write down the values in Edit text, but I resolved my issue with this code.

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

<androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:descendantFocusability="beforeDescendants"
                android:focusableInTouchMode="true">
    
                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/titleName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="textCapWords"
                    android:hint="@string/hint_your_full_name"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

Thanks!

Lucan answered 21/6, 2022 at 12:9 Comment(0)
P
1

XML Code

<EditText 
    android:imeOptions="flagNoExtractUi"
    android:focusable="false"
    />

Java Code in onClickListerner

 mEdtEnterPhoneNumber.setFocusable(true);
    mEdtEnterPhoneNumber.setFocusableInTouchMode(true);
Polished answered 16/6, 2016 at 3:24 Comment(0)
M
0

It is possible to use android:focusable="false" to disable it, but if that does not work for some reason, then you can simply put a LinearLayout and it will take the focus without disrupting your layout.

NOTE: Eclipse will give you an error saying that your LinearLayout is useless because it has no contents. You should be able to disregard it with no problems.

For example:

<LinearLayout
    android:focusable="true"
    android:focusableInTouchMode="false"
    android:layout_width="0dp"
    android:layout_height="0dp"
    />
<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/changePass"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="167dp"
    android:ems="10"
    android:imeOptions="flagNoExtractUi"
    android:inputType="textPassword"
    android:maxLength="30" >
</EditText>
</LinearLayout>
Magnificat answered 18/8, 2013 at 5:16 Comment(0)
D
0

You can do this programmatically. use editText.setEnabled(false) in your onStart() method of your activity (not in onCreate() - because this is some method for initializing GUI components)

Daltondaltonism answered 6/1, 2016 at 9:40 Comment(1)
This would completely disable the EditText, which is not what is needed. I'm not sure (because I haven't tested), but I don't think disabling and then re-enabling would work either. Unless there's something I'm missing, make sure you understand the question.Angadresma
R
0
<activity
            android:name=".MainActivity"
            android:windowSoftInputMode="stateAlwaysHidden" />

android:windowSoftInputMode="stateAlwaysHidden"

add this line in the activity tag of the Manifest.xml file.when you click on the EditText the keyboard gets into focus.

Rubescent answered 5/10, 2017 at 7:26 Comment(0)
E
0

The easiest way is to add

android:windowSoftInputMode="stateAlwaysHidden|adjustPan" 

in the activity tag of the Manifest.xml file

Esra answered 26/2, 2018 at 9:44 Comment(0)
G
0

1) Its the simplest,open the manifest and put the following code between the activity tag:

android:windowSoftInputMode="stateHidden"

2) Put this attributes in the parent layout

android:focusable="true" 
android:focusableInTouchMode="true"
Godavari answered 18/9, 2018 at 8:23 Comment(0)
C
0

In Manifest , copy and paste the beneath code.

 <activity
   android:name=".LoginActivity"
   android:windowSoftInputMode="stateAlwaysHidden"/>
Clairclairaudience answered 9/11, 2018 at 7:57 Comment(0)
B
0
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/changePassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:text="Change Password"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/accountPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Account Password"
        app:layout_constraintBottom_toBottomOf="@+id/changePassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/changePassword" />

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="152dp"
        android:ems="10"
        android:imeOptions="flagNoExtractUi"
        android:inputType="text"
        android:text="Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/changePassword" />
</androidx.constraintlayout.widget.ConstraintLayout>
Bolero answered 25/1 at 18:0 Comment(1)
That's one long wall of xml text... for the sake of future visitors, can you please edit and explain what does what?Camus
B
-7

use android:focusable="false" for focusing disable

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/changePass"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="167dp"
    android:ems="10"
    android:imeOptions="flagNoExtractUi"
    android:inputType="textPassword"
    android:maxLength="30"
 android:focusable="false" >

</EditText>
Bilbo answered 18/8, 2013 at 5:7 Comment(3)
it works, but that makes it so it can never be focused on, even after touching.Angadresma
you can use event listener when you need to make it focusableBilbo
edit your answer to have that, and I'll vote you up. Someone already answered with what you said, though.Angadresma

© 2022 - 2024 — McMap. All rights reserved.