Here we are going to see how we can create wallpaper app in android.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:focusableInTouchMode="true"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/img_m"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="fitXY"
android:src="@drawable/pic1" />
<Button
android:id="@+id/btnSetWall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="Set Wall" />
<HorizontalScrollView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_1"
android:layout_width="125dp"
android:layout_height="85dp"
android:layout_margin="6dp"
android:scaleType="fitXY"
android:src="@drawable/pic1" />
<ImageView
android:id="@+id/img_2"
android:layout_width="125dp"
android:layout_height="85dp"
android:layout_margin="6dp"
android:scaleType="fitXY"
android:src="@drawable/pic2" />
<ImageView
android:id="@+id/img_3"
android:layout_width="125dp"
android:layout_height="85dp"
android:layout_margin="6dp"
android:scaleType="fitXY"
android:src="@drawable/pic3" />
<ImageView
android:id="@+id/img_4"
android:layout_width="125dp"
android:layout_height="85dp"
android:layout_margin="6dp"
android:scaleType="fitXY"
android:src="@drawable/pic4" />
<ImageView
android:id="@+id/img_5"
android:layout_width="125dp"
android:layout_height="85dp"
android:layout_margin="6dp"
android:scaleType="fitXY"
android:src="@drawable/pic5" />
<ImageView
android:id="@+id/img_6"
android:layout_width="125dp"
android:layout_height="85dp"
android:layout_margin="6dp"
android:scaleType="fitXY"
android:src="@drawable/pic6" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Example ::
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:focusableInTouchMode="true"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/img_m"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="fitXY"
android:src="@drawable/pic1" />
<Button
android:id="@+id/btnSetWall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="Set Wall" />
<HorizontalScrollView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_1"
android:layout_width="125dp"
android:layout_height="85dp"
android:layout_margin="6dp"
android:scaleType="fitXY"
android:src="@drawable/pic1" />
<ImageView
android:id="@+id/img_2"
android:layout_width="125dp"
android:layout_height="85dp"
android:layout_margin="6dp"
android:scaleType="fitXY"
android:src="@drawable/pic2" />
<ImageView
android:id="@+id/img_3"
android:layout_width="125dp"
android:layout_height="85dp"
android:layout_margin="6dp"
android:scaleType="fitXY"
android:src="@drawable/pic3" />
<ImageView
android:id="@+id/img_4"
android:layout_width="125dp"
android:layout_height="85dp"
android:layout_margin="6dp"
android:scaleType="fitXY"
android:src="@drawable/pic4" />
<ImageView
android:id="@+id/img_5"
android:layout_width="125dp"
android:layout_height="85dp"
android:layout_margin="6dp"
android:scaleType="fitXY"
android:src="@drawable/pic5" />
<ImageView
android:id="@+id/img_6"
android:layout_width="125dp"
android:layout_height="85dp"
android:layout_margin="6dp"
android:scaleType="fitXY"
android:src="@drawable/pic6" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
MainActivity.java
package com.example.malik.blog;
import android.content.Intent;
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.ImageView;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ImageView img_m;
Button buttonSetWall;
ImageView img_1,img_2,img_3,img_4,img_5,img_6;
int i;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_m = (ImageView) findViewById(R.id.img_m);
buttonSetWall = (Button) findViewById(R.id.btnSetWall);
img_1 = (ImageView) findViewById(R.id.img_1);
img_2 = (ImageView) findViewById(R.id.img_2);
img_3 = (ImageView) findViewById(R.id.img_3);
img_4 = (ImageView) findViewById(R.id.img_4);
img_5 = (ImageView) findViewById(R.id.img_5);
img_6 = (ImageView) findViewById(R.id.img_6);
i = R.drawable.pic1;
img_1.setOnClickListener(this);
img_2.setOnClickListener(this);
img_3.setOnClickListener(this);
img_4.setOnClickListener(this);
img_5.setOnClickListener(this);
img_6.setOnClickListener(this);
buttonSetWall.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.img_1:
img_m.setImageResource(R.drawable.pic1);
i = R.drawable.pic1;
break;
case R.id.img_2:
img_m.setImageResource(R.drawable.pic2);
i = R.drawable.pic2;
break;
case R.id.img_3:
img_m.setImageResource(R.drawable.pic3);
i = R.drawable.pic3;
break;
case R.id.img_4:
img_m.setImageResource(R.drawable.pic4);
i = R.drawable.pic4;
break;
case R.id.img_5:
img_m.setImageResource(R.drawable.pic5);
i = R.drawable.pic5;
break;
case R.id.img_6:
img_m.setImageResource(R.drawable.pic6);
i = R.drawable.pic6;
break;
case R.id.btnSetWall:
bitmap = BitmapFactory.decodeStream(getResources().openRawResource(i));
try {
getApplicationContext().setWallpaper(bitmap);
Toast.makeText(getApplicationContext(),"Wallpaper Set",Toast.LENGTH_LONG).show();
finish();
Thread thread = new Thread(){
public void run(){
try{
sleep(2000);
}catch (Exception e){
e.printStackTrace();
} finally {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
}
}
};
thread.start();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.malik.blog">
<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>
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.malik.blog"
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}