I am trying to create a dashboard layout. It is different from the google i/o’s dashboard in the sense that the background of the image icon and the text below the icon is different.
In other words, the icon(image only) has a StateListDrawable for its background while the text has a transparent background. One way of doing this is to put this combination of the image and text in a LinearLayout and use it in the dashboard layout for each of the item which seems not-so-simple. Is this the only way I could do it? or is there a better and simple solution?
Update : You can take the project setup from here and experiment.
-
The layout file
dashboard2.xmlis using the google’sDashboardLayoutclass. In the main activity I’m just setting the contents of the view to bedashboard2 -
In that project you will find a statelist drawable in the
drawablefolder by the namedashboard_icon_bg_selector.xml. I want the background of all the dashboard icons to be this statelist drawable.
Any suggestion is appreciated.
One possible solution I can think of is to use a
LayerDrawableas your TextView’s topDrawable. I’ll outline the idea, enumerate some details and attach a link to the modified example project below.Code for a Button (in the layout file):
Two LayerDrawables, one for the ‘pressed/focussed/selected’ state and one for every other state (rest):
dashboard_icon_layer_background_selected.xml:
dashboard_icon_layer_background_rest.xml:
Some particularities for this specific example:
<item android:drawable="@drawable/btn_star_big_on_rest" />, will result in the star image being scaled up. Just try it once to understand the effect.<item android:drawable="@android:drawable/btn_star" />to see the image getting scaled up, but keep the predefined states intact.Hope this makes sense! Just to show you the result: left is all in rest state, right is topleft button being pressed.
Zipped project here.