Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: combining text & image on a Button or ImageButton

I'm trying to have an image (as the background) on a button and add dynamically, depending on what's happening during run-time, some text above/over the image.

If I use ImageButton I don't even have the possibility to add text. If I use Button I can add text but only define an image with android:drawableBottom and similar XML attributes as defined here.

However these attributes only combine text & image in x- and y-dimensions, meaning I can draw an image around my text, but not below/under my text (with the z-axis defined as coming out of the display).

Any suggestions on how to do this? One idea would be to either extend Button or ImageButton and override the draw()-method. But with my current level of knowledge I don't really know how to do this (2D rendering). Maybe someone with more experience knows a solution or at least some pointers to start?

like image 879
znq Avatar asked Oct 07 '09 16:10

znq


People also ask

What is multi line text in Android Studio?

This tag makes the EditText be at most x many lines tall as specified as value. It accepts an integer value. Note : For multiline EditText by default the cursor and hint text is displayed in the center, you can use android:gravity attribute to set it at top and left of the EditText view : android:gravity="top|left"

What is EditText?

A EditText is an overlay over TextView that configures itself to be editable. It is the predefined subclass of TextView that includes rich editing capabilities.

How do you use toggle button?

A toggle button allows the user to change a setting between two states. You can add a basic toggle button to your layout with the ToggleButton object. Android 4.0 (API level 14) introduces another kind of toggle button called a switch that provides a slider control, which you can add with a Switch object.


1 Answers

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

like image 183
OneWorld Avatar answered Oct 07 '22 13:10

OneWorld