How to make a button stay at the bottom of the screen - android

Hello all im getting really frustrated with my button, how do i make it so it stays at the bottom of the screen,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
?

You can do that by replacing the LinearLayout with RelativeLayout like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/* this puts the textview at the top of the relative layout */
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/* this puts the textview1 at the bottom of the first textview */
android:layout_below="#id/textView1"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/* this puts the button at the bottom of the RelativeLayout */
android:layout_alignParentBottom="true"
android:text="Button" />
</RelativeLayout>
Remove the comment lines if you copy and paste the code because are not working in xml...

Add the empty View with weight 1 between your text views and the button. This view will expand over unused space, keeping the button at the bottom.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

Please copy this code and use, it will work...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

Related

Textview in Linear Layout Appears on the wrong position

I have the Below Layout. I want the linear layout that holds the Textviews to appear below the PieChart. However it appears on top and on smaller screens the piechart overlays over the text.
<?xml version="1.0" encoding="utf-8"?>
<uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/catexprpt_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.echo.holographlibrary.PieGraph
android:id="#+id/piegraph"
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/tableRowN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:layout_below="#+id/piegraph"
android:orientation="horizontal" >
<TextView
android:id="#+id/titleN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rounded_edges"
android:text="#string/note"
android:textColor="#ffffff"
android:textSize="13sp"
android:typeface="serif" />
<TextView
android:id="#+id/notice3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:typeface="serif" />
</LinearLayout>
Can you try with the PieGraph as a child of the LinearLayout.
I'm not sure about ActionBarSherlock's PullToRefreshLayout but the Android implementation SwipeRefreshLayout supports only one direct child view.
Edit:
I'm suggesting something like this as an only child to the Refresh layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.echo.holographlibrary.PieGraph
android:id="#+id/piegraph"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip" />
<TextView
android:id="#+id/titleN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/piegraph"
android:background="#drawable/rounded_edges"
android:text="#string/note"
android:textColor="#ffffff"
android:textSize="13sp"
android:typeface="serif" />
<TextView
android:id="#+id/notice3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/piegraph"
android:textColor="#000000"
android:typeface="serif" />
</RelativeLayout>
More on layout optimization here.
I managed to solve it as below thanks to # LordRaydenMK :
<?xml version="1.0" encoding="utf-8"?>
<uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/catexprpt_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/tableRowM"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.echo.holographlibrary.PieGraph
android:id="#+id/piegraph"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/tableRowN"
android:paddingBottom="20dp" />
<LinearLayout
android:id="#+id/tableRowN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true">
<TextView
android:id="#+id/titleN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rounded_edges"
android:text="#string/note"
android:textColor="#ffffff"
android:textSize="13sp"
android:typeface="serif" />
<TextView
android:id="#+id/notice3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Heloooooo Mr. Fish"
android:typeface="serif" />
</LinearLayout>

XML Layout overlap

I am trying to create a certain layout, that consists of 2 parts a scrollview and a "footer" at the bottom, but always on top of the scrollview. Everything inside the scrollview must be visible (by scrolling).
This is my current code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/banner" />
<TextView
android:layout_width="fill_parent"
android:paddingTop="1dp"
android:layout_height="3px"
android:background="#DADADA" />
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Start Connecting" />
<Button
android:id="#+id/button01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_alignParentLeft="true"
android:text="First use" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Welcome to ADB Wireless!"
android:textSize="25sp" />
</RelativeLayout>
</LinearLayout>
But it gives an overlap on small screens & landscape mode. I would only like to have the 2 buttons to be inside the 'footer'. Thank you!
<?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:orientation="vertical" >
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Next" />
<Button
android:id="#+id/button01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Previous" />
</LinearLayout>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/button_layout" >
</ScrollView>
</RelativeLayout>

Android: how to align views vertically and horizontally

I'm wondering how I can align the buttons as the following: Button1 and Button2-2 are supposed to align horizontally, Button2-1 and Button2-2 should align vertically.
[Button2-1]
[other views] [Button1] [Button2-2]
Here is my code but it doesn't work:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ButtonGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="#+id/Button2-1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<Button
android:id="#+id/Button2-2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
EDITED:
To be more clear, I posted more code here and hopefully it could explain my needs better:
What I want is to align the text view horizontally with the image buttons while vertically with the seekbar above it.
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ButtonGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/Button00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background00"
android:gravity="bottom"
android:scaleType="center"
android:src="#drawable/button00" />
<ImageButton android:id="#+id/Button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/button11"
android:scaleType="center"
android:gravity="bottom"
android:background="#drawable/background11"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical" >
<SeekBar
android:id="#+id/SeekBar1"
style="#style/MySeekBar1"
android:layout_width="60dp"
android:layout_height="35dp"
android:layout_marginBottom="20dp"
/>
<TextView
android:id="#+id/MyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:background="#drawable/MyTextImg"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Use RelativeLayout. Stacking LinearLayouts is usually not the way to go!
https://developer.android.com/reference/android/widget/RelativeLayout.html
https://developer.android.com/guide/topics/ui/layout/relative.html
Try changing the gravity attribute of the first button (Button 1) to bottom.
As someone suggested, use a RelativeLayout to design this type of pattern! Here's an example for your situation:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ButtonGroups"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/Button2-2"
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="button 2-2" />
<Button
android:id="#+id/Button2-1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Button2-2"
android:layout_alignLeft="#+id/Button2-2"
android:text="button 2-1" />
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="54dp"
android:text="button 1" />
</RelativeLayout>
I hope this get you started!
Try the code below:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">
<TableRow >
<TextView />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2-1" />
</TableRow>
<TableRow >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2-2" />
</TableRow>
</TableLayout>
The trick is to use a blank text view for the empty space in the first row. Let me know if this helps you. :)

