In this Android Studio article iam going to show you how to Set Wallpaper Programmatically. so for good explanation you can watch the video for this article.
Also check my Android Development Courses
Now lets get started!
1: First of all create a new project in your Android Studio, and choose Empty Activity also iam using API 20 for this project.
2: OK after creating project in android studio open you AndroidManifest.xml file and you need to add this persimmon to the manifest file.
1 |
<uses-permission android:name="android.permission.SET_WALLPAPER"/> |
So after adding your AndroidManifest.xml will look like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.forogh.parwiz.setwallpaperp"> <uses-permission android:name="android.permission.SET_WALLPAPER"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
3: OK now open your activitiy_main.xml and we are going to add a button in our this file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set Wallpaper!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> |
4: So now open your MainActivity.java, first let me add the code and after i explain the codes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package com.forogh.parwiz.setwallpaperp; import android.app.WallpaperManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.io.IOException; public class MainActivity extends AppCompatActivity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setWallpaper(); } }); } public void setWallpaper() { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.salahpic); WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext()); try { wallpaperManager.setBitmap(bitmap); Toast.makeText(this, "Wallpaper Changed Successfully", Toast.LENGTH_SHORT).show(); } catch (IOException e) { Toast.makeText(this, "An Error Occured", Toast.LENGTH_SHORT).show(); } } } |
OK at the top first we have created the object of our button, and after that we have find the id of that button also we have created an onClickListener for the button, because i want when a user click on the button i want to change the wallpaper of my phone or virtual emulator. And this is the method for Wallpaper changing programmatically. also you need to add an image in your drawable folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public void setWallpaper() { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.salahpic); WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext()); try { wallpaperManager.setBitmap(bitmap); Toast.makeText(this, "Wallpaper Changed Successfully", Toast.LENGTH_SHORT).show(); } catch (IOException e) { Toast.makeText(this, "An Error Occured", Toast.LENGTH_SHORT).show(); } } |
So now run the code and this is the result wallpaper changed
Subscribe and Get Free Video Courses & Articles in your Email