I am trying to change the text color from the DatePicker using an XML style with no luck. My app theme is Theme.MaterialComponents.Light and the style code that I am using is the following.
<style name="MyDatePicker" >
<item name="android:textColorPrimary">#color/textColorPrimary</item>
<item name="colorControlNormal">#color/colorAccent</item>
</style>
<DatePicker
android:id="#+id/datePicker"
style="#style/MyDatePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:datePickerMode="spinner" />
I have seen many posts tackling this issue, but unfortunately none of the answers worked for me. I am using version 1.2.0 of the material library.
Is there any other way to solve this issue? Thanks!
Since DatePicker is a ViewGroup, you apply the defined style to it (and its children) using the theme attribute.
style, in contrast, is applied to the parent DatePicker only and its children remain unaffected.
So change to:
android:theme="#style/MyDatePicker"
Related
I have very simple checkbox:
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/clipboardBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/clipboard_checkbox" />
For checked/unchecked I have two different images.
After migration to AndroidX I see default image for Android on devices below API 21.
What I've tried already:
change CheckBox to AppCompatCheckbox (from AndroidX) - nothing changed
set background to checkbox & set android:button="#null" - background is OK, but I still see default image on background (see image below)
Seems that Android completly disrespect button attribute.
I am out of ideas. For Lollipop+ everythink works as it should. Have anyone faced issue like this?
Only change I did was migration to AndroidX :/
In the appcompat theme, the checkBoxStyle below API 21 is defined as
<style name="Base.Widget.AppCompat.CompoundButton.CheckBox" parent="android:Widget.CompoundButton.CheckBox">
<item name="android:button">?android:attr/listChoiceIndicatorMultiple</item>
<item name="buttonCompat">?attr/listChoiceIndicatorMultipleAnimated</item>
<item name="android:background">?attr/controlBackground</item>
</style>
the attr buttonCompat has a default value to show the click animation. The attr buttonCompat take effect and ignore the button attr.
To fix it, the attr buttonCompat must be undefined. In your theme add
<item name="checkboxStyle">#style/MyCheckBox</item>
and add a style
<style name="MyCheckBox" parent="android:Widget.CompoundButton.CheckBox">
<item name="android:button">?android:attr/listChoiceIndicatorMultiple</item>
<item name="buttonCompat">#null</item>
<item name="android:background">?attr/selectableItemBackgroundBorderless</item>
</style>
Also in your values-v21 dir, add this to your theme
<item name="checkboxStyle">?android:attr/checkboxStyle</item>
Change CheckBox to AppCompatCheckBox(AndroidX) and replace android:button to app:buttonCompat
<androidx.appcompat.widget.AppCompatCheckBox
android:id="#+id/clipboardBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/email_sign_in_button"
app:buttonCompat="#drawable/clipboard_checkbox" />
You need to set the button and buttonCompat null for androidx libraries. It will look like below-
<androidx.appcompat.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
app:buttonCompat="#null"
android:background="#drawable/cb_pause_resume_selector"
/>
I can able to set theme to button using android:theme attribute in xml. Is there any equivalent programmatical code?
<Button
android:id="#+id/btnPayment"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Add Payment"
android:theme="#style/ButtonPeter" // theme..
/>
style.xml
<style name="ButtonPeter" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/fbutton_color_peter_river</item>
<item name="android:textColor">#android:color/white</item>
</style>
Can I able to set theme dynamically using something like this btnPayment.setTheme(R.style.ButtonPeter) ?
I search a lot but all posts are related to set style or create dynamic button and apply style to it but I dont want to do that .I want to set theme to button
How to achieve this?
I think you are confused with style and theme. From this document, it says: theme for the whole activity while style is for view.
A style is a collection of properties that specify the look and format for a View or window.
A theme is a style applied to an entire Activity or application, rather than an individual View.
For this code:
android:theme="#style/ButtonPeter" // theme is used but only valid style attributes for button will be applied
Hope this help.
As I mention here, using TextViewCompat.setTextAppearance should work :-)
I'm trying to style edit text widgets in my app. Right now if I use this:
<style name="MyEditText" parent="#android:style/Widget.EditText">
<item name="android:textColor">#color/input_text</item>
<item name="android:gravity">left</item>
</style>
It will show black background on phone I use for testing. I'd like to get something that looks the same everywhere. I think I will be OK with this style (I also believe this is "standard"). How do I pull this stale or should I make my own somehow?
EDIT:
Here is my code for TextEdit
<EditText
style="#style/MyEditText"
android:id="#+id/et_serverURL" android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxLength="25"
android:imeOptions="actionNext" android:singleLine="true" android:inputType="textUri"
android:hint="#string/str_login_activity_server_url_hint"
/>
The style shown in your image is called Holo. You can use it like so
<style name="MyEditText" parent="#android:style/Widget.Holo.EditText">
<!-- Your custom attributes here -->
</style>
EDIT
If you want your app to use your themes as you have them right now but have your EditText be Holo then you can override the <item name="android:editTextStyle"></item>under your current theme to use MyEditText instead of the default parent style. Then set your theme either to your activity, or to your application.
So I am trying to change my app color to blue and some of most the views I have are not willing to cooperate with me.
Here is the image:
Here I want to change the color of the green parts on the spinner, edittext and checkbox views (which are green) to black or blue.
I've looked all over Stack Overflow and I can't find the solution!
Thank you very much, If possible I would like to have a XML solution but I wouldn't mind a programmatic solution!
Add these to your base theme in styles.xml
<item name="colorControlNormal">#color/YOUR_COLOR</item>
<item name="colorControlActivated">#color/YOUR_COLOR</item>
<item name="colorControlHighlight">#color/YOUR_COLOR</item>
Note: The above change will affect the EditTexts and other views probably throughout the application.
If not, and if you are using the AppCompat v22 support library, you can specify the theme in the EditText like: android:theme="#style/Theme.App.Base.
This will ensure the style won't also affect other views in your layouts that you don't want to change
Also if you want to change the above solution, just add another Theme specific to EditTexts and Spinners and apply it to all Spinners if you want
<style name="MyWidgetTheme">
<item name="colorControlNormal">#color/YOUR_COLOR</item>
<item name="colorControlActivated">#color/YOUR_COLOR</item>
<item name="colorControlHighlight">#color/YOUR_COLOR</item>
</style>
and in your EditText, Spinner or any other View, just assign this theme:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Demo"
android:lines="1"
android:theme="#style/MyWidgetTheme"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/MyWidgetTheme"></Spinner>
Take a look to this resource generator.
Choose your color, the widgets you want to generate and voila! You copy them to your project and reference them in your xmls.
I have few checkbox's at my application with different colors.
Now when I applied material design theme checkbox's without style changed their color to "colorControlActivated", checkbox's with style left the same.
Also I wanted to change colors of my other checkbox's without drawables files.
I tried to set style with different "colorControlActivated" color
<style name="CheckBox.Custom" parent="#style/Theme.Material.Blue">
<item name="colorControlActivated">#color/custom_color</item>
</style>
but it's not working, also I tried to create checkbox like:
ContextThemeWrapper ctw= new ContextThemeWrapper(getActivity(), R.style.CheckBox_Info);
Checkbox cb = new CheckBox(ctw);
and color didn't changed.
The same code working with SwitchCompat control.
Do somebody now how to set different colors to checkbox's without drawables?
Thank you!
This did the trick for me. Notice the android namespace.
<style name="CheckBox.Custom" parent="#style/Theme.Material.Blue">
<item name="android:colorControlActivated">#color/custom_color</item>
</style>
You can set color directly in the xml. Use buttonTint for the box: (as of API level 23)
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#color/CHECK_COLOR" />
You can also do this using appCompatCheckbox v7 for older APIs:
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="#color/COLOR_HERE" />
Add this line in your styles.xml file:
<style>
<item name="android:colorAccent">#color/custom_color</item>
</style>