Android: Combining Text & Image On A Button Or ImageButton


Answer :

For users who just want to put Background, Icon-Image and Text in one Button from different files: Set on a Button background, drawableTop/Bottom/Rigth/Left and padding attributes.

<Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="@drawable/home_btn_test"         android:drawableTop="@drawable/home_icon_test"         android:textColor="#FFFFFF"         android:id="@+id/ButtonTest"         android:paddingTop="32sp"         android:drawablePadding="-15sp"         android:text="this is text"></Button> 

For more sophisticated arrangement you also can use RelativeLayout (or any other layout) and make it clickable.

Tutorial: Great tutorial that covers both cases: http://izvornikod.com/Blog/tabid/82/EntryId/8/Creating-Android-button-with-image-and-text-using-relative-layout.aspx


There's a much better solution for this problem.

Just take a normal Button and use the drawableLeft and the gravity attributes.

<Button   android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:drawableLeft="@drawable/my_btn_icon"   android:gravity="left|center_vertical" /> 

This way you get a button which displays a icon in the left side of the button and the text at the right site of the icon vertical centered.


You can call setBackground() on a Button to set the background of the button.

Any text will appear above the background.

If you are looking for something similar in xml there is: android:background attribute which works the same way.


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable