State list drawable for textview - java

I just want to put a text, for example in blue, and when i press it to turn in another color.
But not like setTextColor(int) but more like a link, that's why i why thinking about state list drawable. Do you know how can i implement this ?

You can use colors instead of drawables for that.
Use some thing like this:
file: res/color/state_white_blue.xml
<?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="true"
android:color="#color/text_white" />
<item
android:state_focused="false"
android:state_pressed="true"
android:color="#color/text_white" />
<item
android:state_focused="true"
android:color="#color/text_white" />
<item
android:state_selected="true"
android:color="#color/text_white" />
<item
android:state_checked="true"
android:color="#color/text_white" />
<item
android:state_selected="false"
android:state_checked="false"
android:state_focused="false"
android:state_pressed="false"
android:color="#color/text_blue" />
</selector>

Related

set Image(PNG etc) as an icon on drawer layout navigation items

I'm setting the image as icon but its showing grey icon not the image,
This #drawable/image_second is the image I want to use as icon.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/image_second"
android:title="#string/menu_home" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="#string/menu_gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="#string/menu_slideshow" />
<item
android:id="#+id/nav_tools"
android:icon="#drawable/ic_menu_manage"
android:title="#string/menu_tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="#string/menu_share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="#string/menu_send" />
</menu>
</item>
</menu>
after running the code,I get this,
please tell what to do and why its happening..
As always thanks a lot..
You can do so programmatically using
NavigationView.setItemIconTintList.
yourNavigationView.setItemIconTintList(null);
Also If you want to change the tint of other icons use this
In XML
<android.support.design.widget.NavigationView
...
app:itemIconTint="#android:color/black"
... />
Try removing the item icon tint.
mNavigationView.setItemIconTintList(null);

Android custom ratingbar with dynamic numstar and custom icon behaving weird on click

I am trying to create a custom rating bar where number of stars is dynamic and the drawable that I need to use also depends on the type, that I get from server.
I am doing this -
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:id="#+id/rating_bar_star"
android:layout_gravity="center"
style="#style/RatingBarStar"
android:stepSize="0.1"/>
then I added this in style -
<style name="RatingBarStar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/rating_bar</item>
<item name="android:minHeight">20dip</item>
<item name="android:maxHeight">20dip</item>
</style>
this is my rating_bar.xml I added in drawable -
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background"
android:drawable="#drawable/rating_bar_empty" />
<item android:id="#android:id/secondaryProgress"
android:drawable="#drawable/rating_bar_empty" />
<item android:id="#android:id/progress"
android:drawable="#drawable/rating_bar_filled" />
</layer-list>
rating_bar_empty.xml
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/rating_off" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/rating_off" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/rating_off" />
<item android:drawable="#drawable/rating_off" />
and rating_bar_filled.xml -
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/rating_on" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/rating_on" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/rating_on" />
<item android:drawable="#drawable/rating_on" />
and then added this in my java file -
rating_bar_star.setNumStars(getStars());
Obviously I am getting stars from server. Now my problem is when I click/select a star it doesn't select one whole star.
If number of star is 6 and I set stepSize as 0.8 then it works fine but same stepSize doesn't work for 10 stars. All I want, my stars to show selected appearance for selected stars.
this is what I get when I don't set stepSize and select/click on first star
and this when I click on second -

Custom button drawable state_pressed and state_checked not working on CheckBox or RadioButton - Android

I am setting custom drawable to the checkbox button,
But only android:state_checked="true" and android:state_checked="false" seem to work.
Other states aren't working.
I am unable to set a custom drawable on pressed state.
This is the selector I am using:
<?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/radio_btn_selected" />
<item android:state_checked="false" android:drawable="#drawable/radio_btn_normal" />
<item android:state_pressed="true" android:drawable="#drawable/radio_btn_pressed" />
<item android:state_focused="true" android:drawable="#drawable/radio_btn_pressed" />
<item android:drawable="#drawable/radio_btn_normal"/> <!-- default -->
</selector>
This is how I am setting it to radio button:
radioButton.setButtonDrawable(R.drawable.radio_btn_selectors);
android:state_pressed="true" and android:state_focused="true" are not working because of the ordering in which you declared various states. Whenever you declare a drawable like this, system will goes from top to bottom and if a state is matched, it doesn't look for the other ones and applies the changes based on the very first selection. I think here android:state_checked="false" condition might be executing instead of android:state_pressed="true" and android:state_focused="true". So move android:state_checked="false" to the bottom and then try.

How to change button background colour without changing onclick/onpress colors

I'm wondering if this is even possible but hopefully someone will be able to confirm.
I've created a simple custom button layout in XML to handle the focused/pressed and dormant states. See code at bottom. This works fine when I use it to create a new button. However, I would like the user to be able to change the button colour via a colour picker if they don't like the default. However, the only way I know to change the button background colour programmatically is to use
mybutton.setBackgroundColor(someothercolor);
but if I do this it overwrites all the XML layout code and I lose the colour change when the button is pressed. I guess this is by design as I'm essentially overwriting the entire background style but what I really want to do is to allow the user to change the button colour when its not pressed to something custom but keep the style and layout of the other states the button could be in (i.e. what happens when its pressed).
Any ideas anyone?
Thank you in advance.
Nat
<?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:drawable="#color/originalbuttoncolor" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:drawable="#color/originalbuttoncolor" />
</selector>
Maybe you can consider creating a ColorStateList programmatically, as described here: How do I create ColorStateList programmatically?
You can try this:
1. remove the default color <item android:drawable="#color/originalbuttoncolor" />
2.Then:
`StateListDrawable ret = (StateListDrawable) res.getDrawable(R.drawable.btn_selector);
ret.addState(new int[] {}, new ColorDrawable(your_desire_color));
mybutton.setBackgroundDrawable(ret);`
You can make multiple files of your selector-list, each one contain different color as the default color, and link those files to the color picker, so you save your logic of selector.
For example:
<?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:drawable="#color/originalbuttoncolor" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:drawable="#color/originalbuttoncolor" />
</selector>
And:
<?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:drawable="#color/yellowbuttoncolor" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:drawable="#color/originalbuttoncolor" />
</selector>
Edit: if you want to take color from user, this may work, if the selector state will overrided by this code:
ColorDrawable cd = new ColorDrawable(); // initialize it from the color picker;
StateListDrawable states = (StateListDrawable) mybutton.getBackground();
states.addState(new int[] {-android.R.attr.state_pressed, android.R.attr.state_focused}, cd); // the minus means false value
mybutton.setBackground(states);

How can I customise a radio button on the right?

I use this in my Radio Buttons so the text will be on the left:
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
But when I do it it uses the android's ugly drawable. Is there a way I can change it to my own customized Radio Button?
Thanks in advance! :)
The default drawable btn_radio is in fact a selector.
For example, the api 15 has for that element this description :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="#drawable/btn_radio_on" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="#drawable/btn_radio_off" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="#drawable/btn_radio_on_pressed" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="#drawable/btn_radio_off_pressed" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/btn_radio_on_selected" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/btn_radio_off_selected" />
<item android:state_checked="false" android:drawable="#drawable/btn_radio_off" />
<item android:state_checked="true" android:drawable="#drawable/btn_radio_on" />
</selector>
So, to override that you would need a selector that follows the method but uses your own images.

Categories