I would like to move a single element (relative layout) of my layout above the software keyboard when an editText is focused.
I've tried to set android:windowSoftInputMode with adjustResize in my manifest but as I seen in many other stackoverflow questions it works only with the entire Activity.
So, is it possible to move a single element above the keyboard?
I hope the following sample helps.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<ScrollView android:id="#+id/scrollView1" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_weight="1">
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<EditText android:id="#+id/editText1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="EditText1" />
<EditText android:id="#+id/editText2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="EditText2" />
<EditText android:id="#+id/editText3" android:layout_height="wrap_content"
<EditText android:id="#+id/editText4" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="EditText4" />
<EditText android:id="#+id/editText5" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="EditText5" />
<EditText android:id="#+id/editText6" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="EditText6" />
<EditText android:id="#+id/editText7" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="EditText7" />
<EditText android:id="#+id/editText8" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="EditText8" />
<EditText android:id="#+id/editText9" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="EditText9" />
</LinearLayout>
</ScrollView>
<LinearLayout style="#android:style/ButtonBar" android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:id="#+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="Button"></Button>
</LinearLayout>
</LinearLayout>
http://www.androidpub.com/?document_srl=1703090&mid=android_dev_qna&comment_srl=1704523&rnd=1704523#comment_1704523
Related
When I'm trying to add ScrollView its giving me error that Scroll view can contain only a single child. So how to add a ScrollView ?
And also when I open my app in various phones all the phones gives different layouts. How do manage the look and feel of the app same for all mobile devices ?
Register.xml
<?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:background="#0066CC"
android:orientation="vertical"
android:padding="10dp" >
<ImageView
android:id="#+id/upImage"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="#+id/textView1"
android:layout_width="465dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:layout_weight="0.02"
android:layout_marginBottom="100dp"
android:textSize="15sp"
android:textStyle="bold"
android:text="Tap to upload Profile Picture" />
<EditText
android:id="#+id/imName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:hint="Enter name of the Image"
android:ems="10" >
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="#+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age" />
<EditText
android:id="#+id/etAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username" />
<EditText
android:id="#+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:inputType="textPassword" />
<Button
android:id="#+id/bRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register" />
</LinearLayout>
Yes ScrollView allow only single child
So put you parent LinearLayout inside ScrollView like below
<ScrollView 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0066CC"
android:orientation="vertical"
android:padding="10dp" >
<ImageView
android:id="#+id/upImage"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="#+id/textView1"
android:layout_width="465dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:layout_weight="0.02"
android:layout_marginBottom="100dp"
android:textSize="15sp"
android:textStyle="bold"
android:text="Tap to upload Profile Picture" />
<EditText
android:id="#+id/imName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:hint="Enter name of the Image"
android:ems="10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="#+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age" />
<EditText
android:id="#+id/etAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username" />
<EditText
android:id="#+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:inputType="textPassword" />
<Button
android:id="#+id/bRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register" />
</LinearLayout>
</ScrollView>
Android only supports a single child inside a ScrollView. The simplest way is to enclose everything inside a LinearLayout and make it a child of a ScrollView.
If you however want to maintain the same look and feel throughout the application, try using a RelativeLayout instead of the LinearLayout as the child of the ScrollView.
I have an activity with a form. I put all views in a ScrollView because when the keyboard opens, I would scroll through the fields.
When the keyboard opens, the first field goes over the top of the ScrollView and becomes unreachable.
This is the layout:
<ScrollView 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Username"
android:inputType="text|textEmailAddress"
android:singleLine="true" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Password"
android:inputType="textPassword"
android:singleLine="true" />
<EditText
android:id="#+id/password1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="#string/repeat_password"
android:inputType="textPassword"
android:singleLine="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/gender" />
<RadioGroup
android:id="#+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<RadioButton
android:id="#+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="M" />
<RadioButton
android:id="#+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/age" />
<EditText
android:id="#+id/age"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:paddingLeft="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/education" />
<Spinner
android:id="#+id/education"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:onClick="salva"
android:text="#string/save" />
</LinearLayout>
</ScrollView>
This is the activity definition in the manifest:
<activity
android:name=".activities.SignUpActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_sign_up">
</activity>
This is a screenshot. The username field is unreachable! (look at the scrollbar on the right)
Use following in your App Manifest
android:windowSoftInputMode="stateHidden|adjustPan"
For more details you can visit here
http://developer.android.com/guide/topics/manifest/activity-element.html
In your activity manifest entry add this:
android:windowSoftInputMode="stateVisible|adjustPan".
I've found the solution. The problem was this line:
android:layout_gravity="center"
I just removed it from the layout file. The manifest was correct.
http://snag.gy/0kz5e.jpg please look this Image I have to create layout but
I am unable to do this there is some Problem with android control set on particular.
I am able to display controls in android:gravity bottom but my controls is not fix like image please check what I am missing where am doing wrong .
here is my Xml file code:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/registraionimage" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="132dp"
android:layout_gravity="bottom"
android:orientation="vertical" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="49dp"
android:entries="#array/country_arrays"
android:prompt="#string/country_prompt" />
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText1"
android:layout_width="94dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:text="Code" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="phone"
android:text="Phone" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout1"
android:text="Next"
android:textColor="#0066FF"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
Can you just try below code, I replace FrameLayout to RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/main_background" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:background="#android:color/transparent"
android:orientation="vertical" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText1"
android:layout_width="94dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:text="Code" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"
android:text="Phone" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Next"
android:textColor="#0066FF"
android:textStyle="bold" />
</LinearLayout>
Dear All experts I have problem with using scrollView anyone please help.. the Error is showing that the ScrollView is useless and also in other form of mine is shows the same , how can I solve this and how can I make my forms and activities been scroll ??
`
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="#string/welcome"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_margin="30dp"
android:contentDescription="#+id/button1"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/username"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/password"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="android:buttonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="goToMain"
android:text="#string/log_in" />
<Button
android:id="#+id/button2"
style="android:buttonStyle "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="register"
android:text="#string/registration" />
</LinearLayout>
</ScrollView>
`
You need a child container as LinearLayout, TableLayout or RelativeLayout in your ScrollView like this:
<ScrollView
... >
<LinearLayout
... >
<!-- Your views: TextView, LinearLayout, etc. -->
</LinearLayout>
</ScrollView>
According to the reference:
A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll
You should put all the other fields in a single Layout.. Like linear layout,relative layout. Your whole code should look like this
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="#string/welcome"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_margin="30dp"
android:contentDescription="#+id/button1"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/username"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/password"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="android:buttonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="goToMain"
android:text="#string/log_in" />
<Button
android:id="#+id/button2"
style="android:buttonStyle "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="register"
android:text="#string/registration" />
</LinearLayout>
</LinearLayout>
</ScrollView>
"Scrollview can host only one direct child"
Put all your stuff in some Layout e.g LinearLayout
Name, password1 field not showing up in scrollview
everything else working properly
here is my xml for it..
Made a linear layout after scrollview as it supports only one child.
Any idea what is the problem ?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name" />
<EditText
android:id="#+id/password1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword" />
<EditText
android:id="#+id/password2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/retype"
android:inputType="textPassword" />
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/email"
android:inputType="textEmailAddress" />
<TextView
android:id="#+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender" />
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="#+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<ImageView
android:id="#+id/capcha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/typecaptcha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Type" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</ScrollView>
</LinearLayout>
For me the Password field works, seems fine (I copied you XML to test it). If you use eclipse try Project>Clean. I had similar Problems.
Happy Coding
EDIT(Result):