mumf
mumf

Reputation: 11

How do I align the right/left side of a button in android to the center of the screen?

I was thinking it would be something like android:layout_startOf"centerHoriztonal" or something like that but I have searched and couldn't find anything. If it helps I am trying to make a soundboard app where all the buttons can be the same size across multiple device screens.

Upvotes: 0

Views: 2494

Answers (3)

Yurets
Yurets

Reputation: 4009

Are you sure you want the same size of buttons in all devices? People try to avoid it and make make view elements flexible. Anyways if you want to set same size just directly write in layout

android:layout_width = "@dimen/valueForButton"; //some absolute value

For starting at same point, just add all buttons inside LinearLayout and make some operations there, such as

android:layout_gravity = "center_horizontal";

Your layout must be something like this:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="@dimen/valueForButton"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="@dimen/valueForButton"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="@dimen/valueForButton"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

Upvotes: 0

samgak
samgak

Reputation: 24417

You can wrap the button(s) inside a horizontal LinearLayout and use layout_weight to divide the screen in half and then layout_alignParentLeft/layout_alignParentRight to align the buttons inside a RelativeLayout that takes up exactly half the screen width.

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

     <RelativeLayout
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1">

         <Button 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignParentRight="true"
             android:text="Button Text"
             /> 
     </RelativeLayout>

     <RelativeLayout
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1">

     </RelativeLayout>

 </LinearLayout>

Another way to do it is to create a dummy view object that is centered horizontally in the parent view, and align your buttons to the left or right of it:

   <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

      <View
        android:id="@+id/dummyview"
        android:layout_width="0px"
        android:layout_height="0px"
        android:layout_centerHorizontal="true"/>

      <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/dummyview"
        android:text="Button Text"/>

  </RelativeLayout>

Upvotes: 0

myoungjin
myoungjin

Reputation: 915

try this xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/button1"
android:text="Here is button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
/>

<Button
android:id="@+id/button2"
android:text="Here is button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
/>

</RelativeLayout>

Upvotes: 1

Related Questions