How to customize default theme of radio button in android? - android

i want to change default theme of radio button to custom but it's not changing the theme. please help me out
styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="android:radioButtonStyle">#style/radionbutton</item>
</style>
<!-- custom style -->
<style name="radionbutton"
parent="Base.Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:button">#drawable/radiobutton_drawable</item>
</style>
radiobutton_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="#mipmap/radio_unselected" >
</item>
<item android:state_checked="true" android:drawable="#mipmap/radio_selected">
</item>

Declare custom style in your styles.xml file.
<style name="MyRadioButton" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">#color/indigo</item>
<item name="colorControlActivated">#color/pink</item>
</style>
Apply this style to your RadioButton via android:theme attribute.
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Radio Button"
android:theme="#style/MyRadioButton"/>
only if your activity extends AppCompatActivity

Finally, i found answer by myself
I just create custom layout and add to ListAdapter object
radiobuttonlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/checkedTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="#drawable/radiobutton_drawable"
android:gravity="center_vertical"
android:text="New CheckedTextView"
android:padding="#dimen/dp10"
android:textColor="#color/white" />
Java file code
ListAdapter listAdapter = new ArrayAdapter<String> (context,R.layout.radiobuttonlayout,aryStrLockscreens);
listview_lockscreen.setAdapter(listAdapter);
It's worked for me.

The radiobutton_drawable.xml has a missing </selector> tag in the end.
Plus you should do this to your radio button-
<RadioButton android:layout_width="wrap_content"
android:layout_marginTop="20dp"
android:id="#+id/radio"
style="#style/radionbutton"
android:layout_height="wrap_content"/>

Sure you can do it in the parent AppTheme just do the following:
First you will Use the MaterialTheme as your parent theme:
<style name="AppTheme" parent="Theme.MaterialComponents.*">
Then create your RadioButton style in res/values/styles
<style name="Widget.App.RadioButton" parent="Widget.MaterialComponents.CompoundButton.RadioButton">
<item name="buttonTint">#color/white</item>
//put any thing here
</style>
Add it to Parent AppTheme
<style name="AppTheme" parent="Theme.MaterialComponents.*">
<item name="radioButtonStyle">#style/Widget.App.RadioButton</item>
</style>
Then Use it in your layout like:
<com.google.android.material.radiobutton.MaterialRadioButton
android:id="#+id/rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hi"
/>
for more details about theming Theming RadioButton

Need to apply individual style to radio button
<RadioButton
android:layout_width="wrap_content"
style="#style/radiobutton_drawable"
android:layout_height="wrap_content"/>

You could add this to your styles.xml
<style name="Widget.Styles.Radio.RadioButton"
<item name="android:textAppearance">#style/ANY_TYPOGRAPHY_STYLE</item>
</style>
ANY_TYPOGRAPHY_STYLE is for example:
TextAppearance.MaterialComponents.Caption if you are using the Material theme
TextAppearance.AppCompat.Caption if you are using AppCompat theme.
OR any typography style you have created
Example:
<style name="TextAppearance.TypographyStyles.Body2" parent="TextAppearance.MaterialComponents.Body2">
<item name="fontFamily">#font/poppins_regular</item>
<item name="android:fontFamily">#font/poppins_regular</item>
<item name="android:textSize">14sp</item>
</style>
And use the radio button style as a style for the MaterialRadioButton or RadioButton
<com.google.android.material.radiobutton.MaterialRadioButton
style="#style/Widget.Styles.Radio.RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT"/>

Related

Android override predefined parent style

I use one of the predefined styles for MaterialButton.
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />
And this is the style predefined style which i used.
</style>
<style name="Widget.MaterialComponents.Button.OutlinedButton" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:paddingLeft">#dimen/mtrl_btn_padding_left</item>
<item name="android:paddingRight">#dimen/mtrl_btn_padding_right</item>
<item name="strokeColor">#color/mtrl_btn_stroke_color_selector</item>
<item name="strokeWidth">#dimen/mtrl_btn_stroke_size</item>
</style>
And this is parent style of Widget.MaterialComponents.Button.OutlinedButton
<style name="Widget.MaterialComponents.Button.TextButton" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:textColor">#color/mtrl_text_btn_text_color_selector</item>
<item name="android:paddingLeft">#dimen/mtrl_btn_text_btn_padding_left</item>
<item name="android:paddingRight">#dimen/mtrl_btn_text_btn_padding_right</item>
<item name="iconTint">#color/mtrl_text_btn_text_color_selector</item>
<item name="iconPadding">#dimen/mtrl_btn_text_btn_icon_padding</item>
<item name="backgroundTint">#color/mtrl_btn_text_btn_bg_color_selector</item>
<item name="rippleColor">#color/mtrl_btn_text_btn_ripple_color</item>
</style>
Now i tried to override one of the attributes of Widget.MaterialComponents.Button.TextButton in my styles.xml
<style name="Widget.MaterialComponents.Button.TextButton">
<item name="android:textColor">#color/colorAccent</item>
</style>
I wonder whether this is the right approach or not to override a predefined style and will style Widget.MaterialComponents.Button.OutlinedButton use #color/colorAccent as text color. I think its working but i can not be sure is there any side effect of this approach or is there a better way ?
You can use one of the following:
<style name="MyCustomStyle" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="android:textColor">#color/colorAccent</item>
</style>
<style name="Widget.MaterialComponents.Button.OutlinedButton.MyCustomStyle">
<item name="android:textColor">#color/colorAccent</item>
</style>
With these approach, you would inherit all the properties of your parent style and you would be declare only those properties that you like to change/override.
And use this style in your layout file as follow:
<com.google.android.material.button.MaterialButton
style="#style/MyCustomStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />
or
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton.MyCustomStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />

