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?
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>
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With