Applying a Theme to Dynamic Radio Buttons in Android - android

I have a Theme in my Application which defines default textappearance and Button Style.
<style name="AppTheme" parent="#android:style/Theme.Light.NoTitleBar.Fullscreen">
<item name="android:textAppearance">#style/TextAppearance.Medium</item>
<item name="android:textAppearanceLarge">#style/TextAppearance.Large</item>
<item name="android:textAppearanceMedium">#style/TextAppearance.Small</item>
<item name="android:textAppearanceSmall">#style/TextAppearance.Medium</item>
<item name="android:textColorPrimary">#000000</item>
<item name="android:textColorSecondary">#000000</item>
<item name="android:textColorTertiary">#000000</item>
<item name="android:buttonStyle">#style/Button</item>
</style>
<style name="TextAppearance" parent="#android:style/TextAppearance">
<item name="android:textColor">?android:attr/textColorPrimary</item>
<item name="android:textColorHint">?android:attr/textColorHint</item>
</style>
<style name="TextAppearance.Large">
<item name="android:textColor">?android:attr/textColorPrimary</item>
<item name="android:textSize">32dp</item>
<item name="android:textStyle">bold</item>
</style>
<style name="TextAppearance.Medium">
<item name="android:textColor">?android:attr/textColorSecondary</item>
<item name="android:textSize">22dp</item>
</style>
<style name="TextAppearance.Small">
<item name="android:textColor">?android:attr/textColorTertiary</item>
<item name="android:textSize">18dp</item>
</style>
<style name="Button" parent="#android:style/Widget.Button">
<item name="android:textColor">?android:attr/textColorSecondary</item>
<item name="android:textColorHint">?android:attr/textColorHint</item>
<item name="android:textStyle">normal</item>
<item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
</style>
When I create a RadioButton using
RadioButton radioButton = new RadioButton(this, null, R.style.AppTheme);
The RadioButton is created and the text is styled properly but the checked marker disappears. Why?
I have the Radiobutton in a Buttongroup, where I dynamically add the buttons. When I add a Button via the layout the marker is displayed properly. But the dynamically added buttons don't.

The solution was to use the
RadioButton(this)
constructor.
All stylings set with the defStyle-Constructor were ignored and the marker were removed. I am not sure if this is a bug or it is not documented.
I actually don't need it since the other stylings from my styles.xml apply to the button automatically now, but if anyone is interested:
adding
<item name="android:radioButtonStyle">#style/MyRadioButton</item>
to my "AppTheme"-Style and
<style name="MyRadioButton" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:textSize">5dp</item>
</style>
to the style.xml makes all my radiobuttons display the marker AND style the text (without setting it in the constructor).

As for me, it has been successfully solved just by using inflater:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
...
RadioButton rb = (RadioButton) inflater.inflate(R.layout.radio_butt, null);
Take a look at the full answer!
Hope, it'll help somebody.

Related

How to change the highlighter color circle for radio button on focus

I am referring to the transparent gray color that comes on radio button when focused
I tried most of the style attributes with color for the RadioButton.
<style name="Widget.App.RadioButton" parent="Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:shadowRadius">50</item>
<item name="android:buttonTint">#color/Red</item>
<item name="colorPrimarySurface">#color/Green</item>
<item name="colorOnSurface">#color/md_red_800</item>
<item name="colorSecondary">#color/md_red_400</item>
<item name="android:colorFocusedHighlight">#color/md_purple_500</item>
</style>
Basically, you can set transparent background to your RadioButton groups.
To do that, you can add this line to your custom style.
<item name="android:background">#android:color/transparent</item>
So, your custom style will look like this;
<style name="Widget.App.RadioButton" parent="Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:shadowRadius">50</item>
<item name="android:buttonTint">#FF3333</item>
<item name="colorPrimarySurface">#android:color/darker_gray</item>
<item name="colorOnSurface">#android:color/holo_red_light</item>
<item name="colorSecondary">#android:color/holo_red_dark</item>
<item name="android:colorFocusedHighlight">#android:color/holo_purple</item>
<item name="android:background">#android:color/transparent</item>
</style>

How to change to text color of all the button in my app?

