One possible solution that I can think of is to use LayerDrawable as your TextView topDrawable. I will describe this idea, list some details and attach a link to the draft modified example below.
Code for the button (in the layout file):
<Button android:id="@+id/home_btn_schedule" style="@style/DPDashBoardButton" android:text="button 1" android:drawableTop="@drawable/dashboard_icon_bg_selector" />
Two LayerDrawables, one for the βpress / focus / selectβ state and one for each other state (rest):
dashboard_icon_layer_background_selected.xml:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/dashboard_icon_background_selected" /> <item android:bottom="14dip"> <bitmap android:src="@drawable/btn_star_big_on_pressed" android:gravity="center" /> </item> </layer-list>
dashboard_icon_layer_background_rest.xml:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/dashboard_icon_background_rest" /> <item android:bottom="14dip"> <bitmap android:src="@drawable/btn_star_big_on_rest" android:gravity="center" /> </item> </layer-list>
Some features for this particular example:
- The reason star images are referred to as bitmaps is to prevent Android from scaling them to the full width and height of the container. A lower distance is added to actually center the star relative to the background image (textSize = 12, plus 2 to fill), which, to be honest, is pretty ugly for hard code like this. Without this displacement, centered gravity will place a star-shaped image in the center of the entire container, so that it will add space to which the text is added.
- Thus, replacing an element with a raster image with
<item android:drawable="@drawable/btn_star_big_on_rest" /> will cause the star image to be enlarged. Just try once to understand the effect. - This phenomenon is rather unfortunate, since we could refer to another xml, which could be used above, using all the already defined states! That's why I really had to copy two star images from the Android repository: you cannot reference the xml drawable as the source of the bitmap, and the actual images also cannot be publicly referenced. Try replacing the definition of the bitmap with
<item android:drawable="@android:drawable/btn_star" /> to see the scalable image, but do not save the predefined states. - Perhaps you will encounter the problem of scaling by creating 9 images from star images, creating a StateListDrawable for different states and referring to the resulting xml-object, which can be selected as an element of the layer. Although Android will still stretch the entire image to the full size of the container, 9-ports can only scale transparent pixels.
- // Edit: I realized that there is another limitation of this approach, since clicking on the text will not change the state of the icon. Perhaps this may be what you need, but personally, I would say that from the point of view of the user it would be reasonable for all this to respond to touch events ...
Hope this makes sense! Just to show you the result: left - everything is at rest, right - the topleft button is pressed.
Project with zip file here

source share