RelativeLayout Problems with differences in height - android

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>

Related

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.

Android GUI not displaying properly in relative layout

I am creating gui in relative layout and reason i am using relative layout is that, i am using a fakeview in the middle of the screen to align all the other objects properly in the center of the screen. But the output on my mobile is completely different and is completely stretch out.
My problem is that the black line in the middle is supposed to be just 1dp in height, still it appears as 50dp in height on my device. Also there is huge gap in the middle below exchange rate and above discount amt($). Such problems weren't there in the emulator. the entire screen is divided into two parts, the lower part and upper part and there is gap in middle.
I am using nexus 4 running stock rom 4.4.2 to test my application.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#FFFFE0">
<View android:id="#+id/fakeView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/carat"
android:layout_marginTop="10dp"
android:layout_below="#+id/cut3"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/fakeView"
android:layout_marginRight="20dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/price"
android:layout_above="#+id/caratrs"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/fakeView"
android:layout_marginRight="20dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/list"
android:layout_marginRight="20dp"
android:layout_below="#+id/carat"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/fakeView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/total"
android:layout_marginRight="20dp"
android:layout_above="#+id/calculate"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/fakeView" />
<Spinner
android:drawSelectorOnTop="true"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/shape2"
android:layout_toLeftOf="#+id/color"
android:layout_alignLeft="#+id/cut3" />
<Spinner
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/cut3"
android:layout_below="#+id/shape2"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/fakeView"
android:layout_marginLeft="20dp"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/color"
android:layout_above="#+id/cut3"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/fakeView"
android:layout_marginRight="20dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/clarity"
android:layout_below="#+id/shape2"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/shape2"
android:layout_marginRight="20dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/discount"
android:layout_marginRight="20dp"
android:layout_below="#+id/list"
android:layout_toRightOf="#+id/fakeView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/converter"
android:layout_marginRight="20dp"
android:layout_below="#+id/discount"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/fakeView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:id="#+id/calculate"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/black"
android:layout_below="#+id/converter"
android:layout_marginTop="20dp"
android:layout_alignParentLeft="true"
android:id="#+id/view"
android:layout_above="#+id/textView5" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/discamt"
android:layout_below="#+id/view"
android:layout_above="#+id/price"
android:layout_alignLeft="#+id/fakeView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:layout_marginRight="20dp"
android:id="#+id/caratrs"
android:layout_above="#+id/total"
android:layout_toRightOf="#+id/fakeView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rap Price"
android:id="#+id/textView2"
android:layout_above="#+id/discount"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Discount(%)"
android:id="#+id/textView3"
android:layout_above="#+id/converter"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exchange Rate"
android:id="#+id/textView4"
android:layout_alignBottom="#+id/converter"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Discount Amt($)"
android:id="#+id/textView5"
android:layout_above="#+id/price"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PPC($)"
android:id="#+id/textView6"
android:layout_above="#+id/caratrs"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ppcrs"
android:id="#+id/textView7"
android:layout_above="#+id/total"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/Rs"
android:id="#+id/textView8"
android:layout_above="#+id/calculate"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Carat :"
android:id="#+id/textView9"
android:layout_alignBottom="#+id/carat"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
</RelativeLayout>
In your black view remove
android:layout_above="#+id/textView5"
You'll end up with the following:
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/black"
android:layout_below="#+id/converter"
android:layout_marginTop="20dp"
android:layout_alignParentLeft="true"
android:id="#+id/view" />
You're already anchoring the view to converter
So you don't need the extra anchor.
I strongly recommend you not to use the fake view, from the screenshot, it is better to use linearlayout to hold blanks in the screen, and each blank you can use a relativelayout, in this way, this layout will look well in all devices

ScrollView implementation in 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>

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.

Android EditText background

I have an edittext and I am changing his background at one moment with:
EditText textEmail;
textEmail.setBackgroundColor(Color.RED);
The problem is that the edittext outline disapears and I have a big red thing on the screen.
How I can change this, maybe set a bigger outline ...
Here is my XML layout:
<?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"
android:background="#drawable/bg_white" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#null"
android:src="#drawable/bg_mdpi" />
<EditText
android:id="#+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:hint="Password"
android:inputType="textPassword"
android:password="true"
android:maxWidth="220px"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText2"
android:layout_marginTop="35dp"
android:minWidth="220px"
android:onClick="goLoging"
android:text="Login" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentRight="true"
android:minWidth="220px"
android:text="Forgot Password" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="52dp"
android:visibility="gone" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView1"
android:layout_centerHorizontal="true"
android:hint="Email Adress"
android:inputType="textEmailAddress" />
</RelativeLayout>
Just fix the EditText height and check. It may be because of wrap_content.

Categories

Resources