Today i am going to see about how to set a image as wallpaper in 2 line of code in android.
we can easily set any image from your res folder as a wallpaper in a 2 line of code.
First step is you need to change your image as Bitmap, then use getApplicationContext() to set your wallpaper.That's it!
we can easily set any image from your res folder as a wallpaper in a 2 line of code.
First step is you need to change your image as Bitmap, then use getApplicationContext() to set your wallpaper.That's it!
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class ExampleApp extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ImageView imageView = (ImageView) findViewById(R.id.ImageView01);
imageView.setBackgroundResource(R.drawable.gall_2);
imageView.setBackgroundResource(R.drawable.gall_2);
Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.gall_2));
public void onClick(View v) {
Bitmap bitmap = BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.gall_2));
try {
getApplicationContext().setWallpaper(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
});
getApplicationContext().setWallpaper(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
test.xml
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Set as Wallpaper"></Button>
<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Set as Wallpaper"></Button>
<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>
Add permission in AndroidManifest.xml
<uses-permission android:name="android.permission.SET_WALLPAPER" />

No comments:
Post a Comment