Disable auto focus on edit text - android

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);
...

Related

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"

Remove focus from EditText and also keep it editable

I am using EditText but when Activity is started my EditText is already focused. I want to remove focus and also make it editable on click.
Here is the code of my edittext.
<EditText
android:id="#+id/et_HotelLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:drawableLeft="#drawable/ic_edit_location_black_24dp"
android:drawablePadding="8dp"/>
You can create dummy layout as below which you should keep exactly above your EditText and then use nextFocusUp and nextFocusLeft as shown.
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:id="#+id/et"
android:drawablePadding="8dp"
android:nextFocusUp="#id/et"
android:nextFocusLeft="#id/et"
app:met_floatingLabel="normal" />
Add the following two properties in parent Layout of the Edit text
android:focusable="true"
android:focusableInTouchMode="true"
if your edit text is inside linear layout you can do like this
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/et_HotelLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:drawableLeft="#drawable/ic_edit_location_black_24dp"
android:drawablePadding="8dp"/>
</LinearLayout>
This question has already been responded here
Try this also -
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Otherwise, declare in your manifest file's activity -
<application android:icon="#drawable/icon"
android:label="#string/app_name">
<activity android:name=".Main"
android:label="#string/app_name"
android:windowSoftInputMode="stateHidden"
>

Why keyboard is overlap on the AutoCompleteTextView when inside ScrollView

