android imageview android:state_selected="true" not working - android

I am using Image selector for changing my tab-widget image icons , when I selected any one of image view, image of that image view should change. For that i have used following code, but it is working good , but problem is android:state_pressed="true" is working but android:state_selected="true" not working in any image view , i am getting stuck from this issue , can anyone help me, answer will be appreciable.Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_contacts_vippie_selected" android:state_selected="true"/>
<item android:drawable="#drawable/ic_contacts_vippie"/>
Here is my Imageview:
<ImageView
android:id="#+id/hiiMoblieContact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/flexi_contact_selector" />

Instead of image view use toggle button or check-box or radio button. For example I am using check box.
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/hiiMoblieContact"
android:button="#drawable/flexi_contact_selector"/>
flexi_contact_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_contacts_vippie_selected" android:state_checked="true" />
<item android:drawable="#drawable/ic_contacts_vippie" />
</selector>
Hope this will help you..

Imageview doesn't save states. But don't worry, there is a workaround. Either use toggleButtons and set background or use CheckableLinearlayout as parent of imageview. Xml of imageview should be like this-
<package_name.CheckableLinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/hiiMoblieContact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:duplicateParentState="true"
android:src="#drawable/flexi_contact_selector"/>
</package_name.CheckableLinearLayout>
The changes are highlighted in bold. Now you can handle the checkablelinearlayout's set checkedmethod. If for example you set the checkacle linearlayout's set checked true, the image will automatically change. You will have to make this CheckableLinearLayout class in your project and its link is - http://developer.android.com/intl/es/samples/CustomChoiceList/src/com.example.android.customchoicelist/CheckableLinearLayout.html

Related

Set background checkbox android when dissable

to day i work with it and I get a problem.
When I set Checkbox like:
checkboxsend.setEnabled(false);
Here i want the background of checkbox not change to disable?
What can i do that..please help me! Thank
The my xml:
<CheckBox
android:id="#+id/checkboxsend"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:enabled="false" />
You will want to use a state drawable as the background for the Checkbox, something like this should get you closer to your goal:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_checked="true" android:drawable="#android:drawable/checkbox_on_background"/>
<item android:state_enabled="true" android:state_checked="true" android:drawable="#android:drawable/checkbox_on_background"/>
<item android:drawable="#android:drawable/checkbox_off_background"/>
</selector>
You can make modifications to this in order to match your exact logic.
Your CheckBox will look like this:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:background="#drawable/custom_checkbox"
android:checked="true"/>
Try this...If you don't want to change the background Color of your check box than....
Instead:
checkboxsend.setEnabled(false);
Use:
checkboxsend.setClickable(false);

Checkbox without the square android

I would like to simply have the "tick" without the square. Also only as ImageView would be fine. Anyone knows the name of the resource in Android?
Just set selector to drawable left to Checkbox in whatever shape you desire.
Do like this.
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:drawableLeft="#drawable/checkImageSelector" />
android:button="#null" will remove the default image of square with tick and drawableLeft will place your image in place of that.
checkImageSelector.xml will be like this.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/check" android:state_selected="true"/>
<item android:drawable="#drawable/unchecked"/>
</selector>
You need two images for checked and unchecked state.
Create a selector with same resources :
checkbox_selector.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/checked_image" android:state_checked="true"/>
<item android:drawable="#drawable/unchecked_image"/>
</selector>
Then set this selector as button :
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector" />
Activity:
CheckBox.setButtonDrawable((int)getResources().getColor(R.color.transparent));
Fragment:
CheckBox.setButtonDrawable((int)getActivity.getResources().getColor(R.color.transparent));

Setting background on ImageButton doesn't work

I have an ImageButton and I wanted to be able to see a color when I clicked on it. I added a background, and used the same listselector I use for all my listviews, but it doesn't show anything when I click the ImageButton.
Here is the ImageButton xml
<ImageButton
android:id="#+id/button_biomes"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/biome"
android:background="#drawable/listselector"/>
And this is the listselector drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="#color/actionbar" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="#color/actionbar" />
<item
android:drawable="#color/android:transparent" />
</selector>
If anyone could help me It'd be greatly appreciated. Thanks!
It is possible that your image takes up entire space and doesn't show the background. Here, image and background are two different things, so do not expect the src image to be transparent.

android: How to change the state of some view in an item of a listview independly of the state of the item?

I want to change the state of an ImageView in the items of a listview,without being affected by the state of the parent item. but I failed to get it? I'd be really appreciate for your kind responses! and my code is just like the following !
first part of the items' layout in the listview :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_new_folder"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView android:id="#+id/iv_edit_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/iv_delete_folder"
android:visibility="invisible"
android:focusableInTouchMode="false"
/>
second the drawable of the imageview
<?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/iv_delete_p"/>
<item android:drawable="#drawable/iv_delete_n"/>
</selector>
when I click the item while not the image itself ,I find that the drawable is being pressed
! any idea?
You can make use of the attribute: android:duplicateParentState="true/false"
When this attribute is set to true, the view gets its drawable state (focused, pressed, etc.) from its direct parent rather than from itself

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