How we add styles programmatically? - android

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.

Related

How to set text and/or background color of Cut Copy Paste

I am using a custom Dark Theme in my app. Everything is doing okay, except for the Cut Copy Paste dialog that appears as a solid black square.
Surely there is a style that determines that, but I can't find it, or an answer that addresses this directly. Min API is 16.
<style name="DarkTheme" parent="AppTheme.NoActionBar">
<item name="colorPrimary">#color/blackBackground</item>
<item name="colorPrimaryDark">#color/blackBackground</item>
<item name="colorAccent">#color/yellow</item>
<item name="android:textColor">#color/whiteText</item>
<item name="android:background">#color/blackBackground</item>
<item name="buttonStyle">#style/darkButtonStyle</item>
<item name="spinnerStyle">#style/darkSpinnerStyle</item>
</style>
<style name="darkButtonStyle" parent="#android:style/Widget.Button">
<item name="android:layout_width">fill_parent</item>
<item name="android:textColor">#color/whiteText</item>
<item name="android:textSize">16sp</item>
<item name="android:padding">10dp</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#color/darkBackground</item>
<item name="android:gravity">center</item>
<item name="android:textAllCaps">true</item>
</style>
<style name="darkSpinnerStyle" parent="Widget.AppCompat.Spinner">
<item name="android:background">#color/blackBackground</item>
<item name="android:paddingTop">0dp</item>
<item name="android:layout_marginStart">1dp</item>
<item name="android:layout_marginLeft">1dp</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
</style>
I was having the same issue when regarding to MaterialAlertDialog. I was able to fix it using backgroundTint instead of background in styles. This is how:
Before
<style name="materialAlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="android:background">#color/colorPrimaryDark</item>
<item name="materialAlertDialogTitleTextStyle">#style/TitleTextStyle</item>
<item name="materialAlertDialogBodyTextStyle">#style/BodyTextStyle</item>
<item name="buttonBarPositiveButtonStyle">#style/PositiveButtonStyle</item>
<item name="buttonBarNegativeButtonStyle">#style/NegativeButtonStyle</item>
</style>
This will be enough to repropduce the error that you are saying... So what i did was changing background for backgroundTint...
After
<style name="materialAlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="android:backgroundTint">#color/colorPrimaryDark</item>
<item name="materialAlertDialogTitleTextStyle">#style/TitleTextStyle</item>
<item name="materialAlertDialogBodyTextStyle">#style/BodyTextStyle</item>
<item name="buttonBarPositiveButtonStyle">#style/PositiveButtonStyle</item>
<item name="buttonBarNegativeButtonStyle">#style/NegativeButtonStyle</item>
</style>
It seems like when you set a background and the dialog is created, the toolbar of copy/paste menu was changing his background in some weird way that i cant explain.
So this is not the exact answer for your problem but it may help you.
Note: Try removing background attr of your style. I have a dark theme and there is no need to use it. Just simple use <style name="AppTheme" parent="Theme.MaterialComponents.DayNight"> instead of <style name="DarkTheme" parent="AppTheme.NoActionBar">.

Is it possible to set constraint layout properties in a style?

When I set the below constraint layout properties within the activity's xml source I get the expected outcome, but when I set them within a style, the set values don't get processed at all.
<style name="vrm_entry_number_button" parent="#android:style/Widget.Button">
<item name="android:layout_margin">7dp</item>
<item name="android:background">#496587</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
<item name="app:layout_constraintHeight_default">percent</item>
<item name="app:layout_constraintHeight_percent">0.90</item>
<item name="app:layout_constraintWidth_default">percent</item>
<item name="app:layout_constraintWidth_percent">0.10</item>
</style>
What could this be down to and is there an alternative?
Do as below and it should work.
<style name="vrm_entry_number_button" parent="#android:style/Widget.Button">
<item name="layout_constraintHeight_default">percent</item>
<item name="layout_constraintHeight_percent">0.90</item>
<item name="layout_constraintWidth_default">percent</item>
<item name="layout_constraintWidth_percent">0.10</item>
</style>

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.

Applying a Theme to Dynamic Radio Buttons in 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.

android: format textview

is it possible to format a textview for a widget so that it looks like the text below the apps.
like in this screenshot:
i am looking for the exact textcolor, the exact dropshadow and the rounded background.
When in doubt, check with the source, which shows that Android is using a custom TextView to implement the bubbled background around the text.
It appears that the text itself is styled using styles in the launcher app:
<style name="WorkspaceIcon">
<item name="android:textSize">13dip</item>
<item name="android:singleLine">true</item>
<item name="android:ellipsize">marquee</item>
<item name="android:shadowColor">#FF000000</item>
<item name="android:shadowRadius">2.0</item>
<item name="android:textColor">#FFF</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">#drawable/shortcut_selector</item>
<item name="android:paddingLeft">5dip</item>
<item name="android:paddingRight">5dip</item>
</style>
<style name="WorkspaceIcon.Portrait">
<item name="android:drawablePadding">5dip</item>
<item name="android:paddingTop">4dip</item>
<item name="android:layout_marginLeft">3dip</item>
<item name="android:layout_marginRight">3dip</item>
<item name="android:layout_marginTop">13dip</item>
<item name="android:layout_marginBottom">8dip</item>
</style>
<style name="WorkspaceIcon.Landscape">
<item name="android:drawablePadding">3dip</item>
<item name="android:paddingTop">2dip</item>
<item name="android:layout_marginLeft">10dip</item>
<item name="android:layout_marginRight">10dip</item>
</style>
Those styles are called from the launcher's layouts as appropriate.

Categories

Resources