ScrollView implementation in Android - android

I am new to android.Wen I run this apk file in my phone,it runs and wen I rotate for a horizontal view,it shows only half a page and if I try to scroll,it doesnt scroll.I tried a lot using ScrollView.It is not getting Implemented.the error wch occurs is :"activity_main.xml: ScrollView can host only one direct child".Can someone suggest me how to solve it??
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_centerHorizontal="true"
android:text="LOGIN"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView1"
android:layout_marginTop="49dp"
android:text="Username"
android:textSize="20sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/editText3"
android:layout_marginTop="17dp"
android:text="Password"
android:textSize="20sp" />
<TextView
android:id="#+id/link_to_register"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="14dp"
android:gravity="center"
android:text="Forgot Password"
android:textColor="#0b84aa"
android:textSize="20dip" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="26dp"
android:layout_toLeftOf="#+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_alignLeft="#+id/editText4"
android:ems="10"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="42dp"
android:layout_toLeftOf="#+id/textView1"
android:ems="10" />
<EditText
android:id="#+id/editText4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_marginLeft="100dp"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_below="#+id/editText4"
android:layout_marginTop="23dp"
android:background="#9ACD32"
android:text="Log In" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/imageView2"
android:scaleX="1.5"
android:scaleY="1.5"
android:src="#drawable/igs_login" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="21dp"
android:layout_toRightOf="#+id/button1"
android:src="#drawable/igs" />

The error says it very clearly ScrollView can host only one direct child. That means when you are using ScrollView you should consider creating a layout which holds all views in it and add your ScrollView as it's parent. For example :
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
//ADD YOUR VIEWS ONLY HERE
</RelativeLayout>
</ScrollView>
You layout should look like this! Instead of RelativeLayout, you can use whenever layout you want, but just remember: All views should be added in one layout!

