I'm using following function to enable and disable drawables...
public static void setDrawableState(Drawable d, boolean enabled)
{
if (d == null)
return;
d.mutate(); // so drawables don't share state anymore
if (enabled)
d.setAlpha(255);
else
d.setAlpha(100);
}
This worked on all phones I've tried yet, now I see it does not seem to work on android 4.4.2 (maybe it's not even version specific).
Is there another (better) way to set the alpha of a drawable? Or am I'm missing something?
Because drawables might share the same state, changing the drawable state will not have any effect. You need to mutate the drawable, for example in your code, try something like:
d.mutate().setAlpha(100);
Android developer Blogs has a great blog post explaining more on drawable state and mutations.
in case you wanna be really sure:
Drawable d2 = d.getConstantState().newDrawable().mutate();
d2.setAlpha(100)
Related
The bounty expires in 14 hours. Answers to this question are eligible for a +50 reputation bounty.
km11 wants to draw more attention to this question:
Should not experience any more crashes with the proposed changes
In my latest release, I suddenly started getting this error: Resources$NotFoundException: Resource ID #0x7f080120. The resource ID is my notification icon, which is used in one of the tabs in a ViewPager, which is displayed at startup. I am not able to reproduce the issue myself, but see a lot of new crashes in Firebase Crashlytics.
The notification icon is stored in multiple densities:
The code that causes the crash:
ContextCompat.getDrawable(context, R.drawable.ic_notification_icon)
The weird thing is that I did not change anything related to this drawable or the code in the ViewPager in this release. The best suggestion I found from searching around, was to clean and rebuild the project. I did this and uploaded a new version, but the crash reappeared.
The crash is observed on the following Android versions:
Android 13
Android 11
Android 8.1.0
Android 7.1.2
And the following devices:
Galaxy A53 5G
Nexus 5X
Standard PC (i440FX + PIIX, 1996)
vivo S1
OnePlus8Pro
Mi 9x
Edit:
I can see that 44% of the affected devices are rooted. Does it mean I should not worry about the issue?
We are too facing this kinda of error a lot in our app. So,By doing little bit of search, I am changing ContextCompat to AppCompatResources to get the drawables. I seriously don't know now yet it will be beneficial for our app or not but reason to change is simply because the way ContextCompat returns result with respect to the way the AppCompatResources returns result. ContextCompat is just calling context's getDrawable method which in turn calls Resource's getDrawable method which in turn calls ResourceImpl getValueForDensity method and finally calling a native method. Whereas AppCompatResources calls AppCompatDrawableManager's getDrawable method which try different approaches to get the drawable and eventually fallback to ContextCompat if none of the approaches work. This seems to be better candidate for me get the drawable.
AppCompatDrawableManager's getDrawable method
public Drawable getDrawable(#NonNull Context context, #DrawableRes int resId,
boolean failIfNotKnown) {
checkVectorDrawableSetup(context);
Drawable drawable = loadDrawableFromDelegates(context, resId);
if (drawable == null) {
drawable = createDrawableIfNeeded(context, resId);
}
if (drawable == null) {
drawable = ContextCompat.getDrawable(context, resId);
}
if (drawable != null) {
// Tint it if needed
drawable = tintDrawable(context, resId, failIfNotKnown, drawable);
}
if (drawable != null) {
// See if we need to 'fix' the drawable
DrawableUtils.fixDrawable(drawable);
}
return drawable;
}
This answer also has useful information too.
ContextCompat is creating the issue
use it directly
getDrawable(context, R.drawable.ic_notification_icon)
So, my boss asked me to analyze how to implement Material You colors starting from Android 12. I've been researching for it and every blog of every page about "how to do it" tells you that, you need to hold down at home screen, then go to "Wallpapers & Style" and change your wallpaper. There is an issue, though.
I remember that while Android 12 was in Beta, it was supporting Material You colors. However (I assume) after the official release, this support has been removed, because I am unable to find the option. Here is what it looks like when I hold down while at home screen:
It says "Wallpapers" and when I click on it, it does not open a menu called Wallpaper & style, it just redirects to Live Wallpapers. I've unable to find the wallpaper colors option on the official Android 12 release. However, it is present on the upcoming API 32 (Android 13 I believe) emulator.
Upon researching a little bit, I've found out that the system generates the wallpaper colors under the system resources such as system_accent1_600 etc. which are available starting from API 31 (more info here). This does work when I use an API 32 emulator which is in beta, but it defaults to something else (a shade of blue on accent colors, and shades of gray on neutral, a.k.a background colors) that I haven't figured out where from on an API 31 official emulator.
Here is the question:
Is Material You colors officially supported starting from Android 12 (API 31)? If yes, then why am I not able to find it on Android's official emulator?
Also, is it possible to detect if the device supports Material You colors with different options?
I think this is what you are looking for:
public static boolean isDynamicColorAvailable()
{
return VERSION.SDK_INT >= VERSION_CODES.S && DYNAMIC_COLOR_SUPPORTED_MANUFACTURERS.contains(Build.MANUFACTURER.toLowerCase());
}
Found this in DynamicColors class in com.google.android.material.color
You can simply call it like this:
boolean isdca = DynamicColors.isDynamicColorAvailable();
I hope this answers the last part.
1. Is Material You colors officially supported starting from Android 12 (API 31)?
Yes! But it based how the ROM implements. If you using Pixel, the you can change the color via the built wallpaper app. But if you're using AOSP, sine there is not an official way in UI to user to change it.
Check out this doc: https://gist.github.com/zhaofengli/232f5a3d33113871ad61491629886084
2. If yes, then why am I not able to find it on Android's official emulator?
It looks like Google removed it from the mirror. The previous mirror had this feature.
3. Also, is it possible to detect if the device supports Material You colors?
Since Android 12 supports Material You officially, so you can just check the api version simply.
But, according to the second point, some systems still don't support it, so you can use the method com.google.android.material.color.DynamicColors#isDynamicColorAvailable. This is the definitive method used by Material Design, depending on the SDK version and phone manufacturer.
https://cs.github.com/material-components/material-components-android/blob/2ae3ca42985722900f53de9d9a1ef61c143767eb/lib/java/com/google/android/material/color/DynamicColors.java#L279-L289
4. What is the correct way to implement Material You?
XML way: Follow the official doc: https://m3.material.io/libraries/mdc-android/color-theming
Programmatically way:
Check out my app's code:
val Context.colorOnPrimary: Int
get() {
return when {
DynamicColors.isDynamicColorAvailable() -> {
getColorFromAttr(R.attr.colorOnPrimary)
}
isNight() || !supportNight() -> {
ContextCompat.getColor(this, R.color.md_theme_dark_onPrimary)
}
else -> {
ContextCompat.getColor(this, R.color.md_theme_light_onPrimary)
}
}
}
#ColorInt
fun Context.getColorFromAttr(
#AttrRes attrColor: Int,
typedValue: TypedValue = TypedValue(),
resolveRefs: Boolean = true
): Int {
theme.resolveAttribute(attrColor, typedValue, resolveRefs)
return typedValue.data
}
I know that there are a lot of questions about capturing screenshots, and I have checked most of them. They have the same answer (with small code variations).
I have following method for screenshot capturing:
#NonNull
public static Bitmap takeScreenShot(Window window) throws IOException {
final View rootView = window.getDecorView().getRootView();
final boolean drawingCacheEnabled = rootView.isDrawingCacheEnabled();
rootView.setDrawingCacheEnabled(true);
try {
return Bitmap.createBitmap(rootView.getDrawingCache());
} finally {
rootView.setDrawingCacheEnabled(drawingCacheEnabled);
}
}
And you can use it like these: takeScreenShot(getActivity().getWindow())
However these approach has several limitations:
If you have some dialogs on the screen they will not be captured on
screenshot.
Will it work with hardware accelerated views? According
to documentation:
When hardware acceleration is turned on, enabling the
drawing cache has no effect on rendering because the system uses a
different mechanism for acceleration which ignores the flag
Screenshot contains black boxes
instead of GLviews. (e.g. when you app has maps.). It seems to be as a result of 2nd point.
So my question is, is there any solution without rooting that can solve at least some of my issues?
Check out the following GitHub repo (not mine!): https://github.com/AndroidDeveloperLB/ScreenshotSample
Also, the following will be useful reading:
How to properly take a screenshot, globally?
I am using UMP example provided by Google, I have not made any change in my code, even I have not tried anything out of the box, I just imported your project into my work-space and checked it on my device, and found that I am not getting Thumb with Genres (Songs by genre) and List of Genres...
Whereas I supposed to get Thumb from our JSON, here is what I have tried (but no success) -
holder.mImageView.setImageBitmap(description.getIconBitmap());
UPDATE # 1 AS PER SUGGESTED BY #NageshSusarla here
holder.mTitleView.setText(description.getTitle());
holder.mDescriptionView.setText(description.getSubtitle());
AlbumArtCache cache = AlbumArtCache.getInstance();
Bitmap art = cache.getIconImage(url);
if (art == null) {
cache.fetch(url, new AlbumArtCache.FetchListener() {
#Override
public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
if (artUrl.equals(url)) {
holder.mImageView.setImageBitmap(icon);
}
}
});
} else {
holder.mImageView.setImageBitmap(bitmap);
}
holder.mImageView.setImageBitmap(description.getIconBitmap());
and getting Cannot resolve symbol 'url'
The icon bitmap may not have been set. It's best to use the AlbumartCache to fetch the icon and then set it on the imageView. The url to be passed to AlbumArtCache.getInstance().fetch(url,..) is description.getIconUri().toString()
The reason you may not be seeing it in uAmp is because of the tint being applied to it. You can remove the tint from media_list_item.xml to try out the changes.
Aside: This is indeed by design and the icon is only shown at the bottom when a user selects the item to be played.
This is by design. To keep the navigation cleaner, we decided to not
show the MediaItem icon on the local browsing UI. Other browsing UI's
may show it, like Android Auto and Android Wear.
I think you should check it in issues of android-UniversalMusicPlayer. Take a look at comment given by mangini here.
If you want to change uAmp to show the MediaDescription.getIconUri,
set the holder.mImageView at this.
I am trying to figure out some way to determine the current background on a phone and then set that background as my own in my application to create a seamless transition between my application and the phone. However I haven't been able to find any functions for this at Androids SDK site.
Setting android:theme="#style/Theme.NoBackground" inside my Manifest almost does what I want, but it still has the icons in the background.
Thanks in advance
Yeah the WallpaperManager was the answer, a little searching revealed this in one of the API Demos for anyone who needs this.
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
Obviously add the variable wallpaperDrawable to setBackgroundDrawable();
You should also be able to declare in your manifest that your activity should use the 'wallpaper' theme, like so:
<activity android:theme="#android:style/Theme.Wallpaper">
Try to use the following code in your program, so that you can overcome your problem:
activity android:theme="#android:style/Theme.Wallpaper"
this will help for you.
You can do call this.getWallpaper() in your Activity in prior 2.0.