Custom ToggleButton setChecked(..) issue [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
After setting toggle button to checked state, every time I click on it, its in same state.
custom selector :
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#drawable/list_view_icon" android:state_checked="true"
android:state_pressed="true" android:state_enabled="true"/>
<item android:drawable="#drawable/list_view_icon" android:state_checked="true"
android:state_focused="false" android:state_enabled="true"/>
<item android:drawable="#drawable/map_view_icon" android:state_checked="false"
android:state_pressed="true" android:state_enabled="true"/>
<item android:drawable="#drawable/map_view_icon" android:state_checked="false"
android:state_focused="false" android:state_enabled="true"/>
toggle button :
<ToggleButton
android:id="#+id/toggle_button_map_or_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/custom_selector"
android:textOn=""
android:textOff=""
android:background="#drawable/map_view_icon"
/>
java code :
In button onClick listener, isChecked is always false
boolean isChecked = ((ToggleButton) view).isChecked();
Log.i(TAG, "isChecked : "+isChecked);
// **its always false and image also not changing.**
Please suggest whats wrong here.
I had a look at this answer, but no use.
Edit : I did a sample project with same scenario. Its working fine but in my project Fragment, its behaving differently.

Here it is for you:
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#drawable/list_view_icon"
android:state_checked="true" />
<item android:drawable="#drawable/map_view_icon"
android:state_checked="false" />
You've put a lot of conditions for the different states and that's what causing the problem (see here for more information).
Following your EDIT:
<ToggleButton
android:id="#+id/toggle_button_map_or_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/custom_selector"
android:textOn=""
android:textOff=""
android:background="#drawable/selector_name"
/>
This will fix it for you FOR SURE! You're using the map icon directly and never go through the selector.

Selectors have an order (see Drawable States).
Remove the android:button attribute.
Set the Selector as background to your ToggleButton:
<ToggleButton
android:id="#+id/toggle_button_map_or_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:background="#drawable/custom_selector"/>
And the custom_selector.xml looks like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Checked and pressed -->
<item
android:state_checked="true"
android:state_pressed="true"
android:drawable="#drawable/list_view_icon" />
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="#drawable/map_view_icon" />
<!-- Checked -->
<item
android:state_checked="true"
android:drawable="#drawable/list_view_icon" />
<!-- Default (not checked) -->
<item
android:drawable="#drawable/map_view_icon"/>
</selector>

Related

Multiple Toggle Buttons with their own on/off state icon

I have a couple of ToggleButtons inside a HorizontalScrollView, I want each of these ToggleButtons to have a different on and off drawable
ToggleButton:
<ToggleButton
android:id="#+id/toggle_[nameOfToggle]"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/toggle_selector"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
I have a selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- currently pressed turning the toggle on -->
<item android:state_checked="true" android:state_pressed="true"
android:drawable="#drawable/toggle_state_on"/>
<!-- currently pressed turning the toggle off-->
<item android:state_pressed="true"
android:drawable="#drawable/toggle_state_off"/>
<!-- not pressed default checked state -->
<item android:state_checked="true" />
<!-- Default non-pressed non-checked -->
<item />
</selector>
toggle_state_on.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon_on_[nameOfToggle]"/>
</layer-list>
What is the best way to programmatically achieve this, is there a way to check what toggle button it is and change the drawables of the on/off state based on that?

Android change switch image

I would like to know how to change the image of a switch in android. I know that we can customize the switch and change the color etc. but how do we use another image for the switch. I tried setting the background from my drawables but the switch is still visible and appears on top of my image. I understand that we can use toggle button, or image button however I am curious if we can do so to a switch.
Override android:thumb and android:track in Switch component to override switch image and background respectiveli.
For eg:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="#drawable/drawable_switch"
android:track="#drawable/drawable_bg" />
where drawable_switch and drawable_bg are selectors respectively.
drawable_switch.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/switch_disabled" />
<item android:state_pressed="true" android:drawable="#drawable/switch_pressed" />
<item android:state_checked="true" android:drawable="#drawable/switch_enabled" />
<item android:drawable="#drawable/switch_default" />
drawable_bg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/switch_bg_disabled" />
<item android:state_focused="true" android:drawable="#drawable/switch_bg_focused" />
<item android:drawable="#drawable/switch_bg_default" />
#Jerry try this and revert if you still have any issues
There's android:thumb attribute.

how to create or make or set custom background in floating button android

I have created custom floating button and use all library but not created custom floating button i have very tried to make to this button if you any idea how to make custom floating button.
Plz help me
My button look like this:
My code
<Button
android:id="#+id/btn_build_now"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0"
android:background="#color/Background_Main"
android:drawableBottom="#drawable/button_bg"
android:paddingBottom="10dp" />
You could simply use Android support library to create a Floating Action Button.
For example here is a floating action button example with custom background color, pressed and focused states.
XML
<android.support.design.widget.FloatingActionButton xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fb"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/fb_icon_play"
app:backgroundTint="#drawable/fb_play_bg" />
fb_icon_play.xml in Drawable Folder
<?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/floating_button_blue_play_icon_pressed" />
<item android:state_focused="true" android:drawable="#drawable/floating_button_yellow_play_icon_pressed" />
<item android:drawable="#drawable/floating_button_yellow_play_icon_pressed" />
</selector>
fb_play_bg.xml in Drawable Folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#3389b3"/>
<item android:state_focused="true" android:state_pressed="true" android:color="#3389b3" />
<item android:state_focused="false" android:state_pressed="true" android:color="#d1930f" />
<item android:color="#3389b3" />
</selector>
Here is another tutorial for creating a custom FloatingActionButton.
You can use the new version of FAB, Extended FAB in XML
The old one
<com.google.android.material.floatingactionbutton.FloatingActionButton/>
The newest:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton/>
then you can define Icon, Text and Shape:
android:text="Hi"
app:icon="#android:drawable/ic_media_play"
app:shapeAppearance="#style/ShapeAppearanceOverlay.Material3.FloatingActionButton"
app:shapeAppearanceOverlay="null"

Color statelist not working on recyclerview textview

I set up a color statelist like so:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/white_25percent_opacity" android:state_selected="true"/>
<item android:color="#color/white_25percent_opacity" android:state_pressed="true"/>
<item android:color="#color/white_25percent_opacity" android:state_focused="true"/>
<item android:color="#android:color/white"/>
</selector>
Then I tried to set it in the xml for a recyclerview item like so:
<TextView
android:id="#+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:paddingRight="10dp"
android:paddingEnd="10dp"
android:textColor="#color/mySelector"
/>
but it doesnt work - the color doesnt change when pressed. So I tried to set it programmatically in onBindViewHolder like this:
viewHolder.myTextView.setTextColor(ContextCompat.getColorStateList(context, R.color.mySelector));
and I also tried like this:
viewHolder.myTextView.setTextColor(ContextCompat.getColor(context, R.color.mySelector));
which also don't work. Where is the mistake here and why doesn't this work for recyclerviews? To clarify - the text is shown in the initial color (white) but doesn't change to the pressed color.
Edit: also tried to solve it by making the selector a drawable - but it didn't work:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white_25percent_opacity" android:state_selected="true"/>
<item android:drawable="#color/white_25percent_opacity" android:state_pressed="true"/>
<item android:drawable="#color/white_25percent_opacity" android:state_focused="true"/>
<item android:drawable="#android:color/white"/>
</selector>
If I set an ontouchlistener and switch the colors manually then it works properly - but I want to do this with a statelist.
Hi Implement your XML like the below code , Hope it would be helpful may be the issue is you have given both state_pressed and state_selected = true.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Color when the row is selected -->
<item android:drawable="#android:color/darker_gray" android:state_pressed="false" android:state_selected="true" />
<!-- Standard background color -->
<item android:drawable="#android:color/white" android:state_selected="false" />
</selector>
Turns out I needed to set this on the textview to make it work:
android:clickable="true"

Android - Disable ListView Selection Highlight but Keep OnClick enabled [duplicate]

This question already has answers here:
Android: disabling highlight on listView click
(15 answers)
Closed 6 years ago.
I want to disable the highlight that appears when the user selects a row (listSelector) from code. I don't want to disable the onClick and enabled settings (I still want to listen to clicks, just want to remove the highlight).
Specify android:listSelector="#android:color/transparent" in your ListView XML.
Just create a drawable that has a transparent color in it, something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#android:color/transparent"/>
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="true" android:drawable="#drawable/list_focused_holo" />
</selector>
And then set by code or by XML:
listView.setSelector(R.drawable.my_transparent_selector);
The javadoc for this method says:
Set a Drawable that should be used to highlight the currently selected item.
and the XML attribute is:
android:listSelector
You can play with all the states, remember that you also have the focus state.
I have done this way:
By adding two properties of ListView.
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent"
Your ListView should looks like below:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent">
</ListView>
Done
try listview.setSelector(new ColorDrawable(Color.TRANSPARENT));
The highlight effect is a style on the listSelector. You can override the listSelector style.
This is a example with a listview : Android: disabling highlight on listView click

Categories