ScrollView can host only one child, such as RelativeLayout or LinearLayout (or any other View extending VIewGroup, so your implementation should be something like this:
<ScrollView>
<LinearLayout>
here you can put how many Views you need
</LinearLayout>
</ScrollView>

Related

Android Studio Relative layout and Linear layout does not fit in some devices

I am trying to develop a simple app for project management. The problem is with the xml layout of the app screens. The app screens are proportionally not very well put in different devices. Some elements are even hidden in some devices due to lack of space.
I already tried using both linear layout and relative layout. I've always used the "Match_parent" attribute to both the width and height of the relative layout and linear layout parent block. But still in some screen sizes, some elements are not showing, they are below the display area.
<RelativeLayout
android:id="#+id/layout1"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context=".login"
android:paddingTop="20dp"
>
<ImageView
android:id="#+id/loginImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/login_logo"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="#+id/loginText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="LOGIN"
android:textColor="#color/orange"
android:textSize="50sp"
android:layout_below="#id/loginImage"
/>
<EditText
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/logintextbackground"
android:layout_centerHorizontal="true"
android:layout_below="#id/loginText"
android:hint="Username"
android:textColorHint="#color/lightOrange"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:textColor="#color/lightOrange"
android:maxLength="15"
/>
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/logintextbackground"
android:layout_centerHorizontal="true"
android:layout_below="#id/username"
android:hint="Password"
android:textColorHint="#color/lightOrange"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:textColor="#color/lightOrange"
android:inputType="textPassword"
android:maxLength="16"
/>
<Button
android:id="#+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/login_button"
android:layout_below="#id/password"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="#+id/orText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="or"
android:textSize="20dp"
android:textColor="#color/orange"
android:layout_below="#id/loginButton"
android:layout_centerHorizontal="true"
/>
<Button
android:id="#+id/signUpButtonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/signup"
android:layout_below="#id/loginButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
/>
Put a ScrollView as parent of your RelativeLayout and you should be fine. Refer code 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"
android:background="#color/white">
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:paddingTop="20dp"
tools:context=".login">
<ImageView
android:id="#+id/loginImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/login_logo" />
<TextView
android:id="#+id/loginText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/loginImage"
android:layout_centerHorizontal="true"
android:text="LOGIN"
android:textColor="#color/orange"
android:textSize="50sp" />
<EditText
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/loginText"
android:layout_centerHorizontal="true"
android:background="#drawable/logintextbackground"
android:hint="Username"
android:maxLength="15"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:textColor="#color/lightOrange"
android:textColorHint="#color/lightOrange" />
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/username"
android:layout_centerHorizontal="true"
android:background="#drawable/logintextbackground"
android:hint="Password"
android:inputType="textPassword"
android:maxLength="16"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:textColor="#color/lightOrange"
android:textColorHint="#color/lightOrange" />
<Button
android:id="#+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/password"
android:layout_centerHorizontal="true"
android:background="#drawable/login_button" />
<TextView
android:id="#+id/orText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/loginButton"
android:layout_centerHorizontal="true"
android:text="or"
android:textColor="#color/orange"
android:textSize="20dp" />
<Button
android:id="#+id/signUpButtonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/loginButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/signup" />
</RelativeLayout>
</ScrollView>
For future development, I would suggest you to use ConstraintLayout instead of LinearLayout or RelativeLayout as it provides less nesting of the views.

AndroidStudio Problem with Relative Layout

I have a problem with RelativeLayout. In design view I can see my Layout works perfect, but in runtime one EditText goes down from the screen.
Here is some code of my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvInicio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:text="#string/dialog_titulo"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvPrimero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="85dp"
android:text="#string/dialog_max"
android:textColor="#color/colorPrimary"
android:layout_alignParentLeft="true" />
<EditText
android:id="#+id/etMaximo"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvPrimero"
android:layout_alignParentEnd="true"
android:ems="10"
android:hint="#string/dialog_etMax"
android:inputType="number"
android:layout_alignParentRight="true" />
<EditText
android:id="#+id/etMinimo"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="151dp"
android:ems="10"
android:hint="#string/dialog_etMin"
android:inputType="number"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/tvSegundo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/etMinimo"
android:text="#string/dialog_maxmin"
android:textColor="#color/colorPrimary"
android:layout_alignParentLeft="true" />
<EditText
android:id="#+id/etMaximo2"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="#+id/tvSegundo"
android:ems="10"
android:hint="#string/dialog_etMax"
android:inputType="number"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/tvTexto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/etTexto"
android:layout_alignParentStart="true"
android:text="#string/diaog_textoTitulo"
android:textColor="#color/colorPrimary"
android:layout_alignParentLeft="true" />
<EditText
android:id="#+id/etTexto"
android:layout_width="193dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="180dp"
android:ems="10"
android:hint="#string/diaog_etTextoTitulo"
android:inputType="textPersonName"
android:layout_alignParentRight="true" />
<LinearLayout
android:layout_width="360dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/btnCancelar"
android:layout_width="176dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="19dp"
android:layout_toLeftOf="#+id/etTexto"
android:layout_toStartOf="#+id/etTexto"
android:background="#drawable/boton_redondeado2"
android:text="#string/iniciar_sesion_cancelar"
android:textColor="#android:color/background_light" />
<Button
android:id="#+id/btnGuardar"
android:layout_width="178dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/btnCancelar"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:background="#drawable/boton_redondeado"
android:text="#string/diaog_guardar"
android:textColor="#android:color/background_light" />
</LinearLayout>
See here
Run time view look like this
Can someone help me because I'm desperate. My English is bad, so I accept edits
When using RelativeLayout, instead of using
android:layout_alignParentBottom="true"
android:layout_marginBottom="180dp"
like you are doing in the EditText with with id #+id/etTexto, you can simply write this code: android:layout_below="#id/etMaximo2"
You can even place some android:layout_marginTop if you want to add more space between the two Views.
P.S. For testing in the design view, know that you can change the dimensions of the device you're testing, so you can see in the "Preview" window what it would look like on device with different screen sizes. You can change this in the top toolbar of the preview window.
I hope this helps you! :)
Give this id to the LinearLayout:
android:id="#+id/layout"
and add this attribute to etTexto:
android:layout_above="#+id/layout"
also adjust the bottom margin as you like.
Try to delete android:layout_alignParentEnd="true" and then clean your project after the clean, run again.
Make sure that your Relative Layout is closed right after the linear ends
</LinearLayout>
</RelativeLayout>
Does it work now?
I used this xml and it worked on a Nexus 5 emulator
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvInicio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:text="TEST"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvPrimero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="85dp"
android:text="test"
android:textColor="#color/colorPrimary"
android:layout_alignParentLeft="true" />
<EditText
android:id="#+id/etMaximo"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvPrimero"
android:layout_alignParentEnd="true"
android:ems="10"
android:hint="test"
android:inputType="number"
android:layout_alignParentRight="true" />
<EditText
android:id="#+id/etMinimo"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="151dp"
android:ems="10"
android:hint="test"
android:inputType="number"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/tvSegundo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/etMinimo"
android:text="test"
android:textColor="#color/colorPrimary"
android:layout_alignParentLeft="true" />
<EditText
android:id="#+id/etMaximo2"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="#+id/tvSegundo"
android:ems="10"
android:hint="test"
android:inputType="number"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/tvTexto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/etTexto"
android:layout_alignParentStart="true"
android:text="test"
android:textColor="#color/colorPrimary"
android:layout_alignParentLeft="true" />
<EditText
android:id="#+id/etTexto"
android:layout_width="193dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="180dp"
android:ems="10"
android:hint="this should stay right here"
android:inputType="textPersonName"
android:layout_alignParentRight="true" />
<LinearLayout
android:layout_width="360dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/btnCancelar"
android:layout_width="176dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="19dp"
android:layout_toLeftOf="#+id/etTexto"
android:layout_toStartOf="#+id/etTexto"
android:background="#color/colorPrimary"
android:text="button"
android:textColor="#android:color/background_light" />
<Button
android:id="#+id/btnGuardar"
android:layout_width="178dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/btnCancelar"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:background="#color/colorAccent"
android:text="button"
android:textColor="#android:color/background_light" />
</LinearLayout>
</RelativeLayout>
Maybe you should try putting it below EditText that is above it?
Remove
android:layout_alignParentBottom="true"
android:layout_marginBottom="180dp"
Add
android:layout_below="#+id/etMaximo2"
Result
<EditText
android:id="#+id/etTexto"
android:layout_width="193dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="#+id/etMaximo2"
android:ems="10"
android:hint="#string/diaog_etTextoTitulo"
android:inputType="textPersonName"
android:layout_alignParentRight="true" />
Then set some top margin android:layout_marginTop="{some value}dp" to make it look like you want.

