EditText not getting selected, but popping keyboard up - android

My soft keyboard pops up on an activity with an edit text, but I still have to press the edit text to be able to type into it, otherwise pressing keys on the keyboard do nothing
I am using requestFocus() as well as android:focusable="true" as well as android:focusableInTouchMode="true"
thoughts?
I do not have another edit text in this activity or within the fragment that also loads in this activity (hope thats a clue for someone, because I am out of ideas, for now)
<EditText
android:id="#+id/mainText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/ten_dp"
android:layout_marginRight="#dimen/ten_dp"
android:background="#android:color/transparent"
android:hint="#string/write_something"
android:gravity="top"
android:drawablePadding="#dimen/ten_dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="textCapSentences|textMultiLine"
android:scrollHorizontally="false" />
Manifest Entry
<activity
android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:launchMode="singleTask"
android:windowSoftInputMode="stateVisible|adjustResize" />

Try this (works for me):
myEditText.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(myEditText, 0, null);
And you don't have to set android:windowSoftInputMode="stateVisible" for your activity.

What worked for me was having the EditText requestFocus() after 200 milliseconds, using the postDelayed method of the view

Related

Remove auto focus from EditText

I have 2 text fields in the start page. But the first text field gets auto focus after launch. I don't want this. I have seen couple of answers regarding this, but none of that work for me.
My Code
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:drawablePadding="12dp"
android:padding="10dp"
android:ems="10"
android:hint="Username"
android:inputType="textEmailAddress"
android:textColorHint="#android:color/white"
android:layout_marginTop="70dp"/>
I have added two more line here
android:focusable="true"
android:focusableInTouchMode="true"
But still it gets auto focus
Try this use android:windowSoftInputMode="stateAlwaysHidden" in manifest file of your Activity
<activity
android:name=".Youractivity"
android:windowSoftInputMode="stateAlwaysHidden" />
EDIT
I have added two more line here
android:focusable="true"
android:focusableInTouchMode="true"
the above line is used for requesting focus rather than not focusing

Android EditText was not completely visible when keyboard appears

My EditText was not completely visible when the keyboard appears, it was visible till focus but is there any possibility to view complete EditText.
Layout
<ScrollView
android:id="#+id/scrollvew_direct"
android:isScrollContainer="true"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edt_employers_name"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:inputType="text"
android:maxLength="100"
android:hint="#string/dir_deposit_6"
android:textSize="#dimen/edittextsize_sub"
android:gravity="center"
android:layout_gravity="center"
android:textColor="#color/main_header"
android:textColorHint="#color/hint_color_light"
android:background="#drawable/edittext_style"/>
</ScrollView>
In mainfest
<activity
android:name=".activityname"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize"
android:theme="#android:style/Theme.Holo.Light.NoActionBar"/>
please guide me for proper solution.
In you activity tag of the manifest , add this property
android:windowSoftInputMode="adjustResize"
Edited :
Edittext height is not fully matched to the background drawable
make edittext as
android:layout_height="match_parent" or
android:layout_height="wrap_content"
and check it for me it is working
In you activity tag of the manifest , add this property
android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

Automatically popup software keyboard when the app starts

I have an editText on the main page. I want the keyboard to open automatically when the app starts and to be focused on that editText. I tried many "solutions" I found on the Internet but nothing helped me.
The keyboard still doesn't appear automatically.
By the way, my editText has the following properties:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:gravity="left"
android:focusable="true"
android:enabled="true"
android:focusableInTouchMode="true" />
So how do I make the keyboard appear automatically?
You have to do two things (I have just tested this and it works just fine):
1: request focus for the field.
From the activity java file you can do
mTextView = (EditText) findViewById(R.id.textView);
mTextView.requestFocus();
From the XML file you can rewrite your element as follows:
<EditText android:id="#+id/textView">
<requestFocus/>
</EditText>
2: Force the keyboard when showing the activity: in the onCreate method just write this line:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Add android:windowSoftInputMode="stateAlwaysVisible" to your activity tag in the AndroidManifest.xml file
<activity
android:name=".TestActivity"
android:windowSoftInputMode="stateAlwaysVisible" />
Also use requestFocus tag in EditText.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:gravity="left"
android:focusable="true"
android:enabled="true"
android:focusableInTouchMode="true">
<requestFocus />
</EditText>
Call myeditText.requestFocus() after you initialize your layout on your activity's onCreate() method.

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

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

