Remove focus from EditText and also keep it editable - android

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"
>

Related

Recycler View background image gets shrink when keyboard popups through editText

I have a recycler-view with a sample background in my activity but I'm stuck in an error. Whenever keyboard popups in the activity, the background of recycler-view gets shrink. How to avoid this thing. Means how to stop it from being shrinking ? Here are the images attached below.
normal Image
shrinked background
XML Code:
<RelativeLayout 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:background="#drawable/splash_bg"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/editText" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="5dp"
android:background="#fff"
android:hint="Your text here"
android:inputType="text"
android:padding="12dp"
android:textSize="24sp" />
</RelativeLayout>
In the tag of the recyclerview container activity declared in manifest add
android:windowSoftInputMode="stateHidden|adjustNothing"
like
<activity
android:name=".YourActivityName"
android:windowSoftInputMode="stateHidden|adjustNothing" />
add the image to your background of layout, not in recyclerview. And also recyclerview's item should have no background color except its necessary to widgets.
Set attribute android:windowSoftInputMode to be "adjustPan" in your AndroidManifest.xml under your activity:
<activity
android:name=".YourActivityName"
android:windowSoftInputMode="adjustPan" />

Edit text not editable android

I have an Edit Text inside a custom ListView. When I am typing a value in the EditText, it is not showing what I type. If I tap several times on the EditText, it appears the typed text sometimes. How should I overcome this issue?
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edtReason"
android:background="#drawable/edittextstyle"
android:layout_below="#+id/textView20"
android:layout_toLeftOf="#+id/chkSelect"
android:layout_alignLeft="#+id/textView20"
android:layout_alignStart="#+id/textView20" />
I hope you have added the following property in parent layout of your listview item
android:descendantFocusability="blocksDescendants"
Now add the following in xml of EditText
android:focusableInTouchMode="true"
android:focusable="true"
Add these attributes and try again, Hope this helps
android:hint="Enter Message"
android:inputType="textMultiLine|textPostalAddress"
android:focusable="true"
android:clickable="true"
android:cursorVisible="true"
android:layout_width="50dp"
android:layout_height="wrap_content
android:focusable="true"
android:focusableInTouchMode="true"
android:textColor="#44444"
android:ems="10"
Add this in Manifest:
android:windowSoftInputMode="adjustPan"
Manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".ListviewActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan">

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.

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.

Scroll a Scroll View which contains Edit Text

my scroll view contains a linear layout which further contains some of the edit text fields. now when I click on a edit text field at the bottom one then soft keyboard appears, but my problem is that it covers that edit textfield. this is my activity declared in android manifest file:
<activity
android:name="me.example.scrollview.LoginActivity"
android:label="#string/title_activity_login"
android:theme="#android:style/Theme.Black.NoTitleBar"
android:windowSoftInputMode="stateHidden|adjustResize|adjustPan" >
</activity>
and here is my layout.xml file for this activity.
<RelativeLayout 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:background="#android:color/darker_gray"
tools:context=".LoginActivity" >
<ScrollView
android:id="#+id/login_sv_login_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true"
android:background="#android:color/transparent" >
<LinearLayout
android:id="#+id/login_ll_container"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="230dp"
android:layout_gravity="center_horizontal"
android:background="#android:color/transparent"
android:orientation="vertical" >
<EditText
android:id="#+id/txt_username"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="#drawable/rounded_edittext"
android:ems="10"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/txt_phone"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="#drawable/rounded_edittext"
android:ems="10"
android:inputType="phone"
android:maxLines="1"
android:singleLine="true" />
<EditText
android:id="#+id/txt_password"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="#drawable/rounded_edittext"
android:ems="10"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true" />
<Button
android:id="#+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:background="#drawable/ic_login"
android:text="" />
</LinearLayout>
</ScrollView>
Now Please help me as I am very new to android programming. I am unable to fix this issue and can't find a way to scroll my scroll view without resizing my main window.I just want my scroll view to make scrolling when soft keyboard appears.Thanx in advance.
Both previously pointed out solutions can work. But there might still be a problem if the edittext is at the bottom of the ListView where it would still be covered by the onscreen keyboard. Not sure if a scrollview can be scrolled to a point where there is no more content.
What i've seen is that you might use two contradicting flags in your manifest:
android:windowSoftInputMode="stateHidden|adjustResize|adjustPan" > </activity>
adjustResize and adjustPan can imho NOT be combined. Try how it looks with only adjustResize. This should avoid that the keyboard covers anything of your view.
You can try:
ScrollView scroll = (ScrollView)findViewById(R.id.scrollview);
scroll.scrollTo(0, sv.getBottom());
or
scroll.scrollTo(5, 10);
You can use YOURSCROLLVIEW.scrollTo(int x, int y) or YOURSCROLLVIEW.smoothScrollTo(int x, int y) to move the scroll view to the appropriate coordinates so that the edit text box and keyboard are both shown.
More information about these functions and other scrollview functions can be found on the android developer reference page.

Categories

Resources