RelativeLayout Problems with differences in height

In my android studio it should be like this
http://i.imgur.com/iCvntB9.png
but when I run it on my phone it shows up like this with the 2 edittexts that have moved up a bitin relation compared to the backround screen.
http://i.imgur.com/2Cmjz4B.png
I am relatively new to android and would like help on how to make sure that it will be in ratio and work on different phone sizes. It is the 2 editTexts that have changed position. Thanks in advance
XML CODE FOR REFERENCE
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/imageView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="#drawable/mainscreen" />
<ImageView
android:layout_width="260dp"
android:layout_height="170dp"
android:id="#+id/imageView"
android:background="#drawable/title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/button"
android:background="#drawable/predict_b"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:text="Enter Name"
android:maxLength="20"
android:linksClickable="false"
android:layout_below="#+id/imageView"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button"
android:layout_marginTop="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:text="Age"
android:maxLength="20"
android:linksClickable="false"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_below="#+id/editText" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="43dp"
android:layout_below="#+id/editText2"
android:layout_toLeftOf="#+id/button"
android:layout_toStartOf="#+id/button" />
why not using LinearLayout instead ?
use LinearLayout with orientation vertical
or you can also use it with weightSum tag
Add this to your EditTExt:
android:textAlignment="center"
android:gravity="center"
Add the following attribute to the activity in your AndroidManifest.xml:
android:windowSoftInputMode="adjustResize"
Try modifying your layout as:
<?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">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#drawable/urLogiOrTitle" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="20dp"
android:linksClickable="false"
android:maxLength="20"
android:text="Enter Name" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="20dp"
android:linksClickable="false"
android:maxLength="20"
android:text="Age" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="predict" />
</RelativeLayout>

Format Button Sizes within a Relative Layout