I have a simple android layout and code, but the keyboard always overlap my AutoCompleteTextView when I click it.
Note : The AutoCompleteTextView when inside ScrollView.
Note : platform is 6.0.1
package com.example.jason_wu.search;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AutoCompleteTextView;
public class Main2Activity extends AppCompatActivity {
private AutoCompleteTextView mAtv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mAtv = (AutoCompleteTextView) findViewById(R.id.location_auto_complete);
//issue 1
//mAtv.setBackground(getDrawable(R.drawable.search_input_focus));
//issue 2
mAtv.setHeight(300);
}
}
and my AndroidManifest.xml here:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.jason_wu.search">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="com.example.jason_wu.search.Main2Activity"
android:windowSoftInputMode="adjustPan"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="800dp"
android:text="Large Text"
android:id="#+id/textView"/>
<AutoCompleteTextView
android:id="#+id/location_auto_complete"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView"
android:background="#color/transparent"
android:dropDownAnchor="#+id/location_auto_complete"
android:dropDownHeight="wrap_content"
android:dropDownVerticalOffset="1dip"
android:fontFamily="sans-serif-light"
android:hint="#string/new_notification_location_hint"
android:imeOptions="actionSearch"
android:inputType="textUri"
android:dropDownSelector="#color/location_bg_select"
android:popupBackground="#color/location_bg_notselect"
android:singleLine="true"
android:textColor="#FFFFFF"
android:alpha="0.2"
android:textColorHint="#999999"
android:textCursorDrawable="#null"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2"
android:layout_below="#+id/location_auto_complete" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
See
try this attribute to the activity in the AndroidManifest.xml
android:windowSoftInputMode="stateHidden|adjustPan"
it should look something like this.
<activity
android:name="com.myexample.TestActivity"
android:label="#string/login_screen"
android:screenOrientation="portrait" // well you can change the orientation if you want
android:windowSoftInputMode="stateHidden|adjustPan" />
the attribute you want to play with is this windowSoftInputMode try different values as you may require different output after certain time.
adjustPan
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
adjustResize
The activity's main window is always resized to make room for the soft keyboard on screen.
stateHidden
The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity.
you can find more details on activity-element documentation
Update
I have written something that might help you, layout is available on the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="activities.list.first.TestLayoutActivity">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"
android:orientation="vertical">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/TestLayoutActivity_autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:completionThreshold="1"
android:drawableLeft="#android:drawable/ic_menu_search"
android:text="" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
AndroidManifest.xml
<activity
android:name="activities.list.first.TestLayoutActivity"
android:label="#string/title_activity_test_layout"
android:windowSoftInputMode="stateHidden|adjustPan"
/>
Code is on the code
String[] list = {"match1", "match2", "match3",
"match12", "match11", "match8", "match7", "match4",
"match13", "match10", "match9", "match6", "match5",};
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.TestLayoutActivity_autoCompleteTextView);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(getApplicationContext(), R.layout.simple_list_item_1, list);
autoCompleteTextView.setAdapter(adapter);
Before
After
[
Try limiting the dropdown items to show
One way is you can Limit the dropdown items to show. But there's no such proper way of it to show number of maximumSuggestions option, But the trick you can apply is Get the height of one of the rows and multiply it (number of rows to show) times and use setDropDownHeight() to set its height.
There is a method setDropDownHeight of AutoCompleteTextView class. You can see them and use.
If you are confused or not sure of height of suggestions items you can customize this. Have a look how to set custom height.
The keyboard behavior is that scroll the view to close the bottom of text area.
Finally, we have to use android:gravity="bottom" attribute to solve UI issue, but we need to consider the UX, ex: Hint.
So, The solution is to listen onClick() and For keyboard issue, we have to use dynamic method to set the gravity.

How to move the layout above of soft keyboard when it is shown

I have a simple layout for my activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/buttonCancel"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="cancel" />
</RelativeLayout>
and when I touch on EditText soft keyboard appears but my button hides beyond the keyboard !
but I wanna the Button move up (above of) the keyboard.
I added this line of code to AndroidManifest but nothing changed.
<activity
android:name=".Activity"
android:windowSoftInputMode="adjustPan|adjustResize">
</activity>
According to the official documentation adjustPan and adjustResize have an opposite behaviour and therefore I don't think they should be used together. Try just android:windowSoftInputMode="adjustResize" , worked for me.

Android: EditText in ScrollView goes of the screen when kayboard appears

I am trying to create a login screen. I have used ScrollView to hold input fields. I have following xml:
<RelativeLayout ... >
<RelativeLayout
android:id="#+id/top_menu" ... >
<ImageButton ... />
<com.neopixl.pixlui.components.textview.TextView... />
</RelativeLayout>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/top_menu"
android:overScrollMode="always">
<LinearLayout
android:id="#+id/login_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<com.neopixl.pixlui.components.edittext.EditText
android:id="#+id/user_name_email"
android:layout_width="match_parent"
android:layout_height="#dimen/input_height_size"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:background="#drawable/edit_text_strock"
android:hint="#string/user_Name_Email_EditText"
android:maxLines="1"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:textColor="#color/White"
android:textColorHint="#color/White"
android:textSize="#dimen/register_input_text_size"
pixlui:typeface="MuseoSans.otf"/>
<com.neopixl.pixlui.components.edittext.EditText.... />
<com.neopixl.pixlui.components.button.Button... />
<com.neopixl.pixlui.components.textview.TextView... />
<com.neopixl.pixlui.components.textview.TextView... />
<com.neopixl.pixlui.components.textview.TextView... />
<com.neopixl.pixlui.components.button.Button... />
</LinearLayout>
</ScrollView>
</RelativeLayout>
You can see full xml here.
When I click on EditText user_name_email, it goes off the screen.
Before click:
After click:
I have tried scrollTo(x, y) and smootScrollTo(x, y) bu they didn't work.
How can I fix it?
UPDATE:
adjustPan works but I want it to work with adjustResize.
Is it possible?
Use the following line in the manifest file, for the activity you need the change.
android:windowSoftInputMode="adjustPan"
Use it as shown below:
<activity
android:name=".ActivityName"
android:label="title_for_activity"
android:windowSoftInputMode="adjustPan" >
</activity>
Hope this helps.
add this to manifest file:
android:windowSoftInputMode="adjustPan"
in the targeted activity tag.

Categories

Resources