subair_a
subair_a

Reputation: 1088

selector not working with layout and image view

I want to use selector for a layout and image view..But it is not working for me. When I changed image view to imagebutton or button, it works fine. Any help will be greatly appreciated.. Thanks in advance..

here is the xmls I used..

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:drawable="@drawable/bg" 
        />
    <item
        android:drawable="@drawable/bg_01" />
</selector> 

The layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_height="fill_parent">
    <ImageButton
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="layout tester"
        android:textColor="#000000"
        android:textStyle="bold"
        android:paddingTop="10dp"
        android:src="@drawable/testselector"
        android:layout_gravity="center_horizontal" />
    </LinearLayout>

Upvotes: 20

Views: 24849

Answers (6)

Mostafa Hydarzadeh
Mostafa Hydarzadeh

Reputation: 185

setOnClickListener() for selectable components solved the problem

Upvotes: 0

Rockmaninoff
Rockmaninoff

Reputation: 3573

I ran into this problem just now. You'll have to set the LinearLayout to clickable. You can either do this in the XML with

android:clickable="true"

Or in code with

yourLinearLayout.setClickable(true);

Upvotes: 66

zafar
zafar

Reputation: 21

You have to change src to background

or

ImageButton to ImageView and alse write android:clickable="true"in ImageView attributes.

Upvotes: 1

olik79
olik79

Reputation: 1412

For me it helped to not assign the attribute android:clickable to the layout but to the ImageView instead. I just wanted the image to be clickable though, not the containing layout.

Upvotes: 0

skygeek
skygeek

Reputation: 1568

use the id of imageview instead of linear layout and setclicklistener for imageview itself, it will work!!

Upvotes: 1

Niranj Patel
Niranj Patel

Reputation: 33248

try this

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

you can use your image instead of lastread_ok & lastread_ok_selected

Upvotes: 6

Related Questions