I have a problem that I want to create an Android App which captures the current wallpaper of Device Desktop, in the background. Which is already done in an App which is shown below in images. as:
Desktop of Device:
And Background of an Clock App:
So, My problem is the same as above, how we set the background of an Activity as the current Desktop WallPaper. Please suggest me the right solution about the same.
Thanks in advance.
set for your activity in the manifest file :
android:theme="#android:style/Theme.Wallpaper"
it will work even if the wallpaper is a live one
To see if current system has wallpaper, you use peekDrawable():
WallpaperManager wallpaperManager1 = WallpaperManager
.getInstance(getApplicationContext());
final Drawable wallpaperDrawable1 = wallpaperManager1.peekDrawable();
And Implement it like this:
if (wallpaperDrawable1==null)
{
Resources res = getResources();
Drawable drawable1=res.getDrawable(R.drawable.bg1);
getWindow().setBackgroundDrawable(drawable1);
}
else
{
getWindow().setBackgroundDrawable(wallpaperDrawable1);
}
Related
I'm using Xamarin Forms to produce a TabbedPage consisting of more ContentPages. This is the part of code causing trouble:
public void launchMainDesign(object s, EventArgs e) {
MainPage = new TabbedPage {
Children = {
new ContentPage {
Title = "Login",
Content = pages.loginContent,
BackgroundImage = "bgmain.jpg"
},
new ContentPage {
Title = "Sign Up",
Content = pages.signUpContent,
BackgroundImage = "bgmain.jpg"
}
}
};
}
It seems absolutely fine. I have both the images in my Drawable directory, with the build action set to "AndroidResource".
Whenever the launchMainDesign() function is fired by pressing a button, the app crashes immediately, both in emulator and a build version of the app on a tablet. Unfortunately, I can't test on iOS and WP.
I even tried putting the whole inside part of the function in a try/catch block and print out the exception, but the app just crashes nevertheless.
I am desperately trying to solve this simple problem for about a week now. No one seems to be having exactly the same issue as me. Weirdest thing is, I have a different app where I use exactly the same method and it works just fine. Can the Android Theme be causing this (I'm using Holo, in the working app, there's no theme specified)? That seems to be the only difference.
I also don't think this is caused by RAM struggles, as the image is only about 700 kilobytes (1080x1920) - for this example, I've only used one image.
It could be a memory issue, because even do the size is not big depending on the device resolution it might be trying to scale the image to the device dimensions.
Try checking this README:
https://github.com/xamarin/customer-success/blob/master/samples/Xamarin.Forms/SliderView/README.md
Explains Xamarin.Forms Android Image Memory Management so could help you get around the issue you might be having.
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)
Application's icon can be gotten by following code:
Drawable drawable = ResolveInfo.loadIcon(packageManager);
But it is the drawable that maybe modified by ROM (like: right-angle changed to round angle corner), so my question is how can I get original logo icon by program?
P.S. some launcher can do this, like MIUI launcher.
You can get application icon using PackageManager
PackageManager manager = context.getPackageManager();
Drawable appIcon = manager.getApplicationIcon(packagename);
public static Drawable getApplicationIcon(Context context, String packname){
PackageManager pm = context.getPackageManager();
try {
PackageInfo pInfo = pm.getPackageInfo(packname, 0);
int resId = pInfo.applicationInfo.icon;
AssetManager assetManager = AssetManager.class.newInstance();
AssetManager.class.getMethod("addAssetPath", new Class[]{String.class}).invoke(assetManager, pInfo.applicationInfo.sourceDir);
Resources oRes = context.getResources();
Resources resources = new Resources(assetManager, oRes.getDisplayMetrics(), oRes.getConfiguration());
Drawable result = resources.getDrawable(resId);
assetManager.close();
return result;
} catch (Exception e) {
e.printStackTrace();
}
return pm.getDefaultActivityIcon();
}
Sorry for the late reply. But I hope this will help someone new to android.
This method will get the icon associated with the Activity.
public static Drawable getOriginalActivityIcon(Context context,ResolveInfo resolveInfo){
PackageManager packageManager=context.getPackageManager();
try {
ActivityInfo info=resolveInfo.activityInfo;
Drawable drawable = packageManager.getDrawable(info.packageName,info.getIconResource(), packageManager.getApplicationInfo(info.packageName, 0));
if(drawable==null){
drawable=resolveInfo.loadIcon(packageManager);
}
return drawable;
}catch (Exception e){
e.printStackTrace();
}
return packageManager.getDefaultActivityIcon();
}
Use this code
getPackageManager().getApplicationIcon(packagename);
Drawable icon = getPackageManager().getApplicationIcon(packageInfo.packageName);
The conclusion is that if a round icon exists and no adaptive icon exists, you can't get a square icon. Unless this system is customized with a different Android framework than AOSP, such as the MIUI.
At first I was just like you, looking around for a way to load the square icon. I've tried every answer including the ones here.
Finally I found that if both square and round icons are set, then the Android system framework returns the ResId of the round icon when you request the ResId of the icon. At last I went searching for the code of the Android system framework and verified this guess:
When a developer specifies an adaptive application icon, and a non-adaptive round application icon, create an alias from the round icon to the regular icon for v26 APIs and up. We do this because certain devices prefer android:roundIcon over android:icon regardless of the API levels of the drawables set for either. This auto-aliasing behaviour allows an app to prefer the android:roundIcon on API 25 devices, and prefer the adaptive icon on API 26 devices.
If the application contains adaptive icons and the API level >= 26, you can use it to draw out square icon. For example.
I have written a program that can create shortcut of my application in android emulator homescreen.But the problem is when i created the shortcut then it has the default android icon.
My question is how can i change the icon of the shortcut ?
I have used the following line to set the icon
Intent j=new Intent();
j.putExtra(Intent.EXTRA_SHORTCUT_INTENT,i);
j.putExtra(Intent.EXTRA_SHORTCUT_NAME,n);
j.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,R.drawable.icon);
j.putExtra ("duplicate", false);
j.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(j);
The system is not in your context. You therefore need to give more details to send you icon.
see https://github.com/ldo/ShortcutCircus_Android/blob/master/src/Activity2.java
The icon is given by :
Intent.ShortcutIconResource.fromContext(Activity2.this, R.drawable.icon)
Or you could decode the bitmap and use EXTRA_SHORTCUT_ICON instead.
You can do this in two way.
putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,resId);
putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
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.