Question

I'm coding an app that has some wallpapers and I want a Toast to show up when a wallpaper has been applied. Currently it only shows up when you apply the first wallpaper. Any ideas on how to get it to show up for every wallpaper?

public class WallpaperActivity extends Activity implements OnClickListener {

ImageView display;
int toPhone;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wallpaper);
    // Show the Up button in the action bar.
    setupActionBar();

    toPhone = R.drawable.wallpaper_11;

    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.IVimage4);
    ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
    ImageView image6 = (ImageView) findViewById(R.id.IVimage6);
    ImageView image7 = (ImageView) findViewById(R.id.IVimage7);
    ImageView image8 = (ImageView) findViewById(R.id.IVimage8);
    ImageView image9 = (ImageView) findViewById(R.id.IVimage9);
    ImageView image10 = (ImageView) findViewById(R.id.IVimage10);
    ImageView image11 = (ImageView) findViewById(R.id.IVimage11);

    Button setWall = (Button) findViewById(R.id.BsetWallpaper);

    image1.setOnClickListener(this);
    image2.setOnClickListener(this);
    image3.setOnClickListener(this);
    image4.setOnClickListener(this);
    image5.setOnClickListener(this);
    image6.setOnClickListener(this);
    image7.setOnClickListener(this);
    image8.setOnClickListener(this);
    image9.setOnClickListener(this);
    image10.setOnClickListener(this);
    image11.setOnClickListener(this);

    setWall.setOnClickListener(this);

}

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch (v.getId()){
    case R.id.IVimage1:
        display.setImageResource(R.drawable.wallpaper_1p);
        toPhone = R.drawable.wallpaper_1;
        break;
    case R.id.IVimage2:
        display.setImageResource(R.drawable.wallpaper_2p);
        toPhone = R.drawable.wallpaper_2;
        break;
    case R.id.IVimage3:
        display.setImageResource(R.drawable.wallpaper_3p);
        toPhone = R.drawable.wallpaper_3;
        break;
    case R.id.IVimage4:
        display.setImageResource(R.drawable.wallpaper_4p);
        toPhone = R.drawable.wallpaper_4;
        break;
    case R.id.IVimage5:
        display.setImageResource(R.drawable.wallpaper_5p);
        toPhone = R.drawable.wallpaper_5;
        break;
    case R.id.IVimage6:
        display.setImageResource(R.drawable.wallpaper_6p);
        toPhone = R.drawable.wallpaper_6;
        break;
    case R.id.IVimage7:
        display.setImageResource(R.drawable.wallpaper_7p);
        toPhone = R.drawable.wallpaper_7;
        break;
    case R.id.IVimage8:
        display.setImageResource(R.drawable.wallpaper_8p);
        toPhone = R.drawable.wallpaper_8;
        break;
    case R.id.IVimage9:
        display.setImageResource(R.drawable.wallpaper_9p);
        toPhone = R.drawable.wallpaper_9;
        break;
    case R.id.IVimage10:
        display.setImageResource(R.drawable.wallpaper_10p);
        toPhone = R.drawable.wallpaper_10;
        break;
    case R.id.IVimage11:
        display.setImageResource(R.drawable.wallpaper_11p);
        toPhone = R.drawable.wallpaper_11;
        break;

    case R.id.BsetWallpaper:
        InputStream is = getResources().openRawResource(toPhone);
        Bitmap b = BitmapFactory.decodeStream(is);
        Toast.makeText(this, "Wallpaper set", Toast.LENGTH_SHORT).show();
        try{
            getApplicationContext().setWallpaper(b);
        }catch(IOException e){
            e.printStackTrace();
        }
        break;

    }

}

/**
 * Set up the {@link android.app.ActionBar}.
 */
private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.wallpaper, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}


}
Était-ce utile?

La solution 2

OK. So apparently, choosing the newer updated method of applying wallpapers and put the Toast in the try method would give me a Toast every time:

case R.id.BsetWallpaper:

        try{

            WallpaperManager.getInstance(getApplicationContext()).setResource(toPhone);
            Toast.makeText(this, "Wallpaper set", Toast.LENGTH_SHORT).show();

        }catch(IOException e){
            e.printStackTrace();
        }
        break;

Using the WallpaperManager would also apply the wallpapers faster without crashing the app once in a while. Thanks for the help guys!

Autres conseils

Add this Toast.makeText(this, "Wallpaper set", Toast.LENGTH_SHORT).show(); on your every case. That will do the trick.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top