Programmatically Set Android Phone's Background

Programmatically set android phone's background

First one, you need to set the permission in your Manifest.xml file

 <uses-permission android:name="android.permission.SET_WALLPAPER"/>

And you can set the background with this:

Button buttonSetWallpaper = (Button)findViewById(R.id.set);
ImageView imagePreview = (ImageView)findViewById(R.id.preview);
imagePreview.setImageResource(R.drawable.five);

buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.drawable.five);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});

Background black when programmatically setting activity background

Don't get view via LayoutInflater, if your Activity has xml layout and you called setContentView(int resId) you just find your root view and set background.

FrameLayout layout = (FrameLayout) findViewById(...);
layout.setBackgroundResource(images[rand.nextInt(images.length)]);

If you want to get view via LayoutInflater :

LayoutInflater inflater = getLayoutInflater();
FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.activity_my, null);
layout.setBackgroundResource(images[rand.nextInt(images.length)]);
setContentView(layout);

Is it possible to programmatically make a call on background?

Yes it's possible for Android I think. You can use the AlarmManager or an Handler for the schedule problem. To decide which you have to take. Here an exceprt from the Android documentation:

Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.

For starting a call in Android you can take a look here.

Programmatically change background / source for ImageView in android

Optional: Remove the line

android:src="@drawable/android"
//This can be kept as a default image in case the file you want does not exist.

from the XML layout. Since the image may vary every time, you need to use

File file = new  File("/sdcard/Images/test_image.jpg");

if(file.exists()){

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

ImageView image = (ImageView) findViewById(R.id.imageview1);

image.setImageBitmap(bitmap);

}


Related Topics



Leave a reply



Submit