Disable auto focus on edit text

I have an edit text:
<LinearLayout android:id="#+id/linearLayout7" android:layout_width="match_parent" android:layout_height="wrap_content">
<EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:id="#+id/editText1" android:text="3">
<requestFocus></requestFocus>
</EditText>
<Button android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/button2"></Button>
</LinearLayout>
and then some other stuff underneath
The problem I'm having is that right when the app starts the focus is on the input, i dont want focus to be on input right when the app starts.
I tried removing the
<requestFocus></requestFocus>
thing and it didn't do anything.
Add the android:focusable="true" and android:focusableInTouchMode="true" elements in the parent layout of EditText as follow;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout7" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true" android:focusableInTouchMode="true">
I think, it should help you.
See also;
Android Developers official guide on handling touch and input
If you want clear the default focus on the edit text then use the following 2 attributes
android:focusable="false"
android:focusableInTouchMode="true"
inside the parent Linear layout.
for example:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="true"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="email id"
android:inputType="textEmailAddress"
android:maxLines="1"
/>
</LinearLayout>
If you want to hide the keyboard on activity creation use these attributes inside manifest file activity tag
for example:
<activity
android:configChanges="screenSize|orientation|keyboardHidden"
android:screenOrientation="portrait"
android:name=".activities.LoginActivity"
android:windowSoftInputMode="stateHidden|adjustResize"/>
If you want to hide the keyboard on button click or on some event occur use the following code
public void onClick(View v) {
try {
//InputMethodManager is used to hide the virtual keyboard from the user after finishing the user input
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
} catch (NullPointerException e) {
Log.e("Exception", e.getMessage() + ">>");
}
}
} catch (NullPointerException e) {
Log.e("Exception", e.getMessage() + ">>");
}
If you want to take off the focus from edit text fields after leaving the activity
#Override
protected void onResume() {
super.onResume();
mEmail.clearFocus();
mPassword.clearFocus();
}
And finally If you want to clear the data in the edit text fields on submitting the form use
#Override
protected void onResume() {
super.onResume();
mEmail.getText().clear();
mPassword.getText().clear();
}
NO More work... just add android:windowSoftInputMode="stateAlwaysHidden"
to activity tag of the Manifest.xml file .
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px" />
<EditText android:text=""
android:id="#+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/hint">
</EditText>
No other coding needed, it's a simple and clear solution.
You can use window soft input mode hidden for hide keyboard and android:focusable="false" and android:focusableInTouchMode="true"
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
try this code in LinearLayout
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:hint="Search..."
android:drawableRight="#drawable/ic_search"
android:id="#+id/search_tab_hotbird_F"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyHotbird_F"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
It worked for me, wish also will help you.
Either :
android:windowSoftInputMode="stateAlwaysHidden"
add this property to your Manifest.xml file under that specific activity where you want to disable auto focus.
Cons: It will only hide your soft keyboard, will cursor will be still there.
Else :
android:focusableInTouchMode="true"
add this property to your UI xml file (under parent layout), so that it will hide focus also the keyboard both.
<EditText
android:id="#+id/input"
android:layout_width="0dp"
android:layout_height="48dp"
android:focusable="false"
android:focusableInTouchMode="false" />
detailInputView = (EditText) debugToolsView.findViewById(R.id.input);
detailInputView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
detailInputView.setFocusable(true);
detailInputView.setFocusableInTouchMode(true);
return false;
}
});
Just add the following to your main layout view xml tag:
android:focusableInTouchMode="true"
Hei Lee,
There are several way to do this, Mudassir has give a way to you to do that.
If the idea doesn't work, then do just simple thing-
In your AndroidManifest>Activity add this simple code:
android:windowSoftInputMode="stateHidden"
Nothing need to do in EditText in XML, all will be work and no focus will appear.
Look the real life example code:
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
for clear default requestFocuse you should set your View's parent focusable="true" like this below
<android.support.constraint.ConstraintLayout
android:id="#+id/coordinatorChild"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true">
Before I make visible the parent view I do this (and it is working fine) Edit text is not focused automatically just if I touch it. (parentView is including edit text)
...
parentView.setFocusable(true);
parentView.setFocusableInTouchMode(true);
objProperties.setVisibility(VISIBLE);
...

Categories

Resources