Posted by : Unknown Jumat, 15 Juni 2018

package com.mycompany.myapp;

import java.io.IOException;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.WallpaperManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.main);

// Create an actionbar
ActionBar actionBar = getActionBar();
actionBar.show();

// Locate ImageView in activity_main.xml
ImageView mywallpaper = (ImageView) findViewById(R.id.wallpaper);

// Attach image into ImageView
mywallpaper.setImageResource(R.drawable.mery);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Create an actionbar menu
menu.add("Set as Wallpaper")
// Add a new Menu Button
.setOnMenuItemClickListener(this.SetWallpaperClickListener)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

return super.onCreateOptionsMenu(menu);
}

// Capture actionbar menu item click
OnMenuItemClickListener SetWallpaperClickListener = new OnMenuItemClickListener() {

public boolean onMenuItemClick(MenuItem item) {

// Retrieve a WallpaperManager
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(MainActivity.this);

try {
// Change the current system wallpaper
myWallpaperManager.setResource(R.drawable.mery);

// Show a toast message on successful change
Toast.makeText(MainActivity.this,
   "Wallpaper successfully changed", Toast.LENGTH_SHORT)
.show();

} catch (IOException e) {
// TODO Auto-generated catch block
}

return false;
}
};
}

AndroidManifest.xml

<uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.SET_WALLPAPER" >
    </uses-permission>




Main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/wallpaper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © BLOG GUE WANGI - Blogger Templates - Powered by Blogger - Designed by Johanes Djogan -