I'm inexperienced with Android and working on a layout that is supposed to have a bottom area with 3 buttons displayed this way:
I managed to almost get it like this, except that depending on the screen, the bottom buttons will be either overlapping each other or too far off. The buttons are supposed to be perfectly aligned below the "APPLY" button, but I can't seem to get it right.
Here's my current code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp">
<RelativeLayout
android:id="#+id/relapply"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ripple_buttons_carousel"
android:text="#string/apply_button"
android:textSize="15sp"
android:textColor="#android:color/white"
android:width="320dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="bottom"
android:layout_below="#id/relapply">
<Button
android:id="#+id/discard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ripple_buttons_carousel"
android:text="#string/discard_button"
android:textSize="15sp"
android:textColor="#android:color/white"
android:layout_marginStart="30dp"
android:width="154dp"
android:layout_alignParentStart="true"/>
<RelativeLayout
android:layout_width="12dp"
android:layout_height="30dp"
android:layout_toLeftOf="#id/save">
</RelativeLayout>
<Button
android:id="#+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ripple_buttons_carousel"
android:text="#string/save_button"
android:textSize="15sp"
android:textColor="#android:color/white"
android:layout_marginEnd="30dp"
android:width="154dp"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
This is my code for the layout in which the buttons are in. And then everything's inside a linear layout xml test file I made. Any tips on how to improve this?
Thanks!
first of all you have way to much relative layouts, much more than you need.
you can try this layout here (I removed the background and hardcoded the text for demonstration purposes)
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<Button
android:id="#+id/apply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Top Button"
android:textColor="#android:color/white"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/apply"
android:orientation="horizontal">
<Button
android:id="#+id/discard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bottom Left Button"
android:textColor="#android:color/white"
android:textSize="15sp" />
<Button
android:id="#+id/save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bottom Right Button"
android:textColor="#android:color/white"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
you can set the layout margins on the outer relative layout (e.g. android:layout_margin="8dp"
the layout works like this:
in the top row there is just the top Button. Below it there is a horizontal linear layout containing two buttons with
android:layout_width="0dp"
android:layout_weight="1"
This ensures both buttons span equally horizontal (because they have the same width). If you want the whole layout have a specific width just set the android:layout_width="match_parent" from the relative layout to android:layout_width="320dp"
You can do as follow:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="#color/background_floating_material_dark">
<Button
android:id="#+id/apply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ripple_buttons_carousel"
android:text="#string/apply_button"
android:textSize="15sp"
android:textColor="#android:color/white"
android:layout_marginBottom="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/discard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/ripple_buttons_carousel"
android:text="#string/discard_button"
android:textSize="15sp"
android:textColor="#android:color/white"
android:layout_weight="0.9" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"></LinearLayout>
<Button
android:id="#+id/save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/ripple_buttons_carousel"
android:text="#string/save_button"
android:textSize="15sp"
android:textColor="#android:color/white"
android:layout_weight="0.9" />
</LinearLayout>
</LinearLayout>
And the output is near about this:
You can also use following code.....
This is simple and easy.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Apply" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="Discard" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="Save" />
</LinearLayout>
</LinearLayout>
Related
I want to align button on the right side of the view in linear layout.
This is causing both the button on left size and i tried relative layout in that i cause one button left and other on right.
I want both the button side by side with gap of 10 in between.
Please help.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageButton
android:background="#null"
android:id="#+id/reply_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:src="#drawable/reply"
android:layout_gravity="left"/>
<Button
android:id="#+id/readmore_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#null"
android:text="Read more.."
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="14sp" />
</LinearLayout>
You can set android:layout_weight="2" to your container and for every child set android:layout_weight="1" and android:layout_width="0dp" 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="horizontal"
android:layout_weight="2">
<ImageButton
android:background="#null"
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:layout_gravity="left"/>
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#null"
android:text="Read more.."
android:textAllCaps="false"
android:layout_weight="1"
android:textColor="#android:color/white"
android:textSize="14sp" />
</LinearLayout>
But if you already developed some ios apps before as your name is implying, you may find it more easy to work with ConstarintLayout - one layout to fit all screen sizes (its very similar to the drag and drop editor in ios I just cant remember its name)
Here is an example using ConstraintLayout:
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/button2"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="#+id/button"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/button" />
</android.support.constraint.ConstraintLayout>
Edit according to what you asked:
<?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="horizontal"
android:layout_weight="3">
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="left"
android:layout_gravity="start"/>
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:text="Read more.."
android:layout_weight="1"
android:textSize="14sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="right"
android:layout_weight="1"/>
</LinearLayout>
This is my LayoutActivity, and above the LinearLayout where are the "No, Name, Price and Change 24H." appear a big gap on my phone (Samsung S7), look here. On my Android Studio virtual machine, there isn't one, look here. What is causing this gap and how to fix it?
I include the LayoutActivity code:
<?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"
tools:context=".HomeActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/white"
android:elevation="10dp"
android:weightSum="3">
<Button
android:id="#+id/home_button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:background="#5078909c "
android:text="Overview"
android:textStyle="bold" />
<Button
android:id="#+id/portfolio_button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:background="#android:color/white"
android:text="Portfolio" />
<Button
android:id="#+id/settings_button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:background="#android:color/white"
android:text="Settings" />
</LinearLayout>
<Button
android:id="#+id/refresh_button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginStart="0dp"
android:text="Refresh" />
<ListView
android:id="#+id/CoinList"
android:layout_width="match_parent"
android:layout_height="385dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="48dp" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_above="#+id/CoinList"
android:background="#android:color/white"
android:layout_alignParentEnd="true"
android:elevation="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/Background_for_buttons"
android:layout_weight="1"
android:background="#803f51b5"
android:gravity="center"
android:text="No"
android:textColor="#FF000000"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="128dp"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/Background_for_buttons"
android:layout_weight="1"
android:background="#803f51b5"
android:gravity="center"
android:text="NAME"
android:textColor="#ff000000"
android:textStyle="bold" />
<TextView
android:id="#+id/textView4"
android:layout_width="128dp"
android:layout_height="30dp"
android:layout_below="#+id/Background_for_buttons"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:background="#803f51b5"
android:gravity="center"
android:text="PRICE"
android:textColor="#ff000000"
android:textStyle="bold" />
<TextView
android:id="#+id/text_view_test"
android:layout_width="128dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_below="#+id/Background_for_buttons"
android:layout_weight="1"
android:background="#803f51b5"
android:gravity="center"
android:text="CHANGE 24H."
android:textColor="#ff000000"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
Your ListView has a fixed height and is aligned with parent's bottom. Your LinearLayout with headers is put on top of the ListView. So based on vertical resolution of the screen there may or may not be a gap between the LinearLayout and the top of the screen. More specifically, if the screen is taller than 30+385 dp you'll see a gap.
What you probably want to do is align the headers layout with the top of the parent, align the list to the bottom of the headers and make lists's height "match_parent"
Edit: didn't notice the refresh button. You may also need to put
android:layout_above="#+id/refresh_button"
on the ListView
Hello stackoverflow community!
I'm newbie both in programming and on this site, but let's get to the point.
I wrote an app:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"
tools:context="com.example.android.courtcounter.MainActivity"
android:padding="24dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="Team A"
android:textSize="14sp"
android:fontFamily="sans-serif-medium"/>
<TextView
android:id="#+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="0"
android:textSize="56sp"
android:textColor="#000000"
android:fontFamily="sans-serif-light" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="plus3a"
android:text="+3 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="plus2a"
android:text="+2 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="plus1a"
android:text="Free throw" />
</LinearLayout>
<View
android:layout_width="1dp"
android:background="#android:color/darker_gray"
android:layout_height="match_parent">
</View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"
tools:context="com.example.android.courtcounter.MainActivity"
android:padding="24dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="Team B"
android:textSize="14sp"
android:fontFamily="sans-serif-medium"/>
<TextView
android:id="#+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="0"
android:textSize="56sp"
android:textColor="#000000"
android:fontFamily="sans-serif-light"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="plus3b"
android:text="+3 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="plus2b"
android:text="+2 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="plus1b"
android:text="Free throw" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:onClick="scoreReset"
android:text="reset" />
</LinearLayout>
</LinearLayout>
I have a problem with positioning a button at the end.
I want it to be at the bottom of a screen and centered horizontally.
I know I could use relative layout but I decided to check if I can do this using linear layout. And seems that I can't... "bottom" attribute is being ignored.
Simply do one change.
Remove this line from the last button
android:layout_gravity="bottom|center_horizontal"
And add this line in the Linear layout which is containing your last button
android:gravity="bottom|center"
Here is the output:
Follow this link to get a clear idea about gravity and layout_gravity. It'll surely help you in your upcoming designs.
You can add this View before last button:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Add this -->
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:onClick="scoreReset"
android:text="reset" />
</LinearLayout>
Try encapsulating the main layout in one LinearLayout with layout_weight = 1, layout_width = "match_parent", layout_height = "match_parent"and button at bottom in another LinearLayout with layout_width="match_parent" and gravity set to bottom|center_horizontal.
This should work :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="bottom|center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:onClick="scoreReset"
android:text="reset"/>
</LinearLayout>
use gravity="bottom" instead of layout_gravity it should help.
I want my reset button to be at bottom and horizontally centered, but layout_gravity is not working, only one of them is working.
I know it can be done by choosing RelativeLayout as my parent layout but I want to do it with LinearLayout as my parent layout.
I just want reset button as bottom and horizontally centered rest everything is fine.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.courtcounter.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="TEAM A" />
<TextView
android:id="#+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="button3"
android:text="+3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="button2"
android:text="+2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="button1"
android:text="Free Throw" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="TEAM B" />
<TextView
android:id="#+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="button3_b"
android:text="+3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="button2_b"
android:text="+2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="button1_b"
android:text="Free Throw" />
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:onClick="resetButton"
android:padding="8dp"
android:text="RESET" />
</LinearLayout>
Use this instead if you don't want to go completely relative (inside the parent Linear Layout):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:onClick="resetButton"
android:padding="8dp"
android:text="RESET"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
or if you don't want to keep the reset button inside a relative layout, there doesn't seem to be a solution.
You can check this out as well: How to align views at the bottom of the screen?
Since the rest of your ViewGroups in that layout fill the parent's width, you could simply set the parent LinearLayout's gravity to center_horizontal and it should line up properly, without affecting the other children:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context="com.example.android.courtcounter.MainActivity">
Edit: I didn't notice you also wanted it at the bottom... This can be achieved with an invisible weighted View between your content and the bottom:
...
<View
android:layout_width="1dip"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="resetButton"
android:padding="8dp"
android:text="RESET" />
</LinearLayout>
Edit 2: Removed layout_gravity on your button since it no longer applies.
I m trying to have a textview and a button in linear layout with horizontal orientation. The textview should appear at the starting and the button should appear at the end. I thought giving gravity right to the button would do the trick but the buttons doesn't move to the right side. I m thinking if I should probably use relative layout?
<\LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/productPriceTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rs 3579.0"
/>
<Button
android:id="#+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Buy Now" />
<\/LinearLayout>
My way (using a RelativeLayout):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/productPriceTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Rs 3579.0"
/>
<Button
android:id="#+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Buy Now"
/>
</RelativeLayout>
See how I explicitly align the TextView to the Parent's left side and the Button to the Parent's right side
You can then center the TextView vertically in the RelativeLayout, by setting:
android:layout_centerVertical="true"
in the TextView itself
Newr OS versions may prefer this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/productPriceTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="Rs 3579.0"
/>
<Button
android:id="#+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="Buy Now"
/>
</RelativeLayout>
Try below xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/productPriceTextView1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Rs 3579.0"
/>
<Button
android:id="#+id/buyNowButton1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Buy Now" />
</LinearLayout>
There is a cleaner way to do this, using LinearLayout : just give the left element a width of 0 and a weight of 1, and set the right one's width on wrap_content. And that's it!
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/productPriceTextView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rs 3579.0"
/>
<Button
android:id="#+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Now" />
</LinearLayout>
There are two approach:
1) You can use RelativeLayout in which you can drag your Button where you want..
2) You can use weight property for LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/productPriceTextView1"
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Rs 3579.0"
/>
<Button
android:id="#+id/buyNowButton1"
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Buy Now" />
</LinearLayout>
Use this layout instead..
<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="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/productPriceTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rs 3579.0"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Now"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
use android:layout_gravity="end" to your linear layout to move all the elements to the right
Another simple solution here that doesn't rely on using a RelativeLayout is having an empty view between both elements using layout_weight to make sure it fills the empty space.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/productPriceTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rs 3579.0"/>
<view
android:layout_height="0dp"
android:layout_width="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Buy Now" />
</LinearLayout>
That way you can achieve what you want by only using a LinearLayout, now, I am not sure if this is more efficient than using a RelativeLayout. But for me it always feels a bit like an overkill to go for a relative in such a simple layout.
Just add empty view with 0dp width and 1 weight
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/productPriceTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rs 3579.0"
/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="#+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Now" />
</LinearLayout>