Can't change colorAccent from my application's theme

I've been trying to change the color of the progressBar, and i've noticed it's using the accentColor (which is Blue in my case) and i've been trying to change it without luck.
Am I missing something?
This is in my styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="searchViewStyle">#style/AppTheme.SearchView</item>
<item name="editTextColor">#android:color/white</item>
<item name="actionBarStyle">#style/AppTheme.ActionBarStyle</item>
<item name="actionOverflowButtonStyle">#style/AppTheme.OverFlowItem</item>
<item name="colorPrimary">#color/ColorPrimary</item>
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
<item name="colorAccent">#color/ColorPrimaryDark</item>
</style>
This is my element in the layout.xml
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/Widget.AppCompat.ProgressBar"
android:id="#+id/progressBar01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:indeterminate="true"/>
My activity:
public class MainActivity extends android.support.v7.app.AppCompatActivity
If you want to change accent color for one specific view you need to create style own style and set it to the view using android:theme attribute.
UPDATED
<style name="CustomProgressBarTheme" parent="Widget.AppCompat.ProgressBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimaryCutom</item>
<item name="colorPrimaryDark">#color/colorPrimaryDarkCustom</item>
<item name="colorAccent">#color/colorAccentCustom</item>
</style>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:theme="#style/CustomProgressBarTheme"
android:id="#+id/progressBar01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:indeterminate="true"/>
try this
progressBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.ColorPrimaryDark), Mode.SRC_IN);
Since you have colorAccent with that color, you will need to change the ProgressBar's color with this:
android:progressBackgroundTint="#a31212" // your color
As the doc says:
Tint to apply to the progress indicator background.
Finally:
<ProgressBar
android:id="#+id/progressBar01"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="120dp"
android:progressBackgroundTint="#a31212" />

Android style inheritance not working

styles.xml
<style name="Red" parent="android:TextAppearance.DeviceDefault.Medium.Inverse">
<item name="android:textColor">#f00</item>
</style>
<style name="Red.LARGE">
<item name="android:textSize">30sp</item>
</style>
When i use this;
<Button
android:id="#+id/save"
android:text="#string/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/save_marginTop"
android:gravity="center"
style="#style/Red.LARGE"/>
This button attribute only Large and doest painting to Red. Why?
Your Red.LARGE style needs to use the parent attribute. Red.LARGE will inherit Red if it is changed to:
<style name="Red.LARGE" parent="Red">
<item name="android:textSize">30sp</item>
</style>

how to call a another layout file inside a styles in android?

i custom the Theme in android Widget.TabWidget android:tablayout
and overide the style of tabwidget in my ownway not using #android because
i want to custom my style of API 10 below same like API11+
<resources>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="AppBaseTheme" parent="android:Theme">
<!-- Window -->
<item name="android:windowNoTitle">true</item>
<item name="android:tabWidgetStyle">#style/MyTabWidget</item>
</style>
<!-- TabWidget -->
<style name="MyTabWidget" parent="android:style/Widget.TabWidget">
<item name="android:textAppearance">#style/MyTabWidgetTextAppearance</item>
<item name="android:gravity">center|center</item>
<item name="android:tabLayout"> HOW TO CALL api_1_10_indicator.xml LAYOUT FROM MY RES LAYOUT </item>
</style>
<!-- TabWidget TextAppearance -->
<style name="MyTabWidgetTextAppearance" parent="android:style/TextAppearance.Widget.TabWidget">
<item name="android:textSize">14sp</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">#color/select_tabwidget_text_color</item>
</style>
</resources>
my res/layout/api_1_10_indicator.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="48dip"
android:layout_weight="1"
android:layout_marginStart="-3dip"
android:layout_marginEnd="-3dip"
android:background="#color/api_1_10_tab_indicator">
<ImageView android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
style="?android:attr/tabWidgetStyle"
/>
</RelativeLayout>
if i use <item name="android:tablayout">#layout/api_1_10_indicator</item>
its not working icant call my own tablayout for the tabwidget that i custom.
HOW TO DO IT?

Mix Android Style with my own

Thats how my .axml looks like:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:paddingLeft="5dip"
android:textSize="18dp"
android:text="Kommentare"
style="?android:attr/listSeparatorTextViewStyle" />
As you see I'm accessing a style attribute that's defined in a style theme (?android:attr/listSeparatorTextViewStyle)
Now I would like to separate my own style:
<style name="TextViewTitleBar" parent="android:Theme">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:paddingTop">2dip</item>
<item name="android:paddingBottom">2dip</item>
<item name="android:paddingLeft">5dip</item>
<item name="android:textSize">18dp</item>
</style>
and use it like:
style="#style/TextViewTitleBar" in my .axml , without losing the android defined style with the ? at the beginning.
How to combine/mix these styles?
You can set the parent to
#android:attr/listSeparatorTextViewStyle

Categories

Resources