Reputation: 2539
I want to have a ImageButtons
changing the image based on its state. (same as "like" button in nine gag and facebook apps).
I have defined a style for each of them that references a selector as a drawable but when I run the program, images doesn't appear. Can you tell me what's wrong with this approach? (sorry for my bad English)
comment_actions_style in values folder:
<style name="LikeStyle">
<item name="android:src">@drawable/like</item>
</style>
<style name="DislikeStyle">
<item name="android:src">@drawable/dislike_normal</item>
</style>
<style name="ReplyStyle">
<item name="android:src">@drawable/reply_normal</item>
</style>
<style name="ShareStyle">
<item name="android:src">@drawable/share</item>
</style>
</resources>
and like drawable in drawable folder: (other buttons are the same)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/like_clicked" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="@drawable/like_focused" android:state_focused="true"/>
<!-- focused -->
<item android:drawable="@drawable/like_normal"/>
<!-- default -->
</selector>
EDIT:
I forgot to mention, I have a list view that its items are user comments and comments have buttons described above.
here is my items layout (part of it)
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/comment_content"
android:orientation="horizontal"
>
<include
android:id="@+id/like"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="@layout/comment_actions"
style="@style/LikeStyle" />
</LinearLayout>
and comment_actions layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/comment_action"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dip">
<ImageButton
android:id="@+id/comment_action_img"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@android:color/transparent"
/>
<TextView
android:id="@+id/comment_action_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/comment_action_img"
android:gravity="center"
android:layout_marginLeft="5dip"/>
</RelativeLayout>
These buttons also inherit from a layout if that's matter.
Upvotes: 0
Views: 1174
Reputation: 2158
Here, your are setting style for comment_actions layout, and it is a Relative Layout, which won't be abled to response to your "android:src" attribute.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/comment_content"
android:orientation="horizontal"
>
<include
android:id="@+id/like"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="@layout/comment_actions"
style="@style/LikeStyle" /> <!--here,you are setting a style for the RelativeLayout -->
</LinearLayout>
Upvotes: 1