I have a ToggleButton with text and I want to show a drawableStart next to it only if it's selected. When it isn't selected, I'd like to show just the text (the drawable should disappear).
I tried setting up my selector with just a single item for when android:state_checked=true but when I run the app it gets completely ignored. How can I do this?
Here's my code ToggleButton code:
<ToggleButton
android:id="#+id/list_item_service_list_add_button"
style="#style/OrangeToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:textOff="#string/list_item_service_list_add"
android:textOn="#string/list_item_service_list_added"
android:button="#android:color/transparent"
android:drawableStart="#drawable/add_to_cart_icon_selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
and here's my add_to_cart_icon_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/ic_check_circle"
android:state_checked="true" />
</selector>
Related
I need to customize the text color or background color of the selector when button is selected, because the color of the text its too similar and the text disappears
This is my code:
<android.support.design.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:text="#string/lbl_forgotten_password"
android:textColor="#color/gray_text_1"
android:textSize="14sp" />
This is how it looks:
You can solve this problem by using selector.
Create a new xml file into the drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#color/text_color" android:drawable="#drawable/button_bg"/>
<item android:color="#color/text_normal" />
</selector>
After
<android.support.design.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#drawable/your_xml_file_name"
android:text="#string/lbl_forgotten_password"
android:textSize="14sp" />
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
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>
Is there a way to have different text colours for a toggle button? If i use the android:textColor attribute, it will be applied to both states of the toggle button. Can we have one colour for On and another for Off?
Here's an example ToggleButton element :
<ToggleButton
android:id="#+id/btn"
android:layout_width="200dp"
android:layout_height="200dp"
android:textOn="On"
android:textOff="Off"
android:onClick="onToggleClicked"
android:layout_centerInParent="true"
android:textSize="100sp"
android:textColor="#color/yellow"
/>
create a directory called color under the res directory.
In there create an xml file like, toggle_text_selector.xml
This is what the file could look like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/white" android:state_checked="true"/>
<item android:color="#android:color/black" android:state_checked="false"/>
</selector>
Then on your ToggleButton set android:textColor="#color/toggle_text_selector"
and you're done
I have designed my own graphics composed of two images for an Image Button - one focused and one unfocused. I have the following button in the layout and XML file for it in drawable folder:
<ImageButton
android:id="#+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/borderlessButtonStyle"
android:src="#drawable/btn_play" />
.
<?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/ic_playfocused" />
<item android:state_focused="true" android:drawable="#drawable/ic_playfocused" />
<item android:state_selected = "true" android:drawable = "#drawable/ic_playfocused" />
<item android:drawable = "#drawable/ic_playdefault" />
</selector>
When I click the button, it switches the two images properly, but the problem is that I also see a partially transparent blue rectangle that normally highlights button clicks. How can I get rid of this blue highlight so that when my button is clicked, the only thing that happens is the switch between the two images?
Thank you in advance :)
Ok so just set the button background to transparent, worked for me just now.
<ImageButton
android:id="#+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
style="?android:attr/borderlessButtonStyle"
android:src="#drawable/btn_play" />