Android set android:state_checked="false" in code - android

I want to create a selector using the code below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/sh_radio_icon_checked" />
<item android:state_checked="false" android:drawable="#drawable/sh_radio_icon_unchecked" />
</selector>
So far I have managed to add the first item, like this:
StateListDrawable drawable = new StateListDrawable();
int[] sFocusedSelected = {android.R.attr.state_checked};
Drawable dFocusedSelected = getResources().getDrawable(R.drawable.sh_radio_icon_checked);
drawable.addState(sFocusedSelected, dFocusedSelected);
But do I add the state_checked=false since there's no state_unchecked constant?

Observe "-" (Minus/Hyphen) in the beginning of "android.R.attr.state_checked"
sld.addState(new int[] {-android.R.attr.state_checked }, greyD);
Android : How to update the selector(StateListDrawable) programmatically

Related

I want to change the background color of checkbox button at the time of focus in android

I want to change the color of check box button as when it gets focus its background color will change and when it has no focus it will remain the same.
I have tried using selector but not able to change it
can any one help me out in this
Use this:
Backround-color: 'any color'
Create drawable file like this :
checkbox_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/pink"/>
<item android:state_checked="false" android:color="#color/checkbox_not_selected"/>
</selector>
Apply it like this :
<androidx.appcompat.widget.AppCompatCheckBox
android:id="#+id/chkSelected"
android:layout_width="#dimen/_23sdp"
android:layout_height="#dimen/_23sdp"
android:layout_alignParentEnd="true"
android:buttonTint="#drawable/checkbox_color"
android:checked="false"
android:clickable="false" />
then change it runtime like this to change color runtime based on of it is selected :
chkSelected.isSelected = true
or
chkSelected.isSelected = false
add a new color file in your project. (right click on res then new>android recourse file and put the recourse type on Color )
fill this file with this code :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#ce0e0e" />
<item android:state_checked="false" android:color="#6fc97d"/>
</selector>
then in your CheckBox add this color as your checkBox Color.
you can change your color when checked is true or false in your sample color file

BottomNavigatioView ColorStateList not work on api <=26

I have integrated the bottomNavigationView into my app. I have configured some ColorStateList to change the background color. This selector is ignored in api <= 26
This is my Background selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#B59339" android:drawable="#color/gold" android:state_checked="true" />
<item android:color="#112620" android:drawable="#color/app_dark_color" android:state_checked="false" />
</selector>
And this is my bottomNavigationView
app:itemIconTint="#color/bottom_nav_tint"
app:itemTextColor="#color/bottom_nav_tint"
I've also tried using drawables, but it doesn't change anything
app:itemIconTint="#drawable/bottom_nav_tint"
app:itemTextColor="#drawable/bottom_nav_tint"
I've solved.
I moved the colorFilterList file into drawable and called it like this:
app:itemBackground="#drawable/bottom_nav_background"
app:itemIconTint="#drawable/bottom_nav_tint"
app:itemTextColor="#drawable/bottom_nav_tint"

android: how to create a drawable with normal/pressed states programmably [duplicate]

This question already has answers here:
Replace selector images programmatically
(2 answers)
Closed 8 years ago.
It is simple to specify the view background drawable with normal/pressed states in the .xml, like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:state_enabled="true" android:state_pressed="false"
android:color="#color/setting_color_select" />
<item android:state_pressed="true" android:color="#color/setting_color_select" />
</selector>
But how to create a drawable programmably with the focus or pressed state specified? I didn't find the answer in the developer docs.
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
getResources().getDrawable(R.drawable.normal));
imageView.setImageDrawable(states);

List view item click color indication programatically

I have a list view with items, colored white background, I'd like the user interaction to be more clear when user clicks on item, make the background color different than white.
How do I implement this with code (no xml)?
You need to create a selector xml file in your "drawable" folder and use it as the background of your items, for example:
<?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/itemPressedColor" />
<item
android:state_pressed="false"
android:drawable="#drawable/itemNormalColor" />
</selector>
Where itemPressedColor and itemNormalColor would be drawables defined in the same folder
Use State List for xml or State list Drawable in code
You can use a selector drawable, something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/red"
android:state_pressed="true" />
<item android:drawable="#android:color/white" />
</selector>
And then set this drawable as the background of your list item.
To do the same in code, you need to implement a custom adapter and in its getView() method use code like this:
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(R.color.red));
selector.addState(new int[] {}, getResources().getDrawable(R.color.white));
...
View item = ...
item.setBackgroundDrawable(selector);

Android button states programmatically in Java (not XML)

How does one define Android button image for the "state_pressed"
"android:state_focused" in Java?
For example, how would one accomplish the equivalent in Java for the XML from
http://developer.android.com/reference/android/widget/ImageButton.html
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
Just use addState method of StateListDrawable
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.phone));
You can use constants below for the first parameter of this method
android.R.attr.state_accelerated
android.R.attr.state_activated
android.R.attr.state_active
android.R.attr.state_drag_can_accept
android.R.attr.state_drag_hovered
android.R.attr.state_enabled
android.R.attr.state_first
android.R.attr.state_focused
android.R.attr.state_hovered
android.R.attr.state_last
android.R.attr.state_middle
android.R.attr.state_pressed
android.R.attr.state_selected
android.R.attr.state_single
android.R.attr.state_window_focused
Create an instance of StateListDrawable and then assign it with imagebutton.setImageDrawable(stateDrawable).
10x to Tang Ke, I`m using this for different list item color whit selection color.
selected state
stateListDrawable.addState(new int[] {android.R.attr.state_pressed},
new ColorDrawable(getResources().getColor(R.color.alpha_blue500)));
default state
stateListDrawable.addState(new int[] {},
new ColorDrawable(getResources().getColor(R.color.red)));
Here you can change color for different state of the row item (ex. paid vs free)
set state to custom layout row item in list adapter
holder.relativeLayout.setBackgroundDrawable(stateListDrawable);

Categories

Resources