Space to right and left inside EditText - android

I'm trying to create an EditText with 2 buttons inside (right and left). I want to give it a padding on each side so that the text not appears behind the buttons. But I can not. I tried with padding, margin... but nothing works for me. What can be the error?
<LinearLayout
android:id="#+id/buscar_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal"
android:visibility="gone" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/buscar_monumento"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="15"
android:gravity="center_horizontal"
android:hint="Buscar..."
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:textColor="#color/negro"
android:textStyle="bold" >
</EditText>
<Button
android:id="#+id/buscar_lupa"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="7dp"
android:background="#drawable/buscar_lupa" />
<Button
android:id="#+id/buscar_salir"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="7dp"
android:background="#drawable/buscar_salir" />
</FrameLayout>
</LinearLayout>
NEW CODE
<LinearLayout
android:id="#+id/buscar_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal"
android:visibility="gone" >
<EditText
android:id="#+id/buscar_monumento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:gravity="center_horizontal"
android:hint="Buscar..."
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:textColor="#color/negro"
android:textStyle="bold" >
</EditText>
<Button
android:id="#+id/buscar_lupa"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="7dp"
android:background="#drawable/buscar_lupa" />
<Button
android:id="#+id/buscar_salir"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="7dp"
android:background="#drawable/buscar_salir" />
</LinearLayout>

don't use FrameLayout, use RelativeLayout instead
framelayout is best used for single item, or wrap LinearLayout around the Button and TextView if you insists on using FrameLayout
or better, change the parent viewgroup LinearLayout to RelativeLayout, else the ADT will tell a warning about the useless viewgroup

If you just need two buttons on the sides of edit text simplest way is this
<LinearLayout
android:id="#+id/buscar_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<Button android:background="#drawable/ic_launcher"
android:id="#+id/buscar_lupa"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="7dp"/>
<EditText
android:id="#+id/buscar_monumento"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:ems="15"
android:gravity="center_horizontal"
android:hint="Buscar..sdsdsdsds."
android:text="1234567890123456789012345678901234567890"
android:imeOptions="actionDone"
android:inputType="text"
android:textColor="#f00"
android:textStyle="bold" >
</EditText>
<Button android:background="#drawable/ic_launcher"
android:id="#+id/buscar_salir"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="7dp"/>
</LinearLayout>

First of all in your new code segment you have the parent linear layout visibility set to gone
Secondly use a relative layout it's simple
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/buscar_monumento"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="15"
android:hint="Buscar..."
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:textColor="#color/negro"
android:textStyle="bold" >
</EditText>
<Button
android:id="#+id/buscar_lupa"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/buscar_lupa" />
<Button
android:id="#+id/buscar_salir"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="7dp"
android:background="#drawable/buscar_salir" />

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.

Move Layout Up when Soft Keyboard appears

I have login page with Email Id ,Password & LOGIN button. Right now when any one of the Edit Text gain focus then system keyboard hides my Login button. I want to push my layout above when keyboard appears keeping LOGIN button above of keyboard.
P.S I've tried all solution like
adjust Pan|adjust Resize, adjust Pan,adjust Resize"
in manifest as well as in Java code
But none of thing worked for me.
<ScrollView
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/newl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="145dp"
android:drawablePadding="8dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_clock"
android:id="#+id/clock"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timetracker"
android:fontFamily="LucidaGrande"
android:textSize="30dp"
android:textAlignment="center"
android:layout_marginTop="15dp"
android:layout_gravity="center_horizontal"
android:id="#+id/tt"
android:layout_below="#+id/clock"
android:textColor="#FFFFFF"
android:layout_centerHorizontal="true"
/>
<LinearLayout
android:layout_below="#+id/tt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="30dp"
>
<ImageView
android:layout_marginLeft="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_person_black_24dp"
android:layout_centerVertical="true"
/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#FFFFFF"
android:layout_marginLeft="9dp"
android:drawablePadding="12dp"
android:textSize="15dp"
android:layout_marginRight="45dp"
android:fontFamily="Sans Serif"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
android:background="#android:color/transparent"
android:id="#+id/spinner2"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_arrow_drop_down_black_24dp"
android:layout_alignParentRight="true"
android:paddingRight="8dp"
android:layout_centerVertical="true"/>
</RelativeLayout>
<View
android:layout_below="#+id/rel"
android:layout_width="match_parent"
android:background="#FFF"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_height="1dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/lock"
android:drawablePadding="12dp"
android:layout_marginTop="30dp"
android:layout_below="#+id/rel"
android:layout_marginRight="7dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="7dp"
android:hint="Password"
android:background="#android:color/transparent"
android:textColorHint="#FFFFFF"
android:textSize="20dp"
android:textColor="#FFFFFF"
android:id="#+id/editText"
android:inputType="textPassword"
android:backgroundTint="#FFFFFF"
/>
<View
android:layout_below="#+id/editText"
android:layout_width="match_parent"
android:background="#FFF"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_height="1dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_below="#+id/editText"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="#FFFFFF"
android:text="LOG IN"
android:id="#+id/log"
android:textColor="#D04045"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
The solution was to use android:fillViewport="true" on the ScrollView.
and with this you can also use android:windowSoftInputMode="adjustResize"
this worked for me
try this,
Add the below line in your manifest file,
android:windowSoftInputMode="adjustResize"
For example,
<activity android:name=".Game" android:windowSoftInputMode="adjustResize">
</activity>

