Note that there are some explanatory texts on larger screens.

plurals
  1. POStateListDrawable to switch colorfilters
    text
    copied!<p>I want to create custom buttons to use in a TabHost. I haven been trying to just use the same image resource (png), but have the colorfilter change depending on the state. So I made this bit to serve as the layout for the custom button:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;ImageView android:id="@+id/tab_icon" android:layout_centerInParent="true" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt; &lt;TextView android:id="@+id/tab_text" android:layout_below="@id/tab_icon" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>In my activity, I add the tabs like this:</p> <pre><code>tabHost.addTab(tabHost.newTabSpec(TAB_NAME_NEWS).setIndicator(buildTab(R.drawable.tab_icon_news, R.string.news)) .setContent(newsIntent)); </code></pre> <p>And this is the 'buildTab' method:</p> <pre><code>private final static int[] SELECTED = new int[] { android.R.attr.state_selected }; private final static int[] IDLE = new int[] { -android.R.attr.state_selected }; private View buildTab(int icon, int label) { LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.tab_button, null); StateListDrawable drawable = new StateListDrawable(); Drawable selected = getResources().getDrawable(icon); selected.mutate(); selected.setBounds(0, 0, selected.getIntrinsicWidth(), selected.getIntrinsicHeight()); selected.setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x0000FF00)); drawable.addState(SELECTED, selected); Drawable idle = getResources().getDrawable(icon); idle.mutate(); idle.setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x000000FF)); drawable.addState(IDLE, idle); ((ImageView) view.findViewById(R.id.tab_icon)).setImageDrawable(drawable); ((TextView) view.findViewById(R.id.tab_text)).setText(getString(label)); return view; } </code></pre> <p>In the selected state, the image should be completely green (<code>0x0000FF00</code>), and in the non-selected state, it should be blue (<code>0x000000FF</code>). </p> <p>The problem is that the colorfilters appear to be be completely ignored. I can not see the colors change under any circumstances.</p> <p>I've also tried to get the same result by setting the <code>android:tint</code> property on the <code>&lt;ImageView/&gt;</code>, but apparently you cannot use a reference to a <code>&lt;selector&gt;</code> there, since it throws a <code>NumberFormatException</code>.</p> <p>I don't see what I'm doing wrong so any help would be appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload