how to change system wallpaper

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

how to change system wallpaper

Postby arvish2220 » Fri Mar 13, 2009 7:04 am

hello guys,
i have a problem. when i select an image from an image switcher , i want to set this selected image as the background of my application when i click on a button .. how can i do this.. here is a sample of my codes.

thanxs in Advance.
----------------------------------------------------------------------

package com.androwall;

import java.util.ArrayList;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import android.widget.Gallery.LayoutParams;


public class androwall extends Activity implements
ViewSwitcher.ViewFactory, AdapterView.OnItemSelectedListener {

/** Called when the activity is first created. */

private int mCurrentSelectedImage;
private TextView mTVCurrentWallpaper;
private Boolean bool = true;

private ArrayList<Drawable> allimages = new ArrayList<Drawable>();

@Override
public void onCreate(Bundle icicle)

{
super.onCreate(icicle);

requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);

for (int i = 0; i < mImageIds.length; i++)
{
allimages.add(this.getResources().getDrawable(mImageIds[i]));
}


mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));

Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));

gallery.setOnItemSelectedListener(this);

final Button btnSetWallpaper = (Button) findViewById(R.id.btnsetwallpaper);
btnSetWallpaper.setOnClickListener(btnSetWallpaperListener);


}//end of onCreate


//when clicked on button set as wallpaper
private Button.OnClickListener btnSetWallpaperListener = new Button.OnClickListener()
{
public void onClick(View v)
{
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
try
{


Bitmap newwallpaper = Bitmap.createBitmap(newwallpaper, 320, 240, mCurrentSelectedImage, mCurrentSelectedImage, null, true);



Canvas myCanvas = new Canvas(newwallpaper);
allimages.get(mCurrentSelectedImage).draw(myCanvas);
setWallpaper(newwallpaper);



broadcastIntent (new Intent (Intent.ACTION_WALLPAPER_CHANGED)); //added by arvish for test

}
catch (Exception e)
{

e.printStackTrace();//added to test
}

}

private void broadcastIntent(Intent intent)
{
// TODO Auto-generated method stub

}


};

public void onItemSelected(AdapterView parent, View v, int position, long id)
{
mCurrentSelectedImage = position;
mSwitcher.setImageDrawable(allimages.get(position));



}//end of public void onItemSelected


public void onNothingSelected(AdapterView parent)
{
}

public View makeView()
{
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));//added by arvish to test
return i;

}//end of public View makeView


private ImageSwitcher mSwitcher;


public class ImageAdapter extends BaseAdapter
{
private Context mContext;

public ImageAdapter(Context c)
{
mContext = c;
}

public int getCount()
{
return allimages.size(); // to check

}

public Object getItem(int position)
{
return position;
}

public long getItemId(int position)
{
return position;
}


public View getView(int position, View convertView, ViewGroup parent)
{
ImageView galleryview = new ImageView(mContext);

galleryview.setImageDrawable(allimages.get(position));

galleryview.setAdjustViewBounds(true);
galleryview.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return galleryview;

}//end of public View getView


public float getAlpha(boolean focused, int offset)
{
return Math.max(0.2f, 1.0f - (0.2f * Math.abs(offset)));
}

public float getScale(boolean focused, int offset)
{
return Math.max(0, offset == 0 ? 1.0f : 0.6f);
}



}//end of class ImageAdapter extends BaseAdapter {


private Integer[] mImageIds = {
R.drawable.p0, R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.test1};





}//end of class androwall extends Activity i
arvish2220
Freshman
Freshman
 
Posts: 2
Joined: Fri Mar 13, 2009 6:46 am
Location: Mauritius

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 2 guests