I am tried to change my font on entire app with Style.xml and it should change font everywhere on my app but i am getting same old font everywhere, only i see changes on dialog title not other places. Check the screenshot of app only no.2 screen dialog has changes. Can you please help me out. I am not able to get what wrong i am doing here.
Style.xml
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorPrimary">#color/primary</item>
<item name="colorAccent">#color/accent</item>
<item name="colorSurface">#color/white</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:textColorSecondary">#color/secondary_text</item>
<item name="android:divider">#color/divider</item>
<item name="windowActionBarOverlay">true</item>
<item name="fontFamily">#font/myfont</item>
<item name="android:fontFamily">#font/font_elaxer</item>
<item name="selectableItemBackground">?android:selectableItemBackground</item>
<item name="android:colorControlHighlight" tools:targetApi="lollipop">#color/ripplecolor</item>
<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
<item name="android:textColorHint">#color/custom_hint_color</item>
<!-- enable window content transitions -->
<item name="android:windowActivityTransitions" tools:targetApi="lollipop">true</item>
<!-- specify enter and exit transitions -->
<item name="android:windowEnterTransition" tools:targetApi="lollipop">#transition/explode</item>
<item name="android:windowExitTransition" tools:targetApi="lollipop">#transition/explode</item>
<!--<item name="android:windowEnterTransition" tools:targetApi="lollipop">#android:transition/slide_right</item>
<item name="android:windowExitTransition" tools:targetApi="lollipop">#android:transition/slide_left</item>-->
<!-- specify shared element transitions -->
<item name="android:windowSharedElementEnterTransition" tools:targetApi="lollipop">#transition/change_image_transform</item>
<item name="android:windowSharedElementExitTransition" tools:targetApi="lollipop"> #transition/change_image_transform</item>
</style>
myfont.xml
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:fontStyle="normal"
app:fontWeight="400"
app:font="#font/dancing_r" />
<font
app:fontStyle="italic"
app:fontWeight="400"
app:font="#font/dancing_b" />
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="#font/dancing_r" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="#font/dancing_b" />
</font-family>
I have an android application, in it I have a list view with choice mode set to multiple. I have set the layout to simple_list_item_multiple_choice. It gives the checkboxes at the right side of the row.
But here's the problem: button tint of these checkboxes is terrible blue (which is far from my application color scheme). Whatever I do, I can't change it.
I have already tried: creating a style for CheckBox and creating a style for CheckedTextView, changing there all kinds of button tints and color accents.
None of it works.
What should I do?
A piece of layout:
...
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView1"
android:layout_weight="1"
android:choiceMode="singleChoice" /> ...
A piece of activity code:
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(),
android.R.layout.simple_list_item_multiple_choice, players);
itemsList.setAdapter(adapter);
Solutions I've tried:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:colorPrimary">#color/MainColor</item>
<item name="android:colorAccent">#color/colorAccent</item>
<item name="android:textColor">#color/MainColor</item>
<item name="colorAccent">#color/MainColor</item>
<item name="colorPrimary">#color/MainColor</item>
<item name="android:checkboxStyle">#style/CheckBoxStyle</item>
<item name="android:checkedTextViewStyle">#style/CheckedTextViewStyle</item>
</style>
<style name="CheckBoxStyle" parent="AppTheme">
<item name="android:tint">#color/colorAccent</item>
<item name="android:buttonTint">#color/colorAccent</item>
</style>
<style name="CheckedTextViewStyle" parent="AppTheme">
<item name="android:colorButtonNormal">#color/colorAccent</item>
<item name="android:checkMarkTint">#color/colorAccent</item>
<item name="android:tint">#color/colorAccent</item>
<item name="android:buttonTint">#color/colorAccent</item>
<item name="android:colorAccent">#color/colorAccent</item>
</style>
UPD: Also, if someone knows how to implement this without checkboxes, but also without creating a useless custom adapter, I would accept this as an answer, too.
Beginning with Android Lollipop (Api 21) you can set checkMarkTint to your activity's theme:
<item name="android:checkMarkTint">#ff0000</item>
To control the checkMark's color.
If you only want to do this for this specific adapter, then create a new theme, extending your activity's theme:
<style name="AdapterTheme" parent="#style/MainTheme">
<item name="android:checkMarkTint">#ff0000</item>
</style>
And set it to your ArrayAdapter:
ContextThemeWrapper themedContext = new ContextThemeWrapper(this, R.style.AdapterTheme);
ArrayAdapter<String> adapter = new ArrayAdapter<>(themedContext, android.R.layout.simple_list_item_multiple_choice, players);
Create three drawables:
checkbox_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_check_on" android:state_checked="true" />
<item android:drawable="#drawable/btn_check_off" android:state_checked="false" />
</selector>
btn_check_on.xml
<vector android:height="32dp" android:viewportHeight="105.0"
android:viewportWidth="105.0" android:width="32dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#color/colorAccent" android:pathData="M48,17c-17.12,0 -31,13.88 -31,31s13.88,31 31,31S79,65.12 79,48S65.12,17 48,17zM43.74,59.84L32.31,48.55l3.23,-3.19l8.2,8.1l17.34,-17.13l3.23,3.19L43.74,59.84z"/>
</vector>
btn_check_off.xml
<vector android:height="32dp" android:viewportHeight="105.0"
android:viewportWidth="105.0" android:width="32dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#fafafa" android:pathData="M48,17c-17.12,0 -31,13.88 -31,31s13.88,31 31,31S79,65.12 79,48S65.12,17 48,17zM48,75.55c-15.21,0 -27.55,-12.33 -27.55,-27.55c0,-15.21 12.33,-27.55 27.55,-27.55c15.21,0 27.55,12.33 27.55,27.55C75.55,63.21 63.21,75.55 48,75.55z"/>
</vector>
Apply checkbox_selector.xml as drawable to your checkbox. You will get the desired result
There are a lot of answers on this site regarding changing button colors, but none that I have managed to use in my case.
I want to be able to dynamically change a button's color, that button still needs to have a visual feedback on press, and it needs rounded corners.
The rounded corners part was decided upon recently, so previously I used something like this:
StateListDrawable states = new StateListDrawable();
states.addState(new int[]{android.R.attr.state_pressed},
new ColorDrawable(hsvDarkenColor(theme.get_buttonsBgColor())));
states.addState(new int[]{android.R.attr.state_focused},
new ColorDrawable(hsvDarkenColor(theme.get_buttonsBgColor())));
states.addState(new int[]{},
new ColorDrawable(Color.parseColor(theme.get_buttonsBgColor())));
((Button) button).setBackgroundDrawable(states);
//this is for the focused/pressed state of the button
private static int hsvDarkenColor(String originalColor)
{
float[] hsv = new float[3];
int color = Color.parseColor(originalColor);
Color.colorToHSV(color, hsv);
hsv[2] *= 0.8f; // value component
return Color.HSVToColor(hsv);
}
However this doesn't preserve the rounded corners shape of the buttons, it makes them into squares.
My default buttons' backgrounds are a list of states each with it's own drawable, for pressed and unpressed, etc.
In styles.xml:
<style name="xx.Light.ScanButton" parent="android:style/Widget.Button">
<item name="android:focusable">true</item>
<item name="android:background">#drawable/xx_scan_button</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">#color/text_light</item>
</style>
In drawable/xx_scan_button.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:drawable="#drawable/xx_scan_button_normal"
/>
<item
android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/xx_scan_button_pressed"
/>
<!-- Focused states -->
<item
android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/xx_scan_button_pressed"
/>
<item
android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/xx_scan_button_pressed"
/>
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="#drawable/xx_scan_button_pressed"
/>
<!-- Disabled -->
<item
android:state_enabled="false"
android:drawable="#drawable/xx_scan_button_normal"
/>
</selector>
And in drawable/xx_scan_button_normal.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners android:bottomRightRadius="5dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="0dp"
android:topRightRadius="5dp"/>
<solid
android:color="#C74700"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
</shape>
TL;DR: I need some way to extract the shape drawable from a drawable state list from a button in order to change it's color.
Or if you guys have a better solution, I'm all ears.
You will have to make separate drawables for individual color and change drawable programmatically not color. Changing color from program overrides the drawble.
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.
I need to store the current style of button,so that whenever activity is sent to background or destroyed and restarted the buttons retain the style they had last time.I know how to save and retrieve information for textviews but i have no idea how to do that for button styles, basically i am more confused about how to retrieve it.
Here is my code for setting button style:
case R.id.darkorange:
for (Button currentButton : buttons) {
currentButton.setBackgroundResource(R.drawable.darkorange);
}
Now orange can be anything purple, red, blue or green. How do i save this inforation about button and retrive it later when activity is recreated.
Do it like these:
res/drawable/solid.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#0078a5"
android:endColor="#00adee"
android:angle="90"/>
<padding android:left="15dp"
android:top="1dp"
android:right="15dp"
android:bottom="1dp" />
<stroke
android:width="1dp"
android:color="#0076a3" />
<corners android:radius="8dp" />
</shape>
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
style="#style/NiceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Colors"/>
</LinearLayout>
res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Buttons</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Color</string>
</resources>
res/values/styles.xml:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="NiceButton" parent="#android:style/Widget.Button">
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:background">#drawable/solid</item>
<item name="android:textSize">16sp</item>
<item name="android:textStyle">bold</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
</resources>
Output: