Selector on press not working in imageVIew - android

I am using selector for imageView to change image on press event.
but it is not changing image when I press on it.
Here is selector,
<?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/fed_1" />
<item
android:drawable="#drawable/fed" />
</selector>
And this is my imageview,
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:src="#drawable/sel_fed" />
Also tried using background for image view but it's not working.

you should add android:clickable="true". Your imageView will be like this
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:clickable="true"
android:src="#drawable/sel_fed" />

change your src to background and add android:clickable="true" to your ImageView

You can try this selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/fed_1" android:state_selected="true"></item>
<item android:drawable="#drawable/fed_1" android:state_pressed="true"></item>
<item android:drawable="#drawable/fed"></item>
</selector>
Change src to background in your image view.
android:background="#drawable/sel_fed"

Related

Change Color onClick and background on Focus

i have a ListView in my Navigation Drawer and want it looks like the Playstore App or Youtube App.
Means onClick changing FontColor for Example to red and when i just hold it the background should turn grey.
Do i have to use 2 onitemclicklistener?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp">
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="#string/DrawerListIcon"
android:src="#drawable/ic_home"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/icon"
android:textSize="14sp"
android:textStyle="bold"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/DrawerListFontColor"
android:gravity="center_vertical"
android:paddingRight="40dp"
android:paddingLeft="0dp"/>
</RelativeLayout>
Tried with Item and Selector. First thing Font Color don't change when i pressed the Button. Second the background of the Relative Layout turns blue insead of Grey.
drawer_list_font.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:color="#color/DrawerListFontColorPressed"
/>
<item android:color="#color/DrawerListFontColor"/></selector>
drawer_list_background.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:color="#color/DrawerListBackgroundFocused"
/>
<item
android:color="#color/DrawerBackground"
/></selector>
I had to use android:drawable instead of background on the relative Layout. On the Textview Android Stuio accepted android:color.
This is changed by changing view from ur java file at every click event u can change the view. U have to make multiple view ie xml files for that. Which are dynamically changed or u can also send parameters to a function and create an xml at runtime but that will be too deep for u
No need for listeners at all.
A better approach here is to create drawable XML's with definitions for every state ("pressed", "focused", etc).
Check out Color State List resources
For example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false"android:color="#80000" />
<item android:state_focused="true" android:state_pressed="true" android:color="#ff0000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#ff0000" />
<item android:color="#color/DrawerListFontColor" />
</selector>

Android ImageView selector does not work

This is my selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/white_small_down_arrow_v4" android:state_pressed="true"/>
<item android:drawable="#drawable/white_small_up_arrow_v4" android:state_focused="false"/>
<item android:drawable="#drawable/white_small_up_arrow_v4" /> <!-- default -->
</selector>
This is how I applied it on ImageView:
<ImageView
android:id="#+id/change_city_small_down_ImageView"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/changeCityRelativeLayout"
android:layout_marginLeft="5dp"
android:background="#drawable/change_city_selector"
</ImageView>
Now, the problem is, when I pressed the ImageView, the according state drawable image does not change. I have tried it on other wigdet, also not work. I can't figure out why, becasue I used to do this the same way, and it works.
I have monitored imageview states when it been clicked.
v.hasFocus() : false , v.isClickable() : true , v.isInTouchMode() :true , v.isEnabled() : true , v.isPressed() : true
I made a terrible mistake, the white_small_down_arrow_v4 and white_small_up_arrow_v4 actually pointing the same direction, in other words, they are same picture.
so, probably my mistake will help someone else if they found selector does not work, and first thing to do is to check if the state drawables are the same....
Try this: use image android:src="#drawable/change_city_selector" instead of android:background="#drawable/change_city_selector"
<ImageView
android:id="#+id/change_city_small_down_ImageView"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/changeCityRelativeLayout"
android:layout_marginLeft="5dp"
android:src="#drawable/change_city_selector"
</ImageView>
Try adding android:focusable="true" and android:focusableintouchmode="true" in your ImageView property.
You need to set clickable to true in ImageView
android:clickable="true"
change you selection to this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/white_small_down_arrow_v4" android:state_pressed="true"/>
<item android:drawable="#drawable/white_small_up_arrow_v4" android:state_focused="true"/>
<item android:drawable="#drawable/white_small_up_arrow_v4" /> <!-- default -->
</selector>
both of them should be true
Try this, it's checked on Android 4.4.2 and 5.1:
/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<color android:color="#color/item_pressed"/>
</item>
<item>
<color android:color="#android:color/transparent"/>
</item>
</selector>
/layout
<ImageView
android:id="#+id/ivOpenFile"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="8dp"
android:layout_alignParentRight="true"
android:padding="4dp"
android:background="#drawable/selector_settings_item"
android:clickable="true"
android:focusableInTouchMode="true"
android:visibility="invisible"
/>
/java
ivOpenFile = (ImageView) rootView.findViewById(R.id.ivOpenFile);
ivOpenFile.setImageDrawable(VectorDrawableCompat.create(
getResources(),
R.drawable.vd_action_files_black,
null));
Make sure that your selector actually has state_selected instead of state_checked the later will not work
your_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_heart_selected" android:state_selected="true" />
<item android:drawable="#drawable/ic_heart_unselected" android:state_selected="false" />
</selector>
if you have state_checked instead of state_selected, ImageView will never work.

Image selector not working well

This is my problem: I have an ImageView and I assign a selector to it, so the image have to change when I touch it. The problem is that the image doesn't change.
The selector has a different name than the image sources, so that is not a problem. When I touch the image, the app does an http call, so I think that there is the problem, but I don't know how to solve this.
Selector xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/pat_login_button_pressed"
android:state_selected="true" />
<item android:drawable="#drawable/pat_login_button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/pat_login_button"
android:state_selected="false" />
</selector>
ImageView xml:
<ImageView
android:id="#+id/login_button"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:src="#drawable/pat_login_button_selector" />

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
Sunil
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" />
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
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>
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...

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>
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);
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
use the id of imageview instead of linear layout and setclicklistener for imageview itself, it will work!!
You have to change src to background
or
ImageButton to ImageView and alse write android:clickable="true"in ImageView attributes.
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.
setOnClickListener() for selectable components solved the problem

Categories

Resources