When comparing our design between developers, we found a strange behavior. After some analysis we went to this observation.
When the activity starts, on some cases the keyboard appears but sometimes not.
In fact, without a ScrollView, the soft keyboard does not appear by default on an EditText.
<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"
tools:context=".TestActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
</LinearLayout>
But when we add a ScrollView, the soft keyboard shows up by default.
<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"
tools:context=".TestActivity" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
</ScrollView>
</LinearLayout>
It only depends on the presence of the ScrollView. We can fix that with a specific declaration in the AndroidManifest, but this is the default behavior.
I and my fellow developer wonder why is this occurring ?
Here is what I understand of this problem after digging in the code of Android and building some test layouts with an EditText.
As ScrollView is defined as
public class More ...ScrollView extends FrameLayout { ... }
I tried using a FrameLayout as a container for an EditText item. As a result the software keyboard is not triggered.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text" >
<requestFocus />
</EditText>
</FrameLayout>
But as written in the question, using a ScrollView triggers the software keyboard (I simplified the xml source).
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text" >
<requestFocus />
</EditText>
</ScrollView>
So the element that allows the software keyboard to be triggered is in the ScrollView source file.
Edit: after having created my own class MyFrameLayout extending FrameLayout and playing with the code, I found that it is something in default scrollview style (R.attr.scrollViewStyle) that is responsible for the keyboard to be shown or not...
Edit2: finally the attribute android:scrollbars allows the keyboard to be automatically triggered at startup if present...
In my case android:scrollbars fixed this until I had to add:
android:windowSoftInputMode="adjustResize">
To be able to scroll when keyboard shows.
To be able to use both properties I had to add:
android:focusableInTouchMode="true"
In the child of the Scrollview
I found focusableInTouchMode answer here:
Stop EditText from gaining focus at Activity startup
This is because when and app is launched, android focuses on the first available view. In the first case it is the EditText, thats why the keyboard pops up. In the second case, the first view is the ScrollView is the first view, which doesn't require keyboard, so it is not shown.
Also, in the first case, you can remove <requestFocus />, and on some devices, the keyboard, will not pop up. Hope this helps.
Related
Im having difficulty to understand the unique behavior I found.
I have a layout with only a ListView, where its item layout is some texts & editText.
Showing such a dialog onscreen leads to an unexpected behavior - keyboard wont pop-up when giving focus to it.
I strangely find that if I add another element to the dialog's layout, the issue is solved.
Dialog layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listView"
/>
<!-- patch. otherwise, EditText within listview wont pop keyboard. weird, right? -->
<EditText
android:layout_width="wrap_content"
android:visibility="gone"
android:layout_height="wrap_content" />
</LinearLayout>
Item layout:
..
...
....
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="5"
android:maxEms="5"
android:editable="true"
android:numeric="integer"
android:id="#+id/purchaseAmount" />
....
...
..
Can anyone give an explanation to that behavior?
Perhaps the ListView does not have focus. You can ensure this by modifying your xml slightly.
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listView">
<requestFocus/>
</ListView>
i have to develope an app in fullscreen mode and now i have a formular
with some textEdits and some buttons. If the keyboard pops up it hides
two buttons so i put everything into a scrollview:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
tools:context="controller.RegsiterActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/register_textview_firstname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:singleLine="true"
android:text="#string/text_firstname" />
<EditText
android:id="#+id/register_textedit_firstname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:inputType="text" />
<TextView
android:id="#+id/register_textview_lastname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:singleLine="true"
android:text="#string/text_lastname" />
<EditText
android:id="#+id/register_textedit_lastname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:inputType="text" />
<TextView
android:id="#+id/register_textview_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:singleLine="true"
android:text="#string/text_email" />
<EditText
android:id="#+id/register_textedit_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:inputType="textEmailAddress" />
<TextView
android:id="#+id/register_text_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:singleLine="true"
android:text="#string/text_password" />
<EditText
android:id="#+id/register_textedit_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:inputType="text" />
<Button
android:id="#+id/register_button_register"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button_register" />
<Button
android:id="#+id/register_button_cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button_cancel" />
</LinearLayout>
</ScrollView>
As i said i have to run my app in fullscreen:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
But when i now run my app no scrollView appears and no scrolling is working.
How can i make the scrollView work?
First attribute you should probably add to the ScrollView is:
android:fillViewport="true"
When set to true, this attribute causes the scroll view’s child to expand to the height of the ScrollView if needed. When the child is taller than the ScrollView, the attribute has no effect.
Also, change the fill_parent layout dimensions to match_parent because fill_parent is deprecated and you can get rid of any annoying warnings.
Further, change the layout_height attribute of the LinearLayout to match_parent so it will resize together with the ScrollView on screen configuration changes such as pulling up the keyboard.
Also, similarly to #karvoynistas suggestion, you should take a look at windowSoftInputMode property.
You can add this property to your activity:
<activity
android:windowSoftInputMode="stateVisible|adjustResize"
...>
<!-- ... -->
</activity>
The effect of this is (according to Android docs):
To ensure that the system resizes your layout to the available space—which ensures that all of your layout content is accessible (even though it probably requires scrolling)
Adding the adjustPan value to the windowSoftInputMode attribute could work but not be exactly what you want since it might cause some unusual affect (read bellow).
Android Docs comparison of the two values:
adjustResize
The activity's main window is always resized to make room for the soft keyboard on screen.
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.
There is no need to put scrollView for that reason if the positioning is okay for you before the appearance of the keyboard.
All you have to do to avoid scrolling issues with the keyboard is to set in your manifest file, inside the specific activity declaration the following :
android:windowSoftInputMode="adjustPan"
In the application's manifest file, add to the <activity/> you want the following line:
android:windowSoftInputMode="stateAlwaysHidden|adjustResize|adjustPan"
This link states the same issue.
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.
I have an Activity with a simple layout, a kind of 'faux' web browser with an EditText for the URL input, defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="6dp"
android:background="#drawable/activity_title_bar">
<EditText
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:maxLines="1"
android:scrollHorizontally="true"
android:hint="#string/browser_hint_url"
android:text="#string/browser_url">
</EditText>
<ImageButton
android:id="#+id/button_refresh"
android:layout_width="50dp"
android:layout_height="match_parent"
android:src="#drawable/browser_button_refresh">
</ImageButton>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<ImageView
android:layout_width="882sp"
android:layout_height="1532sp"
android:src="#drawable/browser_background">
</ImageView>
</ScrollView>
</LinearLayout>
The problem I am having is that when the Activity is started, be default the EditText is selected and the soft keyboard activated. I'd like for nothing to be selected to start, and thus not have the soft keyboard active, but can't figure out how to do this via XML.
Any suggestions?
Thanks, Paul
android:windowSoftInputMode=[ "stateUnchanged", "stateHidden",
"stateAlwaysHidden", "stateVisible",
"stateAlwaysVisible",
"adjustResize", ] >
Use some of this in your manifest, this will hide the automatic keyboard pop up. However, if you are using your EditText for some input at time you'll need the keyboard. :)
stateHidden will do the work
get a reference to one of your other views, either the ImageButton, or the LinearLayout. Then in your onCreate() method call view.requestFocus(); on the non-EditText view and it will steal focus away and keep the keyboard from being shown.
I have a layout that uses an EditText to let users search a database and populate a ListView. The EditText is about 2/3 of the way from the top of the screen (positioned over an ImageView, and followed by some intro text.)
The problem is that the soft keyboard hides the EditText, so the user can't see what he's typing. (I disabled the auto-suggest.)
I've tried LinearLayout, RelativeLayout, paddings, and different alignments/centerings, but I still can't get it to work right. The EditText is either hidden, gets pushed off the top of the screen, or get "squished" and distorted.
Suggestions???
One possible workaround is to move the EditText to the top of the screen. However, this deviates from the graphic design that I was given.
Another possible workaround is for me to make the soft keyboard open in full screen (not sure how, though). This will still hide the EditText, but then I can re-enable the auto-suggestion so the user can see what he's typing... sort of... because he can only see the suggestions for what he's typing.
Here's my latest attempt. See "introFrame".
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/titleContainer"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="#string/title_string"
android:textSize="15sp" android:textColor="#FFFFFF"
android:textStyle="bold" android:paddingLeft="5dp"
android:layout_height="fill_parent" android:layout_width="wrap_content" />
</LinearLayout>
<FrameLayout
android:id="#+id/introFrame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<ImageView
android:src="#drawable/main_search_image"
android:scaleType="center"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="140dp" >
<LinearLayout android:id="#+id/introSearchContainer"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<EditText android:id="#+id/intro_search_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" Enter keyword "
android:imeOptions="actionGo"
android:inputType="textFilter"
android:maxLines="1" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button android:id="#+id/intro_search_button"
android:background="#drawable/custom_button_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<TextView
android:text="#string/search_intro"
android:textSize="15sp"
android:textColor="#FFFFFF"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</LinearLayout>
</FrameLayout>
<LinearLayout android:id="#+id/listContainer"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/itemlist" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:cacheColorHint="#00000000" />
<TextView android:text="No data found" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center"
android:textSize="16sp" android:textColor="#FFFFFF" android:id="#+id/android:empty" />
</LinearLayout>
</LinearLayout>
What you're looking for is the Activity's windowSoftInputMode attribute. You set this in your AndroidManifest.xml file, and give it a value such as:
adjustResize: "The activity's main window is always resized to make room for the soft keyboard on screen."
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 will probably work for you, as long as you wrap the layout in a ScrollView. It may have negative effects if you have a bitmap image in the background, as it will be resized as well, in which case you may want to use adjustPan instead.
<activity android:windowSoftInputMode="adjustResize" />
or
<activity android:windowSoftInputMode="adjustPan" />
More information is available at the above link.
What has worked for me is to drop my highest-level LinearLayout in a scrollView (the ScrollView can only have one child). This allowed the entire activity/form to scroll up and not clutter the EditText in focus.
First, I set in my activity:
<activity android:windowSoftInputMode="adjustPan" />
Then I did the ScrollView thing I'm talking about:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- STUFF -->
</LinearLayout>
</ScrollView>
just use following in manifest...
<activity
android:windowSoftInputMode="adjustPan">
try giving the edit text a layout_weight (i.e. layout_weight=1) , this may have some other effects on your other layout items that you may have to work through, but this may help it stay visible when soft keyboard pops up