How to align widgets to other widgets

I am new all around design of UI inn android. and i have a 3 alignments problem in the follwoing image:
1. i would like that the rotatte laeft and right will be under the cent of the butoom "Select Photo".
2. for some reason the spinner near to the mobile edit text is not in the hegiht of the mobile edittext.
3.the "Create Account" buttom should be in the Bottom of the screen. but even when i add the following line:
android:layout_alignParentBottom="true"
nothing happend.
this is what i get in my device:
this is the xml 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:orientation="vertical"
android:background="#ffffffff"
tools:context="com.example.matant.gpsportclient.Controllers.Register">
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/submainLayout"
android:layout_margin="10dp"
android:layout_height="match_parent">
<ImageView
android:src="#drawable/camera"
android:layout_width="120dp"
android:layout_height="120dp"
android:id="#+id/imageViewGallery"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:background="#drawable/border"
android:padding="1dp"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_alignParentStart="true" />
<TableRow
android:layout_width="match_parent"
android:id="#+id/signupRow1"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editTextName"
android:hint="Name"
android:background="#drawable/rounded_edit_text"
android:paddingLeft="10dp"
android:layout_marginTop="160dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:id="#+id/signupRow2"
android:layout_below="#+id/signupRow1"
android:layout_marginTop="5dp"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/editTextEmail"
android:background="#drawable/rounded_edit_text"
android:padding="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Email"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:id="#+id/signupRow3"
android:layout_marginTop="5dp"
android:layout_below="#+id/signupRow2"
android:layout_height="wrap_content">
<Spinner
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/spinnerMobile"
android:layout_marginRight="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:spinnerMode="dialog"
android:layout_marginBottom="5dp"
android:background="#606060"
android:layout_toLeftOf="#+id/buttonSelectImg"
android:layout_toStartOf="#+id/buttonSelectImg"
android:layout_below="#+id/editTextEmail"
android:layout_alignBottom="#+id/editTextMobile"
android:layout_weight="21.24" />
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:inputType="phone"
android:ems="10"
android:id="#+id/editTextMobile"
android:paddingLeft="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/rounded_edit_text"
android:hint="Mobile"
android:layout_weight="40.74" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:id="#+id/signupRow4"
android:layout_marginTop="5dp"
android:layout_below="#+id/signupRow3"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:background="#drawable/rounded_edit_text"
android:id="#+id/editTextPassword"
android:paddingLeft="5dp"
android:hint="Password"
android:fontFamily="sans-serif"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:id="#+id/signupRow5"
android:layout_marginTop="5dp"
android:layout_below="#+id/signupRow4"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:background="#drawable/rounded_edit_text"
android:id="#+id/editTextConfirmPass"
android:hint="Confirm Password"
android:paddingLeft="5dp"
android:fontFamily="sans-serif"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:id="#+id/signupRow6"
android:layout_marginTop="5dp"
android:layout_below="#+id/signupRow5"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Gender:"
android:id="#+id/textViewGender"
android:layout_gravity="left|top"
android:layout_weight="5.18" />
<Spinner
android:layout_width="129dp"
android:layout_height="30dp"
android:id="#+id/spinnerGender"
android:background="#606060"
android:layout_weight="2.84" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:id="#+id/signupRow7"
android:layout_marginTop="5dp"
android:layout_below="#+id/signupRow6"
android:layout_height="wrap_content"
android:baselineAligned="false">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Year of Birth:"
android:id="#+id/textViewyearOfBirth"
android:layout_weight="5.18" />
<Spinner
android:layout_width="129dp"
android:layout_height="30dp"
android:id="#+id/spinnerAge"
android:layout_marginLeft="10dp"
android:background="#606060"
android:layout_weight="3.14" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:id="#+id/signupRow8"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/imageViewGallery"
android:layout_below="#+id/rotateLayout"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Photo"
android:id="#+id/buttonSelectImg"
android:layout_marginTop="5dp"
android:background="#606060"
android:textColor="#ffffffff"
android:layout_marginLeft="5dp"
android:layout_margin="10dp"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:id="#+id/signupRow9"
android:layout_below="#+id/signupRow8"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/imageViewGallery">
</TableRow>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="123dp"
android:background="#606060"
android:layout_marginLeft="5dp"
android:id="#+id/rotateLayout"
android:layout_toRightOf="#+id/imageViewGallery">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButtonRleftt"
android:background="#drawable/rotate_left"
android:layout_gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButtonRright"
android:background="#drawable/rotate_right"
android:onClick="rottateRight"
android:layout_gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<Button
android:id="#+id/ButtonSubmit"
android:text="Create Account"
android:layout_width="match_parent"
android:background="#606060"
android:textColor="#ffffffff"
android:layout_alignParentBottom="true"
android:layout_below="#+id/signupRow7"
android:layout_height="50dp"
android:layout_margin="5dp"
>
</Button>
</RelativeLayout>
Just set the width for both drawables to "wrap_content" and delete the weight attribute. Then add the gravity attribute to the table row like this:
<TableRow
android:layout_width="match_parent"
android:id="#+id/signupRow9"
android:layout_marginTop="5dp"
android:layout_below="#+id/signupRow8"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/imageViewGallery"
android:gravity="center">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButtonRleftt"
android:src="#drawable/rotate_right"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButtonRright"
android:src="#drawable/rotate_right"
android:onClick="rottateRight"/>
</TableRow>
The easiest way is to define a static height for you tablerow and then set the height attribute for your spinner and edittext to "match_parent". Another way is to get the height of your edittext programmatically and then set the height for the spinner at runtime. I would recommend the second method, because it looks better for different devices.
You're using the wrong attribute. Try it with android:layout_alignParentTop="true" to set the View to the top.
For the
1)use a single/separate table layout and add first row as "SELECT PHOTO" using colspan attribute and for the second row add the buttons using weight Attribute.
2)Instead of using warpcontent for both of them set layout_height="50dp" or anything, such that it's similar to other widgets in terms of height.
3)use CreateAccount widget directly without a tablerow with this attribute android:layout_alignParentTop="true" such that the parent of this widget is Relativelayout.

