android - ActionBar item is not showing when a ShapeDrawable is set as it's icon -


i working on simple project , want avoid png images as possible. need '+' (add) button , created using shapedrawable given code.

res/drawable/plus_icon_drawable.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">     <item>         <shape android:shape="line">             <stroke android:color="#000"                 android:width="3dp"/>         </shape>     </item>     <item>         <rotate             android:fromdegrees="90"             android:todegrees="90">             <shape android:shape="line">                 <stroke android:color="#000"                     android:width="3dp"/>             </shape>         </rotate>     </item> </layer-list> 

this code works fine when add background of button or imagebutton when add icon of actionbar item, item not getting displayed.

when set png image icon, works fine. there restriction shapedrawables blocks getting rendered on actionbar?

this actiobbar code

res/menu/action_my_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">     <item android:id="@+id/action_some_id"         android:icon="@drawable/plus_icon_drawable"         android:title="my action"         android:showasaction="always"/> </menu> 

update (08 july 2014): tested both native , appcompat. item present there shapedrawable image not getting rendered. action item responds clicks.

what missing here?

is there restriction shapedrawables blocks getting rendered on actionbar?

nothing that. how things are. here's why you're seeing this:

layer/shapedrawables don't have default size. in other words, fine shapedrawable of 0 width , height. stroke-width parameter not dictate either.

this code works fine when add background of button or imagebutton when add icon of actionbar item, item not getting displayed.

yes, of course. imagebuttons/buttons have concept of padding/ min-width/min-height. these allow shapedrawable have room breathe. example, button styled widget.holo.button have minwidth , minheight set:

<item name="android:minheight">48dip</item> <item name="android:minwidth">64dip</item> 

a valid test here be:

define imageview follows:

<relativelayout     android:layout_width="match_parent"     android:layout_height="match_parent" >      <imageview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@drawable/add" />  </relativelayout> 

the imageview has no sizing guidelines in definition above - we're using wrap_content. nothing displayed. on other hand, if use png drawable, defines size. in example above, using explicit size (say 50dp) make shapedrawable visible.

this how actionmenuitemview sets drawable:

public void seticon(drawable icon) {     micon = icon;     if (icon != null) {          // 0 in case don't define <size />          int width = icon.getintrinsicwidth();         int height = icon.getintrinsicheight();         if (width > mmaxiconsize) {             final float scale = (float) mmaxiconsize / width;             width = mmaxiconsize;             height *= scale;         }         if (height > mmaxiconsize) {             final float scale = (float) mmaxiconsize / height;             height = mmaxiconsize;             width *= scale;         }         icon.setbounds(0, 0, width, height);     }     setcompounddrawables(icon, null, null, null);      updatetextbuttonvisibility(); } 

snippet shapedrawable#inflate(...) method:

 setintrinsicwidth((int)             a.getdimension(com.android.internal.r.styleable.shapedrawable_width, 0f));  setintrinsicheight((int)             a.getdimension(com.android.internal.r.styleable.shapedrawable_height, 0f)); 

without shapedrawable_width , shapedrawable_height defined, intrinsic width , height set zero. means, statement icon.setbounds(0, 0, width, height); (from seticon(drawable) method) in fact => icon.setbounds(0, 0, 0, 0);.

so, seems we're indeed dealing shapedrawables don't have size info.

a possible fix:

<shape android:shape="line">     <size android:width="?android:attr/actionbarsize"           android:height="?android:attr/actionbarsize"/>     <stroke android:color="#000"             android:width="3dp"/> </shape> 

but won't work - reason ?android:attr/actionbarsize not considered dimension. odd because ?android:attr/actionbarsize works fine when supplied android:layout_width in case of layouts.

workaround define new dimension, actionbarsizedp , set up:

in res/values/dimens.xml:

<dimen name="actionbarsizedp">48dp</dimen> 

in res/values-land/dimens.xml:

<dimen name="actionbarsizedp">40dp</dimen> 

these values default action bar size values defined in android's own res/values-xx/dimens.xml.

finally, use actionbarsizedp in plus_icon_drawable.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">     <item>         <shape android:shape="line">             <size android:width="@dimen/actionbarsizedp"                   android:height="@dimen/actionbarsizedp"/>             <stroke android:color="#000"                     android:width="3dp"/>         </shape>     </item>     <item>         <rotate             android:fromdegrees="90"             android:todegrees="90">             <size android:width="@dimen/actionbarsizedp"                   android:height="@dimen/actionbarsizedp"/>             <shape android:shape="line">                 <stroke android:color="#000"                         android:width="3dp"/>             </shape>         </rotate>     </item> </layer-list> 

in fact, defining size on 1 of layerdrawables should suffice. other(s) drawn accordingly.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -