android nine patch text color change - android

i have a button defined as Nine Patch like this:
<Button android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/matita"
android:layout_gravity="center"
android:textSize="22sp"
android:text="#string/menu_help" />
In this button there is a text, for example "MY TEXT" this text have a black color. My issue is, when the button is pressed i want to change the text color simply. How i can do it?
Thanks in Advance.

You need to use a selector to do this.
In your drawable folder, create a file, for example dynamic_color_button, and write this into it:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#000000" />
<item android:state_focused="true" android:state_pressed="true" android:color="#FF0000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#FF0000" />
<item android:color="#000000" />
</selector>
And then in your button, access it with textColor:
<Button android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/matita"
android:layout_gravity="center"
android:textSize="22sp"
android:textColor="#drawable/dynamic_color_button"
android:text="#string/menu_help" />

You need to use a ColorStateList
drawable/textColor.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#color/testcolor1"/>
<item android:state_pressed="true" android:state_enabled="false" android:color="#color/testcolor2" />
<item android:state_enabled="false" android:color="#color/testcolor3" />
<item android:color="#color/testcolor5"/>
</selector>
and then apply this to your Button
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/matita"
android:layout_gravity="center"
android:textSize="22sp"
android:text="#string/menu_help"
android textColor="#drawable/textColor" />

Related

Android change circle color and textColor of radio button

When I click on Radio Button, I want the color of the text and the color of the circle to change at the same time as in the image below.
NOTE
The color of choice for the circle and text is none of the colors of the colorPrimary or colorAccent.
If you want to change the text and color of the circles at the same time, follow these steps
1.add style below
<style name="radioButton" parent="AppTheme">
<item name="colorControlNormal">#0f0</item>
<item name="colorAccent">#f00</item>
</style>
2.add this selector.xml to drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#0f0" android:state_pressed="true" />
<item android:color="#f00" android:state_checked="true" />
<item android:color="#0f0" />
</selector>
3.add selector and style to RadioButton
<RadioButton
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="select"
android:textColor="#drawable/selector"
android:textSize="12sp"
android:theme="#style/radioButton"
app:useMaterialThemeColors="false" />
FULL CODE:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="16dp"
android:checkedButton="#+id/one"
android:layoutDirection="ltr"
android:orientation="horizontal">
<RadioButton
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="select"
android:textColor="#drawable/selector"
android:textSize="12sp"
android:theme="#style/radioButton"
app:useMaterialThemeColors="false" />
<RadioButton
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="not select"
android:textColor="#drawable/selector"
android:textSize="12sp"
android:theme="#style/radioButton"
app:useMaterialThemeColors="false" />
</RadioGroup>
And Result
Note: I used Google Material
radiobutton.setOnCheckedChangeListener { buttonView, isChecked ->
if(radiobutton.isChecked())
{
// Changing radio button 1 color on checked.
radiobutton.setTextColor(Color.parseColor("#00ff00"))}}
The MaterialRadioButton uses as default a selector where the colors are based on colorOnSurface when unchecked and colorControlActivated when checked defined in your app theme.
You can override them using a android:theme attribute.
Something like:
<com.google.android.material.radiobutton.MaterialRadioButton
...
android:theme="#style/ThemeOverlay.RadioButton"/>
with:
<style name="ThemeOverlay.RadioButton" parent="">
<item name="colorControlActivated">#color/.....</item>
<item name="colorOnSurface">#color/.....</item>
<item name="colorSurface">#color/.....</item>
</style>
You can also set the useMaterialThemeColors attribute to false
<com.google.android.material.radiobutton.MaterialRadioButton
...
useMaterialThemeColors="false"/>
and apply a custom selector using CompoundButtonCompat#setButtonTintList.
with a selector like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="..." android:state_enabled="true" android:state_checked="true" />
<item android:color="..." android:state_enabled="true" android:state_checked="false" />
<item android:color="..." android:state_enabled="false" android:state_checked="true" />
<item android:color="..." android:state_enabled="false" android:state_checked="false" />
</selector>

cannot update custom ratingbar android

I am trying to write code based on which I should update the rating in steps of 1 as I am getting integer values for Rating. Here is the code for the same
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginRight="3dp" >
<RatingBar
android:id="#+id/PriceRating"
style="#style/foodRatingBarSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:isIndicator="true"
android:numStars="1"
android:stepSize="0.25"
android:max="5" />
</LinearLayout>
Here is the code in the style file:
<style name="foodRatingBarSmall" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/foodratingbar_full</item>
<item name="android:minHeight">20dip</item>
<item name="android:maxHeight">20dip</item>
</style>
Here are the foodratingbar file
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/background" android:drawable="#drawable/foodratingbar_fill"
/>
<item android:id="#+id/secondaryProgress"
android:drawable="#drawable/foodratingbar_fill" />
<item android:id="#+id/progress" android:drawable="#drawable/foodratingbar_empty" />
</layer-list>
And here is the code for foodratingbar_fill:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/dollar" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/dollar" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/dollar" />
<item android:drawable="#drawable/dollar" />
</selector>
where dollar is a png image
The problem with my code is that I cannot get it to update to the required number of stars and I see vague values of the number of stars being displayed on android. I would like to know how I can display one dollar image for 1 rating with a maximum of 5

