Here is sample code:
WallpaperManager wallpaperManager1 = WallpaperManager
.getInstance(getApplicationContext());
final Drawable wallpaperDrawable1 = wallpaperManager1.getDrawable();
getWindow().setBackgroundDrawable(wallpaperDrawable1);
if (wallpaperDrawable1==null)
{
Resources res = getResources();
Drawable drawable1=res.getDrawable(R.drawable.bg1);
getWindow().setBackgroundDrawable(drawable1);
}
I wanted to get the system background in my app, If its not there or if user removes it, then I wanted to set a default image from my app to set as app background. Hope Its all clear....
To see if current system has wallpaper, you use peekDrawable():
final Drawable wallpaperDrawable1 = wallpaperManager1.peekDrawable();
Also, you missed else:
if (wallpaperDrawable1==null)
{
Resources res = getResources();
Drawable drawable1=res.getDrawable(R.drawable.bg1);
getWindow().setBackgroundDrawable(drawable1);
}
else
{
getWindow().setBackgroundDrawable(wallpaperDrawable1);
}
Related
I cannot find a way to disable quick settings tile in Android programmatically that is a requirement for our enterprise launcher.
Are there any clues beyond the How to disable the notification bar pull-down in Android? and https://e2e.ti.com/support/embedded/android/f/509/t/283260
Is it possible to do? Thanks!
May you start your app in the full screen mode?
like explained here: https://stackoverflow.com/a/8470893/2801860
<activity
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
....
>
Yes you can do it. I had used the following snippet to disable quick settings.
public static void preventStatusBarExpansion(Context context) {
WindowManager manager = ((WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
Activity activity = (Activity)context;
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
int resId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
int result = 0;
if (resId > 0) {
result = activity.getResources().getDimensionPixelSize(resId);
}
localLayoutParams.height = result;
localLayoutParams.format = PixelFormat.TRANSPARENT;
customViewGroup view = new customViewGroup(context);
manager.addView(view, localLayoutParams);
}
Call this method where ever you need. Before calling this method make sure you has screen overlay permission.
However this permission is deprecated in Oreo.
It seems is impossible to do at all.
That is the answer.
No, not impossible. Use ACTION_CLOSE_SYSTEM_DIALOGS to prevent it be pulled down when the screen is locked.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativer);
layout.setBackgroundResource(R.drawable.bgorange);
I want the bgorange to get retrieved from a spinner. So bgorange is a static value for now.
In general, for getting a resource programmatically you can use:
public static final Drawable getDrawable(Context context, int id) {
return ContextCompat.getDrawable(context, id);
}
So, in your exmaple you can use it like this:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativer);
Drawable drawable = getDrawable(context, YourSpinner.getYourResBackground());
layout.setBackgroundDrawable(drawable)
Note that you need the latest support lib in your build.grade
compile 'com.android.support:support-v4:xxxxxx
I want to show an image, depending on the value of the int imgch
This is in my onCreate:
ImageView image = (ImageView)findViewById(R.id.gimage);
Resources res = getResources();
Drawable gameImage = res.getDrawable(images[imgch]);
And this in my XML:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/gimage" />
How do I get it to show the correct image in the activity?
Yes, but with java-side code
ImageView img = (ImageView) findViewById(R.id.gimage);
img.setImageDrawable(getDrawable(R.drawable.my_image_name));
If you wanna load from an remote url you may be use this
https://github.com/nostra13/Android-Universal-Image-Loader
Check this ImageView#setImageDrawable. So you should have image.setImageDrawable(gameImage)
Create an Array
int[] images;
on
protected void onCreate(Bundle savedInstanceState) {
add this
images = new int[3];
images[0] = R.drawable.first_image;
images[1] = R.drawable.second_image;
images[2] = R.drawable.third_image;
and then your code (where imgch is an integer)
...
Drawable gameImage = res.getDrawable(images[imgch]);
...
use
Resources resources = getResources();
image.setImageDrawable(resources.getDrawable(R.drawable.myfirstimage));
I have a Adobe Air application that intend to take a screenshot with Native Extension on Android device, but the java code returns a black image.
public FREObject call(FREContext context, FREObject[] params) {
View view = context.getActivity().getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap image = view.getDrawingCache();
}
I don't know much about Adobe Air. My java code runs exactly on Android Java Application, but returns black image on Adobe Air Android Application with Native Extension.
Is there any solution or any way to take a screenshot using Java in NativeExtension?
Thanks much!
Could be that you are not getting the correct view. Try this to get the topmost root view.
public FREObject call(FREContext context, FREObject[] params)
{
View view = findViewById(android.R.id.content).getRootView();
view.setDrawingCacheEnabled(true);
Bitmap image = view.getDrawingCache();
if(image == null)
{
System.out.println("Image returned was null!");
}
}
I also removed the buildDrawingCache() line; that can sometimes cause issues, and from what I've read it's not completely necessary.
Finally you'll want to check if the bitmap being returned is null. If so that could be why it's all black.
You can take a screenshot like this and save it to the SD card:
View content = findViewById(R.id.layoutroot);
content.setDrawingCacheEnabled(true);
Function to get the rendered view:
private void getScreen()
{
View content = findViewById(R.id.layoutroot);
Bitmap bitmap = content.getDrawingCache();
File file = new File( Environment.getExternalStorageDirectory() + "/asdf.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
You have to add this permission to your AndroidManifest (if you want to save it):
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I am trying to make Settings in the app, specificly a ListPreference to be able to change the background animation of the app, but I can't do it, nothing changes if you choose option 1 or option 2... I followed a similar tutorial and tried to make it work for me, but nothing so far.
Here is the java
SharedPreferences backChooser = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String values = backChooser.getString("list", "1");
img = (ImageView) findViewById(R.id.imageView1);
img.setBackgroundResource(R.drawable.anim1);
animation = (AnimationDrawable) img.getDrawable();
animation.start();
if (values.contentEquals("1")) {
img.setBackgroundResource(R.drawable.anim1);
animation = (AnimationDrawable) img.getDrawable();
animation.start();
}
if (values.contentEquals("2")) {
img.setBackgroundResource(R.drawable.anim2);
animation = (AnimationDrawable) img.getDrawable();
animation.start();
}
and here is the xml of the preferences
<?xml version="1.0" encoding="utf-8"?>
<ListPreference
android:entries="#array/list"
android:entryValues="#array/listvalues"
android:key="list"
android:summary="This is a list to choose from"
android:title="List" />
This line of code
img.setBackgroundResource(R.drawable.anim1);
sets an ImageView's "Background".
Were as this code
img.getDrawable();
gets an ImageView's "Drawable" or the image on it, not the actual "Background" image.
Since you want to retrieve the image that you've once set as the BG of "img", this code
img.getBackground();
is the one that you need.