Fill RelativeLayout parent with LinearLayout child

This seems simple but I've exhausted my Google search patience.
I have a RelativeLayout that has two sub LinearLayouts side-by-side and I want them both to vertically fill the parent, no matter the height of the two LinearLayouts. This is being used as a ListView row and the content under #id/item_left is generally shorter than the one on the right.
The background drawable doesn't fill the entire row.
Any ideas on making both sides fill?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/item_left"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingRight="2dp"
android:paddingLeft="2dp"
android:background="#drawable/item_left"
android:orientation="vertical">
<TextView android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/item_left_text"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/item_right_background"
android:layout_toRightOf="#id/item_left">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/item_right_text"
android:text="Row1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/item_right_text"
android:text="Row2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/item_right_text"
android:text="Row3" />
</LinearLayout>
</RelativeLayout>
try this code .... sorry but I changed the color to so the impact when you will change the text of first TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="fill_parent"
android:background="#ffffffff" android:weightSum="1">
<LinearLayout android:id="#+id/item_left"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:paddingRight="2dp" android:paddingLeft="2dp"
android:background="#ff0000ff" android:orientation="vertical" >
<TextView android:id="#+id/time" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#ff555555"
android:text="sdfdsf s ada d a asd sad sad" />
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="match_parent" android:orientation="vertical"
android:background="#ffff0000"
android:layout_weight="1" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#ff555555"
android:text="Row1" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#ff00ff00"
android:text="Row2" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#ff00ff00"
android:text="Row3" />
</LinearLayout>
</LinearLayout>
<?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" >
<LinearLayout
android:id="#+id/layout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="helloooooooooooooo" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/layout1"
android:background="#0000FF"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row3" />
</LinearLayout>
</RelativeLayout>

Align two-button layout left and right in Android

I want to align two buttons with a linear layout, one on the left, and one on the right, like the next and previous buttons on an image gallery. I tried to align them but it doesn't work.
XML layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#android:color/white"
android:gravity="bottom" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="prev"
android:layout_alignParentRight="true" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />
</LinearLayout>
</LinearLayout>
Actual output:
Expected output:
How can I fix it?
Use a RelativeLayout. There you can set android:layout_alignParentLeft and android:layout_alignParentRight.
This should work for you:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#android:color/white"
android:gravity="bottom" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="prev"
android:layout_alignParentLeft="true" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
With Linear Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"/>
</LinearLayout>
With Relative Layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Use Relative layout in your LinearLayout;
Also add android:layout_alignParentLeft="true" to "prev" Button
and android:layout_alignParentRight="true" to "next" Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:gravity="bottom"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" <---- ADD this prop
android:text="prev" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" <----- ADD this prop
android:text="next" />
</RelativeLayout>
</LinearLayout>
I guess, the simplest solution is to use View in your inner LinearLayout.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="prev"/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next"/>
</LinearLayout>
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Button" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/button1"
android:orientation="vertical" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
The idea is, to create a RelativeLayout as container and put first Button and then stick it to the right of parent view. After that, add Another button inside LinearLayout and set this LinearLayout into the left of first Button.
Here's the result.
ur layout xml should be as shown below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:background="#fff">
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
android:layout_gravity="bottom" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Pre" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Next" />
</RelativeLayout>
</RelativeLayout>
Use a RelativeLayout instead of a LinearLayout you can add the tags android:layout_alignParentRight="true" and android:layout_alignParentLeft="true" on each button.
You can use RelativeLayout like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="prev" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="next" />
</RelativeLayout>
</LinearLayout>
(I used grey instead of white for the screenshot)
Use a FrameLayout and add the layout_gravity attribute in each of the buttons
Do something like this it will surely help you out
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Linear"
android:textSize="35dp"
android:layout_margin="90dp"
/>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relative"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="200dp"
android:text="Constraint" />
</LinearLayout>
</LinearLayout>
Use margin_Start and End to seprate the buttons in the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-:Linear Layout:-"
android:textSize="35sp"
android:layout_gravity="center"
android:layout_marginTop="250dp"
tools:ignore="HardcodedText"/>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="90dp">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relative"
tools:ignore="ButtonStyle,HardcodedText"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="200dp"
android:text="Constraint"
tools:ignore="ButtonStyle"
/>
</LinearLayout>
</LinearLayout>
[enter image description here][1]
[1]: https://i.stack.imgur.com/NMYj2.png
For Better Performance:
Best to put two views left and right and one view between in linear layout rather than relative layout. Use the below code it will also remove the nesting of views .... It will be good in performance perpective :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="prev" />
<TextView android:text="Hi its working"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />
</LinearLayout>

Categories

Resources