How can i change the text color of all the button in my app ? right now the only think that work is to do :
<style name="MyTheme" parent="#android:style/Theme.Material.Light.NoActionBar">
<item name="android:textColor">#ff0000</item>
</style>
but i m a little worried to use android:textColor who seam to not only impact the button
I try this but it's didn't work :
<style name="BBButtonColor" parent="#android:style/Widget.Button">
<item name="android:textColor">#ff0000</item>
</style>
<style name="BBButtonStyle" parent="#android:style/Widget.Button">
<item name="android:textColor">#ff0000</item>
</style>
<style name="BBBorderlessButtonStyle" parent="#android:style/Widget.Button">
<item name="android:textColor">#ff0000</item>
</style>
<style name="MyTheme" parent="#android:style/Theme.Material.Light.NoActionBar">
<item name="android:textAppearanceButton">#style/BBButtonColor</item>
<item name="android:buttonStyle">#style/BBButtonStyle</item>
<item name="android:borderlessButtonStyle">#style/BBBorderlessButtonStyle</item>
<item name="android:colorButtonNormal">#ff0000</item>
</style>
It's mostly the color of button inside popup dialog that i want to change
Loki, you can try this. This should change the color of all buttons in your app.
<style name="BBButtonColor" parent="android:Widget.Button">
<item name="android:textColor">#FF0000</item>
</style>
You were trying to use #android:style/Widget.Button, instead use android:Widget.Button and check. Let me know if this works.
You can try this :
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:buttonStyle">#style/ButtonColor</item>
<item name="colorButtonNormal">#color/colorname</item>
</style>
<style name="ButtonColor" parent="#android:style/Widget.Button">
<item name="android:textColor">#color/colorname</item>
</style>
and/change this line in Manifest file :
android:theme="#style/AppTheme.Base
You should create new style for buttons, something like that:
<style name="optionsButton">
<item name="android:layout_width">70dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#color/white</item>
<item name="android:textColor">#color/black</item>
<item name="android:textSize">15sp</item>
</style>
Now you can set this style for every button you want in layout xml, for example:
<Button
android:id="#+id/payCashBtn"
style="#style/optionsButton"
android:text="Cash" />
You can see I set layout_width and height in style, so all my buttons which use this style will have same parameters, but you can set different values for every button, or create multiple styles if you used it more than once. I hope it will help you.

Android white text in search box

See attached
I am using a searchbar with the theme below (I know very little about Android themes, yet)
How do I make the text typed, in this case, "Hi Mom" white?
<style name="_Credential" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:editTextBackground">#drawable/credential_edit_text_holo_light</item>
<item name="android:textColorHighlight">#99005984</item>
<item name="android:textSelectHandleLeft">#drawable/credential_text_select_handle_left</item>
<item name="android:textSelectHandleRight">#drawable/credential_text_select_handle_right</item>
<item name="android:textSelectHandle">#drawable/credential_text_select_handle_middle</item>
<item name="android:autoCompleteTextViewStyle">#style/AutoCompleteTextViewCredential</item>
<item name="android:listChoiceIndicatorMultiple">#drawable/credential_btn_check_holo_light</item>
<item name="android:listChoiceIndicatorSingle">#drawable/credential_btn_radio_holo_light</item>
<item name="android:buttonStyle">#style/ButtonCredential</item>
<item name="android:imageButtonStyle">#style/ImageButtonCredential</item>
<item name="android:dropDownSpinnerStyle">#style/SpinnerCredential</item>
<item name="android:progressBarStyleHorizontal">#style/ProgressBarCredential</item>
<item name="android:seekBarStyle">#style/SeekBarCredential</item>
<item name="android:ratingBarStyle">#style/RatingBarCredential</item>
<item name="android:ratingBarStyleIndicator">#style/RatingBarBigCredential</item>
<item name="android:ratingBarStyleSmall">#style/RatingBarSmallCredential</item>
<item name="android:buttonStyleToggle">#style/ToggleCredential</item>
<item name="android:listChoiceBackgroundIndicator">#drawable/credential_list_selector_holo_light</item>
<item name="android:activatedBackgroundIndicator">#drawable/credential_activated_background_holo_light</item>
<item name="android:fastScrollThumbDrawable">#drawable/credential_fastscroll_thumb_holo</item>
</style>
<style name="_Credential" parent="android:Theme.Holo.Light.DarkActionBar">
...
<item name="android:editTextColor">#ffffff</item> <!--Allow to change the editText color-->
...
</style>
So you can't actually change the TextColor of something like a search box with android:textColor. Instead, you need to use the whole family of the textColor... attributes.
Create a custom style that has your main style as it's parent, and then change all three kinds of colors to be what you want. So, something like this:
<style name="master" paret="#android:Theme.Holo.Light.DarkActionBar">
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:textColorSecondary">#FFFFFF</item>
<item name="android:textColorTertiary">#FFFFFF</item>
</style>
Read more about this here.