I am creating a feedback form, and at the end there are two buttons, "Clear" and "Send". This has to be done within a Relative Layout.
The problem is the buttons are not wide enough, I want the buttons to match the width of what is above them.
Here is the app before trying to change the width:
And the 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/feedback_name_hint" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:hint="#string/feedback_email_hint" >
</EditText>
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/ratingBar1"
android:layout_alignLeft="#+id/editText2"
android:layout_alignRight="#+id/editText2"
android:layout_below="#+id/editText2"
android:gravity="center_vertical|top"
android:hint="#string/feedback_actual"
android:inputType="textMultiLine" >
</EditText>
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Button01"
android:layout_alignLeft="#+id/editText3"
android:layout_marginBottom="21dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/ratingBar1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="18dp"
android:text="#string/Clear" />
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toRightOf="#+id/button1"
android:text="#string/Send" />
</RelativeLayout>
I have tried to create a linear layout around the buttons, like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:orientation="horizontal" >
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Clear"
/>
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Send"
/>
</LinearLayout>
However, whilst the buttons are the size that I want, they jump to the top of the view, and affect the other elements:
Any help on this would be greatly appreciated, thanks.
SOLVED:
It worked by surrouding buttons with the following linear layout: (note how Rating Bar is set to be above the linearlayoutid)
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearlayout_id"
android:layout_alignLeft="#+id/editText3"
android:layout_marginBottom="21dp" />
<LinearLayout
android:id="#+id/linearlayout_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_horizontal"
android:layout_marginBottom="18dp"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Clear" />
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Send" />
</LinearLayout>
Try this
Replace your xml code with the following.
Here I have added android:layout_above="#+id/linearlayout_id" to RatingBar and have put your buttons in LinearLayout by giving android:layout_weight="1" to both buttons
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearlayout_id"
android:layout_alignLeft="#+id/editText3"
android:layout_marginBottom="21dp" />
<LinearLayout
android:id="#+id/linearlayout_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_horizontal"
android:layout_marginBottom="18dp"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#String/Clear" />
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#String/Send" />
</LinearLayout>
This Should work
Your attempt of putting them in a LinearLayout is correct. Simply add android:layout_alignParentBottom="true" to the LinearLayout and it should work for you.
Update:
Also, change android:layout_height="fill_parent" to android:layout_height="wrap_content" in the LinearLayout.

Incorrect layout when keyboard is shown

I have TextView in RelativeLayout, which works properly:
<TextView
android:id="#+id/tv_client_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv_label_client_out"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/tv_label_client_in"
android:background="#drawable/border"
android:ems="10" />
Now I need to place this TextView inside of ScrollView:
<ScrollView android:id="#+id/sv_client_in"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="#+id/tv_label_client_out"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/tv_label_client_in"
android:background="#drawable/border"
android:fillViewport="true"
>
<TextView
android:id="#+id/tv_client_in"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
/>
</ScrollView>
After this change layout looks OK (first image), but when keyboard is shown, it is corrupted (second image). How can I fix this?
Full layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv_label_client_host"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:ems="10"
android:text="#string/label_host" />
<EditText
android:id="#+id/et_client_host"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/tv_label_client_host"
android:ems="10"
android:inputType="text"
android:lines="1" />
<TextView
android:id="#+id/tv_label_client_port"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/et_client_host"
android:ems="10"
android:text="#string/label_port" />
<EditText
android:id="#+id/et_client_port"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/tv_label_client_port"
android:ems="10"
android:inputType="number"
android:lines="1" />
<ScrollView android:id="#+id/sv_client_in"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="#+id/tv_label_client_out"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/tv_label_client_in"
android:background="#drawable/border"
android:fillViewport="true"
>
<TextView
android:id="#+id/tv_client_in"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
/>
</ScrollView>
<Button
android:id="#+id/btn_client_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="#string/btn_send"
android:onClick="OnBtnSendClick"
/>
<TextView
android:id="#+id/tv_label_client_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btn_client_send"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:ems="10"
android:text="#string/label_client_out" />
<EditText
android:id="#+id/et_client_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/btn_client_send"
android:ems="10"
android:inputType="text"
android:lines="1" />
<Button
android:id="#+id/btn_client_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/et_client_port"
android:text="#string/btn_client_clear"
android:onClick="OnBtnClearClick"
/>
<Button
android:id="#+id/btn_client_disconnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/et_client_port"
android:text="#string/btn_client_disconnect"
android:onClick="OnBtnDisconnectClick"
/>
<Button
android:id="#+id/btn_client_connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/et_client_port"
android:layout_toLeftOf="#+id/btn_client_disconnect"
android:text="#string/btn_client_connect"
android:onClick="OnBtnConnectClick"
/>
<TextView
android:id="#+id/tv_label_client_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/btn_client_connect"
android:ems="10"
android:text="#string/label_client_in"
/>
</RelativeLayout>
This happens mainly because of the views aligning to bottom. You have aligned them to bottom, so when the keyboard pops up the bottom changes hence the Views associated with it.
Try checking your xml file.
The problem is solved by using LinearLayout.

Categories

Resources