I want to color my tabhost icons with xml, instead of doing it programmatically (I still couldn’t do it) ... So, I found this thread on SO: Changing the Android image style to simulate a button click
This seems like a pretty good solution, but I couldn’t adapt it correctly in my project ... I made the following changes:
public class TintableImageView extends ImageView { private ColorStateList tint; public TintableImageView(Context context) { super(context); }
I was also unable to reference @drawable/selector.xml on android:tint , so I did this in colors.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="azulPadrao">#2e7cb4</color> <drawable name="tab_icon_selector">@drawable/tab_icon_selector</drawable> </resources>
My selector:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:tint="#007AFF" /> <item android:state_focused="true" android:tint="#007AFF" /> <item android:state_pressed="true" android:tint="#007AFF" /> <item android:tint="#929292" /> </selector>
My tab layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/TabLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:background="@drawable/tab_bg_selector"> <com.myapp.TintableImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_gravity="center" android:tint="@drawable/tab_icon_selector"/> <TextView android:id="@+id/TabTextView" android:text="Text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@drawable/tab_text_selector" android:textSize="10dip" android:textStyle="bold" android:layout_marginTop="2dip"/> </LinearLayout>
Any suggestions? thanks in advance
[EDIT] I was getting a NumberFormatException to use android:tint when the correct one was app:tint (after installing xmlns:app="http://schemas.android.com/apk/res/com.myapp" ) ... but now I think I am using my selector incorrectly because the icons are all black, regardless of state ... I tried to set <drawable name="tab_icon_selector">@drawable/tab_icon_selector</drawable> from colors.xml, did not work
[/ EDIT]
android android-imageview
Lucas Jota Oct 21 '13 at 16:36 2013-10-21 16:36
source share