Styling of AlertDialog buttons

In my application I use holoeverywhere. I would like to change the appearance of AlertDialog and I got stuck on buttons below alert dialog. I took a look how things were done in holoeverywhere and I tried to modify that.
Here is backtracking of my reasoning:
In theme there are atributes which define alertDialogTheme and some other stuff. We will come back later to selectableItemBackground also.
<style name="Holo.Base.Theme" parent="Theme.AppCompat">
...
<item name="alertDialogTheme">#style/Holo.Theme.Dialog.Alert</item>
...
<item name="buttonBarButtonStyle">?borderlessButtonStyle</item>
...
<item name="selectableItemBackground">#drawable/item_background_holo_dark</item> *
...
</sytle>
So i figure alertDialogStyle is my target which is further defined in styles:
<style name="Holo.Base.Theme.Dialog" parent="Holo.Theme">
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowAnimationStyle">#style/Holo.Animation.Dialog</item>
<item name="android:windowBackground">#drawable/dialog_full_holo_dark</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowTitleStyle">#style/Holo.DialogWindowTitle</item>
<item name="borderlessButtonStyle">#style/Holo.Button.Borderless.Small</item>
<item name="buttonBarStyle">#style/Holo.ButtonBar.AlertDialog</item>
<item name="listPreferredItemPaddingLeft">16dip</item>
<item name="listPreferredItemPaddingRight">16dip</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowAnimationStyle">#style/Holo.Animation.Dialog</item>
<item name="windowContentOverlay">#null</item>
<item name="windowMinWidthMajor">#dimen/dialog_min_width_major</item>
<item name="windowMinWidthMinor">#dimen/dialog_min_width_minor</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Holo.Theme.Dialog" parent="Holo.Base.Theme.Dialog" >
</style>
<style name="Holo.Theme.Dialog.Alert" parent="Holo.Theme.Dialog">
<item name="alertDialogStyle">#style/Holo.AlertDialog</item>
<item name="alertDialogTitleDividerColor">#color/holo_blue_light</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
<style name="Holo.Button.Borderless" parent="Holo.Button">
<item name="android:background">?selectableItemBackground</item>
<item name="android:paddingLeft">4dip</item>
<item name="android:paddingRight">4dip</item>
</style>
So Holo.Theme.Dialog.Alert is inherited from Holo:base.Theme.Dialog which has an atribute for borderlessButtonStyle, which I guess defines the buttons of alert dialog.
Style Holo.Button.Borderless referes the atribute selectableItemBackground for its background. Atribute is set in application theme itself. So, my reasoning is if I inherit from theme Holo.theme and set selectableBackground atribut my change should reflect in customized alert dialog buttons and everything else where this background is used.
<style name="ThemeCustom" parent="#style/Holo.Theme>
…
<item name="selectableItemBackground">#drawable/custom_item_background </item>
…
</style>
But this does not work. No change is visible in alert dialog buttons. Is there something wrong with my reasoning?

How we add styles programmatically?

I have written one style for TextView and I set that one for my TextView as style="#style/popup_window_text_style" working fine, no issues.
Now I want that same style to do in programmatically for my textview
TextView textView = new TextView(mContext);
Is there any way to do it programmatically in Java?
My style in styles.xml
<style name="popup_window_text_style">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:minWidth">60dp</item>
<item name="android:padding">10dp</item>
<item name="android:layout_marginLeft">1dp</item>
<item name="android:layout_marginRight">1dp</item>
<item name="android:background">#drawable/ctxmenu_btn_selector</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">16sp</item>
<item name="android:gravity">center_vertical</item>
<item name="android:singleLine">true</item>
</style>
This should work for you:
textView.setTextAppearance(getApplicationContext(), R.style.popup_window_text_style);
But note that some properties may not be affected and may have to be set manually as specified in the Android developer docs.
Unfortunately the current SDK appears to lack any form of a setStyle() method.

Categories

Resources