Autohide scrollbars when not scrolling in a ListView - android

In the new official Twitter app, the scrollbars in all the ListViews the app uses are hidden unless the user is scrolling through the list.
When you start scrolling, the scrollbars appear. When you stop, they fade out with an animation until they are gone completely.
I can't seem to find anything in the documentation that indicates this as being a standard feature.
Is this something included in the API? If not, anyone know how this might be done?

Confirmed : either use android:fadeScrollbars ( if you're API level 5 )
or try to use setOnScrollListener to check scroll status and hide/show the bars . Some code examples are in this thread:
how to detect Android ListView Scrolling stopped?

You can enable scroll bar fading for your entire app on API level 5 and newer via a custom theme and the fadeScrollbars style attribute by adding this to styles.xml:
<style name="Theme.App" parent="android:Theme.Light">
<item name="android:fadeScrollbars">true</item>
</style>
Then set the new theme for your application in AndroidManifest.xml:
<application android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:description="#string/description"
android:theme="#style/Theme.App">
Just be sure you're not overriding this global theme on individual activities. Earlier Android versions will safely ignore this unknown XML attribute and not fade the scrollbars.

I haven't used them yet, but you might play around with android:scrollbarDefaultDelayBeforeFade and android:scrollbarFadeDuration, available on all widgets (i.e., subclasses of View).

I followed Alex's answer and it worked using both the theme settings and through code.
GridView gridview = (GridView) findViewById(R.id.mygridView);
gridview.setScrollbarFadingEnabled(false);
I did encounter a problem however with the Gallery Component. Whilst the following will compile fine, it will throw a NullPointerException. I assume this is to do with a Gallery not having scrollbars to show/hide.
Gallery gallery = (Gallery) findViewById(R.id.myGallery);
gallery.setScrollbarFadingEnabled(false); // <-- this will throw an exception
Android 2.2

Related

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.

Why does setting android:background on a Button cause loss of L/R padding?

I am using Android Studio 2.2.1 with project with these settings:
compileSdkVersion 24
buildToolsVersion "24.0.2"
minSdkVersion 16
targetSdkVersion 24
If I use the GUI to change the button background, it adds this to the layout:
android:background="#color/colorPrimary"
Then, if I run the app on a 4.4 virtual device (Microsoft's Android Emulator because I'm on an AMD system and I want a fast emulator), or on a Samsung Galaxy S6 with Android 6.0.1, the button has the correct color but loses left and right padding and the text runs right to the left and right edge of the button.
If I set only the backgroundTint, then the button has the correct padding on the virtual device, but not the correct color. However, on the S6, it has the correct color and padding.
This seems like a bug somewhere, but where? Is it in the code generation or is this a bug in Android 4.4?
I think Android Studio should be doing whatever needed to make it work correct on both platform levels, whether it is as complex as some of these solutions:
Standard Android Button with a different color
or something more succinct.
My styles.xml file shows:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
and my AndroidManifest theme setting is:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
This seems like a bug somewhere, but where?
Right here:
android:background="#color/colorPrimary"
Is it in the code generation or is this a bug in Android 4.4?
No, it is your replacement background. It is a color, without any intrinsic padding, and without being wrapped in a StateListDrawable.
The stock background of Button widgets has some amount of padding enforced as part of the background itself (e.g., via some transparent pixels in the nine-patch PNG used for the background). The actual background itself is a StateListDrawable, which chooses one of several other drawable resources to apply based on the state of the Button (e.g., normal, pressed, focused, disabled).
To replace a Button background, you need to first use a StateListDrawable of your own. Otherwise, your Button will not appear to respond visually to click events or other state changes (e.g., being disabled). Then, you can either incorporate some padding into the backgrounds for your states, or put padding on the Button widget itself, as you see fit.
I think Android Studio should be doing whatever needed to make it work correct on both platform levels
Android Studio assumes that you know what you are doing. There is no hard-and-fast requirement that Button backgrounds have this sort of padding, and there is no hard-and-fast requirement that a Button be something that makes sense to users (versus "hey, why does this button seem to not respond visually when I tap on it?"). There will be scenarios where developers do want Button widgets to have no padding, such as in an implementation of a segmented-list-control sort of compound widget.
Personally, I think the decision to have some intrinsic padding in the Button background is regrettable. But, that's the way it was implemented back in Android 1.0, and Google has elected to maintain the approach, even with newer themes, presumably for backwards compatibility.
If I set only the backgroundTint, then the button has the correct padding on the virtual device, but not the correct color
I have not played with backgroundTint with appcompat-v7. It is possible that you are seeing a bug there. You might consider posting a separate question with a complete example, plus screenshots, to get more specific help with that particular concern.

Android marshmallow listview scrolling gets blurry

I have an android application which is running on production for several years. Lately, I have discovered a problem with a ListView in the app, that gets blurry while scrolling. The problem only occurs under Android Marshmallow.
Here is a screenshot of the ListView while scrolling
Any help would be much appreciated.
Thanks!
Preface: I posted a separate answer prior to this one, but that solution only worked on my MotoX. I later discovered it did not work for my Galaxy Tab A. The answer here seems to be more universal:
I was able to fix my scrolling blur by defining a separate ListView style for my application and specifying a different list divider. So, for my application theme I set this:
<item name="android:listViewStyle">#style/ListViewStyleNoBlur</item>
Where ListViewStyleNoBlur is defined as:
<style name="ListViewStyleNoBlur" parent="#android:style/Widget.ListView.White">
<item name="android:divider">#android:drawable/divider_horizontal_bright</item>
</style>
I specified these in a values-v23 resources folder so the change doesn't affect pre-Marshmallow devices.
My application theme is based off of android:style/Theme.Light, which is why my list view style's parent is android:style/Widget.ListView.White. My app min SDK is 8, which is why I'm using such an "old" theme. I also noticed that if I use a "newer" theme, such as Holo, the blur does not exist.
I also had this problem and, through trial-and-error, finally found a solution. Hopefully it will also work for you. This appears to be a bug in Marshmallow related to the scroll bar in list views. I had the following property set in my application theme, and removing this property fixed the blurry scrolling:
<item name="android:fadeScrollbars">false</item>
Using true instead of false also works, but is unnecessary since it is the default. I also discovered (through trail-and-error) that using android:fastScrollEnabled="true" causes the same blurring, but based on your scrollbar style, you do not appear to be using it.
In summary, don't use android:fadeScrollbars. If that doesn't fix your issue, try playing around with any other scrollbar-related styles you may be using on your ListView, bearing in mind these styles may be part of the view directly, or part of an activity or application theme.

Default scroll tooltip messing up as of Marshmallow

We have a scroll bar similar to Android Clock App 'Select Cities' activity, but its breaking now that we're making a Marshmallow supported build.
On M it was the wrong size and wrong width. We fixed that with an M-specific theme.
On L it is fine
On K it sits in the middle of the screen and spins, without following the scroll tap target. We guess that we could set a K specific theme too, but not sure how we would get it to follow the scroll bar.
What gives?
If you're using:
<item name="android:fastScrollOverlayPosition">aboveThumb</item>
change it to:
<item name="android:fastScrollOverlayPosition">atThumb</item>
Worked for me. For some reason "aboveThumb" doesn't play nice with pre-L devices.

how to change android spinner popupBackground

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

Categories

Resources