0% found this document useful (0 votes)
17 views3 pages

Prog 4

The document describes how to create an Android application that changes the wallpaper image randomly every 30 seconds by clicking a button. It involves creating an activity with a relative layout, adding a textview and button. It saves 5 images in the drawable folder. The java code uses a timer task to schedule changing the wallpaper to the next image in the list every 30 seconds. It gets the drawable for each image and sets it as the wallpaper.

Uploaded by

Preeti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Prog 4

The document describes how to create an Android application that changes the wallpaper image randomly every 30 seconds by clicking a button. It involves creating an activity with a relative layout, adding a textview and button. It saves 5 images in the drawable folder. The java code uses a timer task to schedule changing the wallpaper to the next image in the list every 30 seconds. It gets the drawable for each image and sets it as the wallpaper.

Uploaded by

Preeti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Program-4: Develop an application to set an image as wallpaper.

On click of a button, the


wallpaper image should start to change randomly every 30 seconds.
1) Firstly Create an Application by Name “WallpaperActivity”
2) Go to xml code of design change the layout to “RelativeLayout”
3) Add TextView component & change the following properties:
• Size: 38dp
• Text: Wall Paper Change Application
• Center-Align
4) Add Button component & change the following properties:
• Size: 38dp
• Text: Click Here To Change Wall Paper
5) Save five images (jpg format) in the drawable folder.
In this example one.jpg, two.jpg, three.jpg, four.jpg and five.jpg images are saved in
drawable folder.

XML-CODE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="167dp"
android:layout_marginRight="167dp"
android:layout_marginBottom="409dp"
android:text="CLICK HERE" />
</RelativeLayout>

JAVA-CODE
package com.example.walpaper;

import androidx.appcompat.app.AppCompatActivity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
Button wallpaperChange;
Timer mytimer;
Drawable drawable;
WallpaperManager wpm;
int prev = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mytimer = new Timer();
wpm = WallpaperManager.getInstance(this);
wallpaperChange = (Button) findViewById(R.id.button1);
wallpaperChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setwallpaper();
}
});
}

private void setwallpaper() {


Toast.makeText(this, "setting Wallpaper please wait.", Toast.LENGTH_LONG).show();
mytimer.schedule(new TimerTask() {
@Override
public void run() {
if (prev == 1) {
drawable = getResources().getDrawable(R.drawable.i1);
prev = 2;
} else if (prev == 2) {
drawable = getResources().getDrawable(R.drawable.i2);
prev = 3;
} else if (prev == 3) {
drawable = getResources().getDrawable(R.drawable.i3);
prev = 4;
} else if (prev == 4) {
drawable = getResources().getDrawable(R.drawable.i4);
prev = 5;
} else if (prev == 5) {
drawable = getResources().getDrawable(R.drawable.i5);
prev = 1;
}
Bitmap wallpaper = ((BitmapDrawable) drawable).getBitmap();
try {
wpm.setBitmap(wallpaper);
} catch (IOException e) {
e.printStackTrace();
}
}
}, 0, 30000);
}
}

You might also like