setOnCheckedChangeListener for every radiobutton - android

I have a radiogroup containing several radiobuttons whose background color is grey. When I click on a radiobutton I would need the clicked one to change background color to black, while others would keep the grey background. I know I can set OnCheckedChangeListeners for all radiobuttons like this:
if(checked) then setBackGroundColor to black;
else setBackGroundColor to grey;
but is there any more efficient way to do that? Like write just one OnCheckedChangeListener for the whole group

Create a selector -> drawable/radio_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/black" /> <!-- checked -->
<item android:state_checked="false"
android:color="#color/grey" /> <!-- unchecked -->
</selector>
And in the RadioButton view add
android:buttonTint="#drawable/radio_selector"
This works on api 21+
If you are using a lower minimum api you will need to set buttonTint in a style
<style name="radio_style" parent="Widget.AppCompat.CompoundButton.RadioButton">
<item name="buttonTint">#drawable/radio_selector</item>
</style>
and add this instead to your RadionButton
style="#style/radio_style"

Related

Customize BottomNavigationView to show text for the selected item, and icon for non-selected items

I'm trying to make this NavigationBottomView:
All I want is to make selected item text instead of icon.
I googled it and tried to make custom navigationBottomView item, but I found nothing like what I want.
is it possible to be a text ? or even can i hide the icon and display item title only ?
Yes it's.
To control toggling BottomNavigationView item text, use app:itemTextColor with a custom selector
In your case you need to show up the text when an item is checked, and hide it otherwise.
To control toggling the item icon, use app:itemIconTint with a custom selector
In your case you need to show up the icon when an item is unchecked, and hide it otherwise.
For both text/icon cases you can use a transparent color as a hack for the hidden state.
Example:
<com.google.android.material.bottomnavigation.BottomNavigationView
...
app:itemTextColor="#drawable/text_selector"
app:itemIconTint="#drawable/icon_selector"
text_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFEB3B" android:state_checked="true" />
<item android:color="#android:color/transparent" android:state_checked="false" />
</selector>
icon_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFEB3B" android:state_checked="false" />
<item android:color="#android:color/transparent" android:state_checked="true" />
</selector>
UPDATE
i tried it before but all i got icon get hidden and text still at the
bottom not shifted to the center like gif u sent
You can have the same behavior by changing the BottomNavView text size:
Create the following style to increase the item text:
<style name="BottomNavigationViewActiveItemText" parent="#style/TextAppearance.AppCompat.Caption">
<item name="android:textSize">20sp</item>
</style>
Apply it to the BottomNavigationView with app:itemTextAppearanceActive="#style/BottomNavigationViewActiveItemText"
If your item text can be in two lines, use also:
<style name="BottomNavigationStyle">
<item name="android:gravity">center</item>
<item name="android:lines">2</item>
</style>
And apply it with android:theme="#style/BottomNavigationStyle"

How to update the color of checkbox in checkboxpreference programmatically?