How to use apply a custom drawable to RadioButton?

It looks like we can use the following in a RadioButton:
android:button="#drawable/myCustomStateBackground"
but that drawable only occupies the spot where the radio drawable would normally go. Ideally I want my entire button background to be stateful. So when pushed, I want the entire button to look like its stuck in the pressed state. To do that, I was hoping I could do something like:
android:button="null"
android:background="#drawable/myCustomStateBackground"
but then the background drawable doesn't know about push state, like the button attribute does. Is there a way around it?
Give your radiobutton a custom style:
<style name="MyRadioButtonStyle" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">#drawable/custom_btn_radio</item>
</style>
custom_btn_radio.xml
<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>
Replace the drawables with your own.
You should set android:button="#null" instead of "null".
You were soo close!
full solution here:
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/oragne_toggle_btn"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/oragne_toggle_btn"
android:layout_marginTop="20dp"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/oragne_toggle_btn"
android:layout_marginTop="20dp"
android:text="RadioButton" />
</RadioGroup>
selector XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/orange_btn_selected" android:state_checked="true"/>
<item android:drawable="#drawable/orange_btn_unselected" android:state_checked="false"/>
</selector>
if you want to change the only icon of radio button then you can only add android:button="#drawable/ic_launcher" to your radio button and for making sensitive on click then you have to use the selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/image_what_you_want_on_select_state" android:state_checked="true"/>
<item android:drawable="#drawable/image_what_you_want_on_un_select_state" android:state_checked="false"/>
</selector>
and set to your radio android:background="#drawable/name_of_selector"
In programmatically, add the background image
minSdkVersion 16
RadioGroup rg = new RadioGroup(this);
RadioButton radioButton = new RadioButton(this);
radioButton.setBackground(R.drawable.account_background);
rg.addView(radioButton);

How to change the color of button after click?

I create button with background color but when i click on it, it's not show anything.
I need to show different color on button after click because user need to know button is
Click.
I don't understand how to do this?
Give me suggestion.
here is my button code.
<Button android:textSize="15px"
android:id="#+id/button9"
android:gravity="center|bottom"
android:textColor="#color/myWhiteColor"
android:drawableTop="#drawable/math"
android:text="#string/HomePage_Math"
android:background="#color/myMaroonColor"
android:layout_width="54dp"
android:layout_height="wrap_content" ></Button>
//XML file saved at res/drawable/button_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
//This layout XML will apply the color list to a View:
<Button android:textSize="15px"
android:id="#+id/button9"
android:gravity="center|bottom"
android:textColor="#color/myWhiteColor"
android:drawableTop="#drawable/math"
android:text="#string/HomePage_Math"
android:background="#drawable/button_bg"
android:layout_width="54dp"
android:layout_height="wrap_content" ></Button>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/colorPrimaryDark" />
<item android:state_focused="true" android:drawable="#android:color/holo_green_dark" />
<item android:drawable="#color/colorCartButton" />
</selector>
This will work

how to set border to layout

I want set a border to Relativelayout. I make relativelayout clickable and use selector for changing selected color, also I use shape for rounding corners of layout. But when I select the layou the shape takes all background including the border. I can't find a way for solving this problem.
This is my main.xml
<RelativeLayout android:id="#+id/map_refresh"
android:layout_height="fill_parent" android:layout_width="wrap_content"
android:background="#drawable/settings_selector_up"
android:padding="15dp"
android:layout_marginLeft="15dip" android:layout_marginRight="15dip" android:layout_marginTop="5dip">
<TextView android:id="#+id/Text1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Map refresh period">
</TextView>
<TextView android:id="#+id/TextView09"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="1 min"
android:layout_alignParentRight="true"
android:paddingRight="5dp">
</TextView>
</RelativeLayout>
</LinearLayout>
this is my settings_selector_up.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false"
android:state_pressed="false" android:background="#drawable/settings_border_roundup" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/settings_border_roundup" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="false" android:drawable="#drawable/settings_border_roundup" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/settings_border_roundup" />
<!-- Pressed -->
<item android:state_selected="true" android:state_pressed="true"
android:drawable="#drawable/selector_pressed" >
</item>
<item android:state_pressed="true" android:drawable="#drawable/list_selector_pressed" />
</selector>
And this is my selector_pressed
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#E6E6E6"></solid>
<corners android:radius ="10dp" />
</shape>
Is there a way receive same result as we go for example using listselector in listView (in listview when I select the listitem only background color changed but border stayed unchanged).
Put the whole thing in another parent layout that handles the drawing of the border, like this:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="your background 9-patch or shape of the border"
android:padding="some padding value so the border looks ok">
< Your RelativeLayout without the border stuff >
</FrameLayout>

Categories

Resources