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.
Related
I am having an activity in android where there is an editbox and 3 buttons below the editbox. Please see attachment. When this activity is launched, the default state is STATE1.(Please see image). The keyboard is visible by default. Now when I press the back button or dispose the keyboard, I wish to have the edittext resized and occupy the whole screen as shown in STATE2.
I am not sure how to accomplish this. I have the height of the edittext hardcoded to some dp based on the target device. I believe this has to be changed. Can anyone help me in how to accomplish this.
The XML of the layout file is below as well as the screencap
<?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="fill_parent"
android:background="#FFFFFF" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/EditMessage"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="#drawable/newback"
android:gravity="top"
android:imeOptions="actionDone"
android:inputType="textMultiLine|textFilter|textVisiblePassword|textNoSuggestions"
android:maxLength="200"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/PostMessage"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_marginRight="0.2dp"
android:background="#drawable/newbutton_corner"
android:text="#string/SubmitMessage"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<Button
android:id="#+id/CancelMessage"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_marginRight="0.2dp"
android:background="#drawable/newbutton_corner"
android:text="#string/CancelMessage"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<Button
android:id="#+id/DeleteMessage"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:background="#drawable/newbutton_corner"
android:text="#string/DeleteMessage"
android:textColor="#FFFFFF"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Change your EditText as follows:
<EditText
android:id="#+id/EditMessage"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:imeOptions="actionDone"
android:inputType="textMultiLine|textFilter|textVisiblePassword|textNoSuggestions"
android:maxLength="200"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
/>
Add layout_weight = 1
In my opinion you do not need adjustPan. I will leave it upon you to decide. The behaviour will be different and you can try it out. Here is what the documentation says about 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.
Your buttons at the bottom may get hidden if using this. Instead use adjustResize:
Put this line in the activity inside AndroidManifest
android:windowSoftInputMode="stateVisible|adjustResize"
Try this.. in your manifest.
<activity
android:name="Activity"
android:windowSoftInputMode="adjustPan"/>
and your edittext use <requestFocus /> for from that start itself it'll focus
and android:layout_weight="1" it will automatically fill the screen.
<EditText
android:id="#+id/EditMessage"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#696969"
android:gravity="top"
android:imeOptions="actionDone"
android:inputType="textMultiLine|textFilter|textVisiblePassword|textNoSuggestions"
android:maxLength="200"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
>
<requestFocus />
</EditText>
Yes you can do that, its simple.
android:windowSoftInputMode="adjustResize" ,, add this attribute to you manifiest under required activity
Set width and height of your text view to match_parent
Done.
add android:windowSoftInputMode="adjustPan" and change android:layout_height="150dp" to android:layout_height="fill_parent"
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.
I'm having trouble when showing a button next to the search box. I'm using an auto complete text view and i don't know if this is due to the auto complete text view. How can i fix this. Here's what i got so far:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<AutoCompleteTextView
android:id="#+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
/>
<Button
android:id="#+id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go"
/>
</LinearLayout>
To fix this, just add the tag android:layout_weight=1 to your AutoCompleteTextView.
This happens because android noticed you set the layout_width to fill_parent on your AutoCompleteTextView, and then it has no space for the Button left. Setting the weight to 1 means the textview will be "generous" and give as much space to any other components as they request, which in this case is limited to the width of the Button after is wrapped.
You are filling the parent width with your AutoCompleteTextView (thus pushing the Button out of screen width). Try to change this
<AutoCompleteTextView
android:id="#+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
/>
into this
<AutoCompleteTextView
android:id="#+id/autocomplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
/>
And your Button should become visible. Here is a nice resource conserning layouts: Android Developer | Declaring Layouts
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
I'm trying to implement a login view where several EditTexts and a logo are displayed on the screen with a ButtonBar at the bottom, something like this:
alt text http://russellhaering.com/media/addAccount.png
The problem is that on very small screens, especially when they are rotated sideways, the entire main view doesn't fit onto the screen.
I currently have
<?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="fill_parent"
android:background="#234C59" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="5dip"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:orientation="vertical" >
<ImageView
android:id="#+id/logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:layout_centerHorizontal="true"
android:src="#drawable/logo" />
<EditText
android:id="#+id/input_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:inputType="textEmailAddress"
android:singleLine="true"
android:hint="Username or email" />
<EditText
android:id="#+id/input_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop=""
android:inputType="textPassword"
android:singleLine="true"
android:hint="Password" />
</LinearLayout>
<LinearLayout
android:id="#+id/buttonbar_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
style="#android:style/ButtonBar" >
<Button
android:id="#+id/button_signup"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sign Up" />
<Button
android:id="#+id/button_login"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Log In" />
</LinearLayout>
</RelativeLayout>
I've tried encapsulating the first LinnearLayout in a ScrollView that looks like this:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Linear Layout Here -->
</ScrollView>
But that introduces two problems:
The ScrollView doesn't scroll, even on screens where the data doesn't all fit
The ButtonBar floats on top of the onscreen keyboard, obscuring even more of the screen when the keyboard comes up.
Everything worked great when I had the buttons inside the ScrollView, but now that I have them in the ButtonBar I'm having a lot of trouble figuring this out.
It turned out that the solution required two steps:
The inability to scroll was a result of the ScrollView being behind the Button Bar. To fix this, I defined the ScrollView below the Button Bar, then used android:layout_above="#id/buttonbar_login" to force the ScrollView to reside entirely above the Button Bar.
Apparently when the onscreen keyboard is opened, if you have a ScrollView it will be resized allowing the Button Bar to float up with the keyboard. To fix this I modified the manifest and added android:windowSoftInputMode="adjustPan" to prevent the ScrollView from resizing.
If your use case supports hiding of the button bar in landscape orientation you can check Resources.getSystem().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE and set the button bar to View.GONE.
You also probably need to set android:windowSoftInputMode="adjustResize" on the <activity> in your manifest file. Android will only put it in adjustResize automatically when the root layout is a ScrollView (iirc).