Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Custom button with imageview and textview inside?

Tags:

java

android

I'm looking to create a custom button. This button ideally would have an image on the left and a textview on the right. How would I accomplish this?

like image 493
Skizit Avatar asked Apr 14 '11 14:04

Skizit


2 Answers

The fastest way

Create clickable View with Button drawable as background that contains ImageView and TextView.

<RelativeLayout
    android:clickable="true"
    android:background="@android:drawable/btn_default"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/image"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@android:drawable/ic_input_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ImageView>
    <TextView
        android:id="@+id/text"
        android:text="Sample text"
        android:textAppearance="?android:attr/textAppearanceButton"
        android:layout_toRightOf="@id/image"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></TextView>
</RelativeLayout>

Other way

Create your own class, extend RelativeLayout, set content view as above, add custom methods to set text, image etc. and that's all. Use your custom layout.

like image 163
pawelzieba Avatar answered Oct 01 '22 08:10

pawelzieba


you may to use such code for that task:

<Button
 android:layout_width="fill_parent"
 android:layout_height="50dp"
 android:id="@+id/btnLogin"
 android:text="@string/text"
 android:gravity="center"
 android:drawableLeft="@drawable/your_image"
 android:background="@drawable/login"/> // this is for nice background - *.9.png
like image 37
Anton Derevyanko Avatar answered Oct 01 '22 09:10

Anton Derevyanko