Android Button Place Image in center and text at bottom

Rohit picture Rohit · Mar 7, 2013 · Viewed 38.4k times · Source

I want to place Image in the center and text just below it in a button, I tried to set android:gravity but could not set it properly,here is the image attached:

enter image description here

below is my 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="match_parent"
    android:orientation="vertical"
    android:paddingLeft="10.0dip" 
    android:paddingTop="10.0dip" 
    android:paddingRight="10.0dip" 
    android:paddingBottom="10.0dip"    
    android:background="@drawable/background_img"
    >

    <LinearLayout
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:paddingLeft="10.0dip" 
        android:paddingTop="10.0dip" 
        android:paddingRight="10.0dip" 
        android:paddingBottom="10.0dip"          
        >

        <Button          
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_listen"
            android:text="@string/listen"
            android:drawableTop="@drawable/listen_btn"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"      
            android:layout_gravity="center"
            />
        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_gallery"
            android:text="@string/gallery"
            android:drawableTop="@drawable/gallery_btn"
            android:layout_marginLeft="10dp"    
            android:layout_marginRight="10dp"    
            android:layout_marginTop="10dp"    
            android:layout_marginBottom="10dp"   
            android:layout_gravity="center"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:paddingLeft="10.0dip" 
        android:paddingTop="10.0dip" 
        android:paddingRight="10.0dip" 
        android:paddingBottom="10.0dip"              
        >

        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_play"
            android:text="@string/play"                
            android:drawableTop="@drawable/play_btn"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            />
        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_find"
            android:text="@string/find"
            android:drawableTop="@drawable/test_btn"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"    
            android:layout_marginBottom="10dp" 
            android:layout_gravity="center"
            />        
    </LinearLayout>
</LinearLayout>

What is the proper way to center the image and text below it?

Many thanks in advance.

Answer

DjHacktorReborn picture DjHacktorReborn · Mar 7, 2013

Try this its work for me

<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"
    tools:context=".MainActivity" >
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="151dp"
        android:onClick="onClick"
        android:drawableTop="@drawable/ic_launcher"
        android:text="Button" />
</RelativeLayout>