Anthony Lo
Anthony Lo

Reputation: 17

Android Linear Layout (3 buttons) cannot centre of the screen

I want to make the three button should be align centre of the '+id/imageRecorder'. But the 3 buttons are start from the edge of the screen now, hopes for help

And here is the xml code, how to make the three buttons are centre of the screen.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/myBackground"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/imageRecorder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:src="@drawable/recorder" />

    <RadioGroup
        android:id="@+id/radioGroupAudio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"        
        android:layout_marginTop="20dp"
        android:layout_below="@+id/imageRecorder"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radiowav"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="@string/wav"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/myText" />

        <RadioButton
            android:id="@+id/radiomp3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="@string/mp3"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/myText" />
    </RadioGroup>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="30dp"
        android:layout_centerHorizontal="true"                
        android:layout_below="@+id/radioGroupAudio"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/buttonStart"
            android:layout_width="76dp"
            android:layout_height="44dp"
            android:background="#00FFFFFF"
            android:scaleType="fitXY"
            android:layout_marginRight="20dp"
            android:layout_toLeftOf="@+id/buttonPause"
            android:src="@drawable/play_off" />

        <ImageButton
            android:id="@+id/buttonPause"
            android:layout_width="76dp"
            android:layout_height="44dp"
            android:background="#00FFFFFF"
            android:scaleType="fitXY"
            android:layout_below="@+id/imageRecorder"
            android:layout_centerInParent="true"            
            android:src="@drawable/pause_off" />

        <ImageButton
            android:id="@+id/buttonStop"
            android:layout_width="76dp"
            android:layout_height="44dp"
            android:background="#00FFFFFF"
            android:scaleType="fitXY"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/buttonPause"
            android:src="@drawable/stop_off" />
    </LinearLayout>

</RelativeLayout>

Upvotes: 0

Views: 1599

Answers (2)

codeMagic
codeMagic

Reputation: 44571

Changing your width of your LinearLayout to wrap_content ought to take care of it. With fill_parent it will want to take up the entire width so naturally it will start at the beginning of the screen

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_centerHorizontal="true"                
    android:layout_below="@+id/radioGroupAudio"
    android:orientation="horizontal" >

Also, change the height to wrap_content because you don't want to tell it to take up the whole screen. Note that fill_parent has been deprecated so you should use match_parent instead

Upvotes: 2

GromDroid
GromDroid

Reputation: 230

Try to change:

    android:layout_centerHorizontal="true" 

to:

    android:gravity="center_horizontal"

in the LinearLayout with the 3 buttons

Upvotes: 1

Related Questions