As of now checkbox is filled with the toolbar theme color but I need to show checkbox with different color other than toolbar so I need to update checkbox color programmatically in android. How to do the same?
For Checkboxpreference you should use this:
https://stackoverflow.com/a/6042324/11549280
and
for CheckBox you should use:
Suppose your checkbox is:
Checkbox checkbox;
To create a ColorStateList programmatically,
Use
ContextCompat.getColorStateList(context, R.color.your_color);
and then
CompoundButtonCompat.setButtonTintList(checkbox, colorStateList);
Create theme to your setting activity, add this parameters to the style:
<style name="AppTheme.NoActionBar.SettingActivity" parent="AppTheme.NoActionBar">
<!--when the check box is checked --!>
<item name="colorControlNormal">#color/your-color</item>
<!--when the check box is unchecked --!>
<item name="colorControlActivated">#color/your-color</item>
</style>`
if you are using AppCompatCheckBox follow the instruction below
Use buttonTint to change color of button and color selector for above 21+ api version.
<android.support.v7.widget.AppCompatCheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="#color/checkbox_filter_tint"
tools:targetApi="21"/>
res/colors/checkbox_filter_tint.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/light_gray_checkbox"
android:state_checked="false"/>
<item android:color="#color/common_red"
android:state_checked="true"/>
</selector>

Set tabSelectedTextColor for TabLayout using selector

Following codes work because I added the tabSelectedTextColor attribute directly and selected text color will be white.
<android.support.design.widget.TabLayout
...
app:tabSelectedTextColor="#color/white"
app:tabTextColor="#color/tab_layout"/>
But following codes don't work and I don't know why, maybe it is a bug!
<android.support.design.widget.TabLayout
...
app:tabTextColor="#color/tab_layout"/>
#color/tab_layout
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected state defined so it's better to use it -->
<item android:color="#color/white" android:state_selected="true"/>
<item android:color="#color/white" android:state_focused="true"/>
<item android:color="#color/white" android:state_pressed="true"/>
<item android:color="#CCFFFFFF"/>
</selector>
Note: #CCFFFFFF color works so it means, the view gets the tabTextColor attribute value correctly but doesn't recognize the android:state_selected item. I tested all rational states but nothing worked.
TabLayout.class
Following codes copied from TabLayout.class and everything is clear. Don't you think getting selected text color from the selector is better way? If it is, please report it.
if(a.hasValue(styleable.TabLayout_tabSelectedTextColor)) {
int selected = a.getColor(styleable.TabLayout_tabSelectedTextColor, 0);
this.mTabTextColors = createColorStateList(this.mTabTextColors.getDefaultColor(), selected);
}
If you want to change the selected text color then use setTabTextColors methode of TabLayout class like this:
tabLayout.setTabTextColors(Color.parseColor("#ADABAE"), Color.parseColor("#FFFFFF"));

Setting button drawable for RadioButton using drawable xml not working for checked state

I've got a group of radio buttons, and I want to set the button's background to a solid color when checked. I created a drawable resource, using a selector and item def's like:
<item android:state_checked="true" android:state_pressed="false"
android:drawable="#color/app_tint"/>
with several variations while trying to get it to work. In the layout containing the buttons, I've tried setting both button and background properties (not at the same time, just one or the other in testing) like:
android1:background="#drawable/radio_state"
OR
android1:button="#drawable/radio_state"
I've read several posts, and I feel I'm close, just missing something to get it done. Thanks.
Here's one we did for an app:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_checked="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_selected="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_pressed="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_focused="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling" />
</selector>
Each state has a different drawable, although in this example, we don't really care about all states being very different - just focus=true get a highlighted drawable (it has "..._focus")

how to change button color when it is clicked in android

By default when button is clicked something like orange color will surround the button for short time, that indicates buttons is clicked. But this feature is not working when button contains background image. This is happening in list view too.why ? Any Ideas? TIA
I used setBackgroundColor(Color.BLUE); But here the color is applied and not gone...
You need to use a selector as your background resource :
<?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" />
<item android:state_focused="true" android:drawable="#drawable/button_focus" />
<item android:drawable="#drawable/button_normal" />
</selector>
This selector makes use of 3 separate drawables to change the image of the button , you may use the same image for pressed and focuses.
You need to put this XML into your drawables directory and use it as a background for your button.
For another solution refer : Standard Android Button with a different color
i too had the same problem. so instead of setting the background color,i included three images of button in three different colors , so based on state focused,pressed,default the respective image will replace the other. and it looks like change in the color of the button.
**<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/c2"
android:state_pressed="true" />
<item android:drawable="#drawable/c1"
android:state_focused="true" />
<item android:drawable="#drawable/c1" />
</selector>**
above is the content of the xml file,which must be assigned to the background of the respective button
android:background="#drawable/drawable_button
hope this might be helpful for you

Categories

Resources