Styling the AppCompat Spinner background color - android

I'm trying to make a simple theme extending from AppCompat and I want to change the background color of a Spinner's pop-up. I can get pretty far in completely restyling AppCompat purely by using colors, but got stuck on the Spinner.
Inspecting the xml for Widget.AppCompat.Spinner, I see that its android:popupBackground is a 9-patch bitmap for API 20 and lower, but a vector drawable for 21 and higher. I know how to create a tinted 9-patch.
For 21+, the spinner background is defined in popup_background_material.xml as:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="2dp" />
<solid
android:color="?attr/colorPopupBackground" />
</shape>
I tried setting this color attribute in my values-v21 style like this:
<style name="MyTheme" parent="MyBaseTheme">
<item name="android:colorPopupBackground">#color/colorSpinnerBackground</item>
</style>
But at build time there is an error:
AAPT: error: style attribute 'android:attr/colorPopupBackground' is private.
Is there some simpler way of recoloring Spinners that I'm missing? I know I could copy-paste the vector xml file and change the color there, but I'd prefer to stick to colors only so if there are changes to AppCompat's theming, I can get them automatically by updating its version.
Currently using AppCompat 1.0.2.

I have a good example about custom spinner.
This example show how to change spinner background color.
but it is korean, not english
see link.
https://blog.naver.com/jinhan38/222094163563

Related

Android Background Drawable Not Working in Button Since Android Studio 4.1

I find out that since Android Studio 4.1 I cannot change the background color of a Button by setting color on its android:background, just no effect. And custom Drawable is not working as well.
My background Drawable:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1.5dp"
android:color="#android:color/black" />
<solid
android:color="#android:color/white" />
<corners
android:radius="8dp" />
</shape>
My Button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add To Cart"
android:background="#drawable/background3"/>
Result:
The Android Studio 4.1 new-project wizard, for many of its templates, has the project use the Material Components for Android library. And, it sets up the default theme to be based on Theme.MaterialComponents.DayNight.DarkActionBar.
A side effect of this is that any <Button> elements in a layout get turned into MaterialButton widgets, not regular Button widgets. MaterialButton ignores android:background.
If all you want to do is to change the color, use android:backgroundTint or change the colorPrimary attribute in the theme.
If you want a button that has a custom background, and your theme is set up to use Theme.MaterialComponents, you could switch the XML element in the layout to be <android.widget.Button> instead of <Button>. This should cause the Material Components for Android to ignore that element, and you can manipulate this button normally with respect to XML attributes.
UPDATE 03/2021
app:backgroundTint="#null" //just need this
android:background="#drawable/background_button"
Ok, since MaterialButton is the default Button starting from Android Studio 4.1, we can modify the shape by using app:shapeAppearanceOverlay attribute.
1. Create a Custom Style in themes.xml:
<style name="leaf">
<item name="cornerSizeTopLeft">70%</item> //can specify corner position
<!--<item name="cornerFamilyTopLeft">cut</item>-->
<item name="cornerSizeBottomRight">70%</item>
<!--<item name="cornerFamilyBottomRight">cut</item>-->
</style>
2. Apply Style in Material Button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Show"
app:shapeAppearanceOverlay="#style/leaf" /> //here
Result:
Helpful video:
https://www.youtube.com/watch?v=jihLJ0oVmGo
Add only this:
app:backgroundTint="#null"
I found easiest solution in this video.
https://www.youtube.com/watch?v=E_1H52FEqII
Just go to themes.xml under values folder in project window and change the MaterialComponents to AppCompact in both themes and button start working as normal as it was in past.
Existing: Theme.MaterialComponents.DayNight.DarkActionBar
After Modification: Theme.AppCompat.DayNight.DarkActionBar
It worked for me, hope it works for others as weell.
Just set null to backgroundTint.
android:backgroundTint="#null".

Background drawable not applying to Android button

I want to applying drawable to my android button.
But, background drawable is not working.
This is my drawable code.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFEF1D1D" />
</shape>
</item>
</layer-list>
and This is my layout code
<Button
android:id="#+id/booking"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6.5"
android:text="bokking"
android:textStyle="bold"
android:textColor="#color/white"
android:background="#drawable/booking_btn"
android:layout_marginRight="10dp"/>
Drawable is not applied to Button, but drawable is applied to AppCompatButton. I wonder why too.
When I applied this code, Only backgroundTint applies to buttons.
I spent a week on this issue, but I couldn't solve it.
plz help me
You can't apply the background on the material button.
If you are using Theme.MaterialComponents.*, then the normal button also used as the material button forcefully.
You have two options.
change it to any AppCompat theme.
Still If you need a material theme for some components, then use any material theme with a bridge. like Theme.MaterialComponents.*.Bridge
So, If you will go with option 2, you can use material button as per your need.
use com.google.android.material.button.MaterialButton for material button else use your normal button.
Looks like there is a problem with the Theme since Android Studio 4.1, try changing the base Theme from Theme.MaterialComponents.DayNight.DarkActionBar to Theme.AppCompat.DayNight.DarkActionBar, it works for me.
There is only one chance for that.I think you are using material theme.That's why the background property is not applying for button.If you want use that,change the theme to appcompat varaint.
You didn't use the selector in your XML. Use Selector instead of Layer-List

