how to change android spinner popupBackground - android

I was trying to change the android spinner popup window background by setting the android:popupBackground, but it didn't have any effect. Is there any way I can change it?
<Spinner
android:id="#+id/eventNameSpinner"
android:layout_width="160dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="6dp"
android:background="#drawable/btn_name"
android:paddingBottom="2dp"
android:paddingTop="2dp"
android:popupBackground="#drawable/bkg">

I presume you are trying to change the "outer" backgroud of a Spinner's popup, not the background of Spinner "pupup items". Also, I presume that by popup, you mean a Spinner's dialog mode, where a floating dialog window appears on top of your activity, as opposed to the new dropdown mode.
Strategy
For a clean, sustainable approach which behaves well across multiple Android platforms and reduces the need for redundancy in the App, I believe it is essential to understand what the official docs don't tell us. So follow me on a short journey.
The tricky part about Spinners is that an Adapter is used to connect them to data. While it is relatively easy to identify the hooks for changing the appearance of android.R.layout.simple_spinner_item and its friends, those only determine the style of the Spinner's currently displayed item as well as each single popup item. But it is the Adapter which is responsible for creating the ListView and other widgets which frame that ListView.
This complexity is probably the reason why Android has introduced some attributes which can be specified on-the-fly "for" the Spinner although they are then applied to the Spnner's children, such as android:popupBackground. This is not necessarily a clean approach, rather a limited set of convenience functions. Regarding popupBackground, btw, this was introduced in API level 1, but Spinners respect it only in spinnerMode=dropdown, which was introduced in API level 11. That's the reason why you'll never be notified if you use it wrongly.
Older Android Versions (such as 2.2)
ListView
Knowing that the Adapter creates a ListView, it's not a bad idea to change the ListView appearance in one's theme, so there's one single place for the design change and the styling straightforward, like so:
<style name="MyTheme" parent="#android:style/[reference to original theme]" >
<item name="android:listViewStyle">#style/myListView</item>
[...]
</style>
<style name="myListView" parent="#android:style/Widget.ListView">
[check Android's Widget.ListView to understand what you can change here]
</style>
AlertDialog
Unfortunately, there's more work ahead. Because android:prompt can be used to create a headline for the popup, the popup really consists of more than just the ListView.
Android uses an AlertDialog
Recent Android Versions (such as 4.2)
Now that the AlertDialogs are styled, we still have to address the fact that more recent versions of Android don't use AlertDialogs for Spinner dialogs any more. That doesn't hurt, because for those, the AlertDialog style shouldd be kept anyways. It just means we need to style the new popup as well.
To do so, create version specific theme XML files to pull the additional styles into your customized theme, and provide version specific style XML files.
Feel like trying it yourself, starting here?

android:popupBackground is only valid when using android:spinnerMode = "dropdown" , thats probably why it wasnt any effect in your code. You need to tell that spinner which mode its in with some XML.
android:spinnerMode = "dropdown"
Links
http://developer.android.com/reference/android/widget/Spinner.html#attr_android:popupBackground
http://developer.android.com/reference/android/widget/Spinner.html

Related

New Android 12+ MaterialSwitch and androidx.preference

So i'm updating my apps to fully support Monet and Material You guidelines, and the official site mentions a new design for the switches. I used it, and that's the result:
I have a preference screen using Androidx preferences library, latest version available at the time of writing, and the only way i found to theme the switches (except the manual theming, which makes no sense) is to use this line in the app's theme:
<item name="switchStyle">#style/Widget.Material3.CompoundButton.MaterialSwitch</item>
And using SwitchPreferenceCompat (it doesn't work in the regular SwitchPreference) this is what i get:
Regardless of the width (which is different, but can be changed) the disabled state is completely different and doesn't match the rest of the app. Why? and most importantly, why do they suggest to use a library which:
Doesn't support Material You out of the box
Doesn't support any new Material3 component
It's hard to properly customize in general
?
I don't want to be too critical, but this is out of my understanding.
EDIT: at the moment, i'm using switchCompat everywhere, to make the app uniform. Looking at the system apps, i can find 4 different type of switches: a custom switch similar to the second screenshot, the old one and the two types in this question. That's hella confusing.
I understand Google stance on this, they don't want to make androidx.* packages dependent to Material library itself, maybe they should provide a separate preference package but this time with fully Material widgets.
In order to have the brand new MaterialSwitch of Material 1.7.0 with preference, I've overridden its widgetLayout with a custom layout by android:widgetLayout="#layout/preference_material_switch" (in fact I applied that programmatically like .widgetLayoutResource = R.layout.preference_material_switch) and put the following on preference_material_switch.xml layout file,
<?xml version="1.0" encoding="utf-8"?>
<!-- Derived from https://github.com/androidx/androidx/blob/8cb282cc/preference/preference/res/layout/preference_widget_switch_compat.xml -->
<com.google.android.material.materialswitch.MaterialSwitch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:clickable="false"
android:focusable="false" />
And here is the result,
The same problem, After seeing these replies, I'm thinking to build the settings fragment without androidx.preference.
MKevin3 said:
I hate the provided Android preferences setup and look. So many times they changed the rules and broke what I had.
Not that this helps you probably but I just did my own to avoid all the headaches and I am in full control of the look. If push comes to hove you might consider doing this as well instead of fighting the "Android Way".
sc00ty said:
I gave up trying to use their widgets and fragments. It was so much less of a headache to spend a little time making my own compound widgets for each setting type.

Android Exposed Dropdown causes exception

I need to add a drop down to my app, which is running on the AppCompat theme, since we are not using the Material design components, but rather have our own design for buttons, etc.
For a simple feature, I need to use a dropdown and here I think the Exposed drop down from the Material design theme would work nicely.
However, if I'm not specifically using the MaterialComponents theme in my styles, the app will crash whenever I try to use the style on the TextInputLayout.
Is there a way of using the nice Exposed dropdown from MaterialComponents without using the entire theme (as this overrides all colors for buttons and such)? Or maybe just a simple way of getting a nice and simple dropdown otherwise. Spinners seem kinda hard to work with..
Cheers.

Can't make ProgressBar use material design style

I'm transitioning an app from Holo style to Material design, and am stuck with a ProgressBar widget that won't show up as it is supposed to.
It's supposed to be flat like (and that's what AndroidStudio's preview gives me):
But when on the emulator (running Android 7.1.1), I only get the version with an arrow tip (also, it's not being animated)
I've already tried removing every single bit of customization on top of the base AppTheme, which inherits from Material:
<style name="AppTheme" parent="android:Theme.Material.Light"></style>
The ProgressBar is declared without any styling whatsoever, plain:
<ProgressBar
android:id="#+id/sign_in_activity_login_progressbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true" />
I'm at a loss here, already removed everything on the project that could reference styling, but this still happens. Already cleaned/rebuilt the project many times. What am I missing? How can I make that progress bar look like the flat one?
This is the problem of the emulator. Sometimes emulator does not give us the proper result.
If you run it on a device then you will get to see the difference and it also depends on a device. If any device's OS is customized then the progress bar may look different.

Android Spinner Custom?

I am trying to create a spinner that looks exactly like the one in this android example, but not sure how or if it has to be custom? I cant imaging it would have to be a custom layout since it is shown this way in their example...
http://developer.android.com/guide/topics/ui/controls/spinner.html#Populate
I am not concerned with how the list looks, but just the way the spinner looks with the lower corner arrow. that is what I want.
This project is an attempt to backport the holo theme by building it into and Android Library project. You can link that library with your project in order to gain access to the holo theme widgets.
I have never used it but I would think that it contains the spinner widget that you are looking for.

How do I use common UI styles in Android?

I am writing my little Android app. I pop up a dialog control which is a nice, non-fullscreen, rounded-corners dialog by setting android:theme="#android:style/Theme.Dialog" on the activity in my manifest. That all works just as I expected. However it is just a drab, grey-titled dialog as in this screenshot:
I've noticed however that a LOT of applications, when they pop up dialogs have a nice, blue-themed title as in this screen shot.
I would assume this theme is some common theme, as it shows up in a LOT of different apps. I would assume it is something built in to the OS. (My phone is a Captivate with the official Froyo release). Of course it COULD be something that every developer simply re-coded on their own, but I doubt that.
Assuming that this is a common theme, how do I utilize it in my app? What changes do I need to make to my activity to have it use that theme?
Thanks in advance!
You can set your activity to use a default theme like Theme.Black. There are default themes and they are in R.style - although i'm not sure which are available to which platforms(i.e. i think the holo themes are for 3.0 and up...
http://developer.android.com/reference/android/R.style.html
see here http://developer.android.com/guide/topics/ui/themes.html for defining your own custom themes and scroll all the way down for using the "platform styles" and themes.
Rather messy (there doesn't seem to be a good reference for this), but the platform styles are defined in \platforms\android-\data\res\values\styles.xml and \platforms\android-\data\res\values\themes.xml. You can dig through those and figure out the theme/style IDs that are available at compile time.
Other than that its really just trial and error.
To make a dialog you need to extend the dialog class. And to have a nice title bar you can use:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
and have your own custom title.
to have a title use:
setTitle("MyTitle");
You can also assign your custom view for the title.

Categories

Resources