This Android Program lets you create an Activity to set the Wallpaper of your Decive using Java.
Here is source code of the Program to create an Activity to set the Wallpaper of your Decive. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.
Main Activity
package com.example.backgroundapp; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button =(Button) findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub startActivity(new Intent("com.example.backgroundapp.bakapp")); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
Backapp Activity
package com.example.backgroundapp; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class bakapp extends Activity implements OnClickListener { ImageView display; int tophone; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // to our activity to cover the whole screen requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.back); Button but = (Button) findViewById(R.id.setwallpaper); tophone = R.drawable.image4; but.setOnClickListener(this); display = (ImageView) findViewById(R.id.ivdisplay); ImageView image1 = (ImageView) findViewById(R.id.IVimage1); ImageView image2 = (ImageView) findViewById(R.id.IVimage2); ImageView image3 = (ImageView) findViewById(R.id.IVimage3); ImageView image4 = (ImageView) findViewById(R.id.image4); ImageView image5 = (ImageView) findViewById(R.id.IVimage5); image1.setOnClickListener(this); image2.setOnClickListener(this); image3.setOnClickListener(this); image4.setOnClickListener(this); image5.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub Toast var; switch (v.getId()) { case R.id.IVimage1: display.setImageResource(R.drawable.image4); var = Toast.makeText(bakapp.this, "image changed", Toast.LENGTH_SHORT); var.show(); tophone = R.drawable.image4; break; case R.id.IVimage2: display.setImageResource(R.drawable.image5); var = Toast.makeText(bakapp.this, "image changed", Toast.LENGTH_SHORT); var.show(); tophone = R.drawable.image5; break; case R.id.IVimage3: display.setImageResource(R.drawable.image6); var = Toast.makeText(bakapp.this, "image changed", Toast.LENGTH_SHORT); var.show(); tophone = R.drawable.image6; break; case R.id.image4: display.setImageResource(R.drawable.image7); var = Toast.makeText(bakapp.this, "image changed", Toast.LENGTH_SHORT); var.show(); tophone = R.drawable.image7; break; case R.id.IVimage5: display.setImageResource(R.drawable.image8); var = Toast.makeText(bakapp.this, "image changed", Toast.LENGTH_SHORT); var.show(); tophone = R.drawable.image8; break; case R.id.setwallpaper: // to set a background we need to use bitmap InputStream is = getResources().openRawResource(tophone); // to phone is a variable that is updated everytime we click on an // ImageView to that imageview resource and by clicking the button // we set the phone background to that image. Bitmap bm = BitmapFactory.decodeStream(is); // decode inputstream is try { getApplicationContext().setWallpaper(bm); // to set the wallpaper of the phone background we need to ask // permission from the user so add permission of background from // manifest file } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } var = Toast.makeText(bakapp.this, "Wallpaper image changed", Toast.LENGTH_SHORT); var.show(); break; } } }
Xml
advertisement
advertisement
Main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Application to Change background picture of device" android:textSize="30dp" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="96dp" android:text="Click to launch" /> </RelativeLayout>
Backapp
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/ivdisplay" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" android:src="@drawable/image4" /> " <Button android:id="@+id/setwallpaper" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="set image background" > </Button> <HorizontalScrollView android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="center" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/IVimage1" android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp" android:src="@drawable/image4" /> <ImageView android:id="@+id/IVimage2" android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp" android:src="@drawable/image5" /> <ImageView android:id="@+id/IVimage3" android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp" android:src="@drawable/image6" /> <ImageView android:id="@+id/image4" android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp" android:src="@drawable/image7" /> <ImageView android:id="@+id/IVimage5" android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp" android:src="@drawable/image8" /> </LinearLayout> </HorizontalScrollView> </LinearLayout>
AndroidManifest
advertisement
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.backgroundapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.SET_WALLPAPER"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.backgroundapp.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.backgroundapp.bakapp" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="com.example.backgroundapp.bakapp" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>
Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.
advertisement
If you wish to look at all Tutorials, go to Java Android Tutorials.
Related Posts:
- Apply for Java Internship
- Check Java Books
- Practice Information Technology MCQs
- Apply for Computer Science Internship
- Check Programming Books