Change text color based on theme in a selector with different states

I try to change the color based on my theme. My TextView is using color-selector with different states for enabled and disabled and I want to use my theme based color in this selector.
I have followed this solution: android themes - defining colours in custom themes
My selector used as android:textColor in my view looks like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#ffffff" />
<item android:state_enabled="false" android:color="?attr/ThemeTest"/>
</selector>
with ThemeTest being my custom attribut which has a color assigned in my themes. If I use this selector as my textColor, the color is actually not what I picked but just a simple plain RED! HOWEVER if I use the custom attribut directly in my view
android:textColor="?ThemeTest"
then it works but I obviously want to do this based on the change of state of my view...
Does anybody understand this behaviour and know how to fix it? Thanks in advance!
Using a theme attribute inside a color selector XML file is only supported in the most recent versions of Android. To overcome this limitation you need to create one color selector file for each theme, and fill them with plain colors. Then create a theme attribute which points to the correct color selector depending on the theme.
source: https://plus.google.com/102404231349657584821/posts/XEeehfwanGy
edit: tested and it works flawlessly!

Use colorPrimary in drawable on pre-lollipop with AppCompat

i am using AppCompat 22.1.1.
For some reasons, my app can change its theme on the fly, during the user navigation. (i.e. move to an another part of the app, like in the google play store app, when you're moved from the "my apps" part, to the "movie" part for example)
To avoid creating one drawable background per theme, i tried to create a background like this :
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/colorPrimary" /></shape>
When changing the theme programmatically, i suppose the colorPrimary will change too, and a button inflated after that, will be tinted with the colour of the new theme.
But i have an inflate exception on pre-lollipop (but works on lollipop). The drawable cannot find the attribute attr/colorPrimary, why ?
Here is the simple theme.xml i'm using :
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/my_blue</item>
<item name="colorPrimaryDark">#color/my_blue_dark</item>
<item name="colorAccent">#color/my_blue_light</item>
</style>
The colors are in values/colors.xml, just hexa colors.. And all resources are in the "values" dir, and NOT in values-r21 dir.
Create a color_primary.xml color resource in res/color/:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary"/>
</selector>
Then reference this in your drawable:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/color_primary" />
</shape>
From my experience, I don't know, but I'm pretty sure there's nothing you can do via XML. Lots of framework resources are provided twice, in the form some_drawable_dark.xml and some_drawable_light.xml; it looks like you can't reference theme values from drawable and color folders.
So you should either:
Create x static resources, where x is the number of themes you are putting in;
Operate at runtime, using setColorFilter() and such. Depending on the case, it can be hard.

How can I get the default color of a list separator in Android in order to use it in a shape?

The question already says it: How can I get the default color of the list separator so that I can use it a shape like this:
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#FFCCCCCC" />
<solid android:color="#FFFFFFFF" />
</shape>
I would to use the color instead of #FFCCCCCC in the stroke color.
You can use :
android:divider="#FFCCCCCC"
to change color of listview seperator
2) to change the shape or make custom dividers, you can add a custom element in the row.xml of listview items. make sure to turn off dividers in this scenario.
3) If you have a drawable you want to use as divider you can set it programatically with:
list.setDivider(Drawable divider)
How can I get the default color of the list separator
First, there are multiple default list dividers, based on theme. Theme.Holo has one, Theme.Holo.Light has one, Theme.Material has one, etc.
Second, particularly in the case of Theme.Material, the divider might be tinted based on theme attributes (e.g., accent color).
Third, the divider is not a simple color, but rather is a nine-patch PNG file. If you go into $ANDROID_SDK/platforms/platform-NN/data/res/drawable-XXXX/, you will find them in the files beginning with list_divider_. In the above path, $ANDROID_SDK is wherever you installed your Android SDK, NN is some API level for an SDK platform that you downloaded, and XXXX is a density (e.g., hdpi).

Categories

Resources