Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53657

what is the best way to set image to imageview

Hi I have created a relative layout. and align the imageview to the right of relativelayout. I set the background of relativelayout using selector. if i select thye relativelayout its color wil be green otherwise its color will be white. Now I want to set the image depending on the selection of scrollview. If scrollview is selected then i want to show imge1 if not then i want to show image2. How can I achieve this?

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/selector">
    <ImageView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_right"

        android:layout_alignParentRight="true" />
</RelativeLayout>

selector.xml

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_focused="true"
        android:state_pressed="false"
        android:drawable="@drawable/grbk" />
    <item
        android:state_focused="true"
        android:state_pressed="true"
        android:drawable="@drawable/grbk" />
    <item
        android:state_focused="false"
        android:state_pressed="true"
        android:drawable="@drawable/grbk" />
    <item
        android:drawable="@drawable/whbk" />
</selector>

Thanks Sunilenter image description here

Upvotes: 0

Views: 676

Answers (4)

Fiona
Fiona

Reputation: 273

Here is my suggestion.. once i set the background to the imageview, then when i want to change the transparent, i can do nothing...after check some website, i found i need to set the imageview's resourse instead of background...

Upvotes: 0

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53657

We can create another selector for imageview and pass that selector as background to imageview.

imageselector.xml

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_focused="true"
        android:state_pressed="false"
        android:drawable="@drawable/arow_selected" />
    <item
        android:state_focused="true"
        android:state_pressed="true"
        android:drawable="@drawable/arow_selected" />
    <item
        android:state_focused="false"
        android:state_pressed="true"
        android:drawable="@drawable/arow_selected" />
    <item
        android:drawable="@drawable/arow_unselect" />
</selector>

main.xml

        <ImageView
        android:id="@+id/arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/imageselector"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dip"/>
</RelativeLayout>

Upvotes: 0

dharmendra
dharmendra

Reputation: 7881

first set the color to all the list list view item and then put the selector in the list view ..i gess u know how to set the selector in list view

Upvotes: 0

bart
bart

Reputation: 2408

In the same way as for background. You should add android:duplicateParentState="true"

<ImageView
        android:duplicateParentState="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_selector"
        android:layout_alignParentRight="true" />

Upvotes: 1

Related Questions