How to align a widget at bottom_vertical | center_horizontal with RelativeLayout

This is the page:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true">
<ImageView
android:id="#+id/imageView1"
android:contentDescription="#string/Image_BG_desrp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/background_main" />
<TextView
android:id="#+id/a1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/1"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="18sp"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:id="#+id/a2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="#string/2"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="28sp"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:id="#+id/a3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_centerHorizontal="true"
android:text="#string/3"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="18sp" />
I want to put this button at bottom_vertical | center_horizontal (center of bottom) page:
<Button
android:id="#+id/btn_xml"
android:layout_width="85dp"
android:layout_height="35dp"
android:gravity="center"
android:text="#string/Continue"
android:textColor="#FFFFFF"
android:textSize="16dp"/>
I put this Button in LinearLayout and use Margin-Top and use some dimension, but with different screen-size it's gonna be problem.
Is there any way to align with RelativeLayout without giving any dimension.
Note: I have to use RelativeLayout!!
Add This in Button Tag:
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
Add
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
to your Button and it will be centered in the bottom of the screen.
try this one
<Button
android:id="#+id/btn_xml"
android:layout_width="85dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="#string/Continue"
android:textColor="#FFFFFF"
android:textSize="16dp" />

How do I align a button with the bottom of an EditText in an Android layout?

With my layout, I would like the EditTexts to fill all of the horizontal space on their row, but leave the remaining space for a button to its right.
I would also like the buttons to be aligned to the bottom of the EditText that is on its row.
Currently, the buttons are not aligned to the bottom of their EditText, and I would like to know how I can achieve this.
Here is a screenshot:
And here is my XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp" >
<LinearLayout
android:id="#+id/nameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/nameEditText"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:ems="8"
android:textSize="15sp"
android:hint="enter name" />
<ImageButton
android:id="#+id/micNameButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/ic_mic" />
</LinearLayout>
<LinearLayout
android:id="#+id/editLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/nameLayout">
<EditText
android:id="#+id/notesEditText"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:ems="8"
android:textSize="15sp"
android:hint="enter notes" />
<ImageButton
android:id="#+id/micNotesButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/ic_mic" />
</LinearLayout>
</RelativeLayout>
Try keeping your margin attributes in the LinearLayout, and not in the actual EditText or ImageButton. And set the gravity of the layout to bottom.
(Edit: Also notice that I added android:orientation="horizontal" to the LinearLayouts. You should always have an orientation when child views use layout_weight)
It should look something like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp" >
<LinearLayout
android:id="#+id/nameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:gravity="bottom" >
<EditText
android:id="#+id/nameEditText"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:ems="8"
android:textSize="15sp"
android:hint="enter name" />
<ImageButton
android:id="#+id/micNameButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/ic_mic" />
</LinearLayout>
<LinearLayout
android:id="#+id/editLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:gravity="bottom"
android:layout_below="#+id/nameLayout">
<EditText
android:id="#+id/notesEditText"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:ems="8"
android:textSize="15sp"
android:hint="enter notes" />
<ImageButton
android:id="#+id/micNotesButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/ic_mic" />
</LinearLayout>
</RelativeLayout>

Categories

Resources