How can I customise a radio button on the right? - java

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.

Related

Why do you use onPrepareOptionsMenu to create a menu where the layout file appears at the top of the screen but the runtime appears at the bottom?

I want to make a menu on my app's top like which showed me in the AS's xml, but in Android 6.0,it's on the bottom. How can I solve this problem? Thanks.
I have tried many methods like change my code, add the android:showAsAction='always', but it doesn't work.
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_new_folder"
android:title="#string/menu_create_folder" />
<item
android:id="#+id/menu_export_text"
android:title="#string/menu_export_text"/>
<item
android:id="#+id/menu_sync"
android:title="#string/menu_sync"/>
<item
android:id="#+id/menu_setting"
android:title="#string/menu_setting" />
<item
android:id="#+id/menu_search"
android:title="#string/menu_search"/>
Create menu like this -
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_more"
android:orderInCategory="1"
android:icon="#drawable/ic_menu_dots"
android:title="more"
app:showAsAction="always">
<menu>
<item
android:id="#+id/menu_new_folder"
android:title="#string/menu_create_folder" />
<item
android:id="#+id/menu_export_text"
android:title="#string/menu_export_text"/>
<item
android:id="#+id/menu_sync"
android:title="#string/menu_sync"/>
<item
android:id="#+id/menu_setting"
android:title="#string/menu_setting" />
<item
android:id="#+id/menu_search"
android:title="#string/menu_search"/>
</menu>
</item>
</menu>
And in your activity's onCreateOptionMenu add this -
menu.findItem(R.id.action_more).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)

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.

Location of Holo focused Button drawable

Does anyone know what is the location of Holo theme focused button drawable? I would like to set it to my View on some event, but I can't find it.
1.first locate following location in your android sdk platforms folder -
yourandroidsdkrootfolderpath\platforms\android-11\data\res\drawable
(ex:D:\android\platforms\android-11\data\res\drawable)
2.find the xml file on folder named as btn_default_holo_dark.xml
and it's contain like below code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_default_normal_disable_holo_dark" />
<item android:state_pressed="true"
android:drawable="#drawable/btn_default_pressed_holo_dark" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_selected_holo_dark" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_focused="true"
android:drawable="#drawable/btn_default_normal_disable_focused_holo_dark" />
<item
android:drawable="#drawable/btn_default_normal_disable_holo_dark" />
</selector>
3.copy xml to your project drawable folder
4.copy drawable-hdpi,drawable-ldpi,drawable-mdpi images mentioned above xml like
btn_default_normal,btn_default_normal_disable,btn_default_pressed,btn_default_selected,btn_default_normal,btn_default_normal_disable_focused,btn_default_normal_disable
5.add style to your style.xml file like below
<style name="Custombutton" parent="Widget.Button">
<item name="android:background">#android:drawable/btn_default_holo_dark</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
<item name="android:textColor">#android:color/primary_text_holo_dark</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">64dip</item>
</style>
6.apply your button style like below code:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
style="#style/Custombutton"
android:layout_height="wrap_content"
android:text="Button" />
Hdpi versions:
/<android sdk path>/platforms/android-15/data/res/drawable-hdpi/btn_default_disabled_holo.9.png
/<android sdk path>/platforms/android-15/data/res/drawable-hdpi/btn_default_focused_holo.9.png
/<android sdk path>/platforms/android-15/data/res/drawable-hdpi/btn_default_normal_holo.9.png
/<android sdk path>/platforms/android-15/data/res/drawable-hdpi/btn_default_pressed_holo.9.png
other resolutions are in drawable-ldpi,drawable-mdpi,drawable-xhdpi directories.

State list drawable for textview

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>

Categories