• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ImageButton's image change dynamically

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All:

I have two set of Images being displayed on ImageButtons and based on timer and random number i am changing the images by setting them dynamically.
but the new/ changed image is not reflected on screen. I believe it has something to do with refreshing the screen after setting the image.
I have looked at API i dont see anything as such. can anyone help me how the image on a imagebutton can be changed dynamically and displayed?

My code is as below:

package com.pkg.rain;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import com.pkg.rain.R.id;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import android.widget.ImageView;

public class Rain extends Activity {

public ImageButton imagebutton1;
public ImageButton imageButton2;
public ImageButton imageButton3;
public ImageButton imageButton4;
public ImageButton imageButton5;
public ImageButton imageButton6;

public Button playButton;

public EditText numberEditText;

Intent intent;
Timer timer;
int randomNumber=0;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Random randomGenerator = new Random();
int random = randomGenerator.nextInt(6)+1;
randomNumber = random;

setUpViews();
}


private void setUpViews(){

numberEditText = (EditText)findViewById(id.txtScore);

imagebutton1 = (ImageButton)findViewById(R.id.imagebutton1);
imageButton2 = (ImageButton)findViewById(R.id.imagebutton2);
imageButton3 = (ImageButton)findViewById(R.id.imagebutton3);
imageButton4 = (ImageButton)findViewById(R.id.imagebutton4);
imageButton5 = (ImageButton)findViewById(R.id.imagebutton5);
imageButton6 = (ImageButton)findViewById(R.id.imagebutton6);
intent = new Intent(this, Rain.class);

playButton = (Button)findViewById(R.id.play_button);
playButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
startTimer(1);
}
}
);
}

public void startTimer(int seconds){
timer = new Timer();
timer.schedule(new RemindTask(),1000, seconds*1000);

}

class RemindTask extends TimerTask {
public void run() {

setImages();

}
}

public void setImages(){
Random randomGenerator = new Random();
int random = randomGenerator.nextInt(6)+1;
randomNumber = random;

switch (randomNumber){
case 1:
imagebutton1.setImageResource(R.drawable.smileicon2);
imageButton2.setImageResource(R.drawable.icon);
imageButton3.setImageResource(R.drawable.icon);
imageButton4.setImageResource(R.drawable.icon);
imageButton5.setImageResource(R.drawable.icon);
imageButton6.setImageResource(R.drawable.icon);
break;
case 2:
imageButton2.setImageResource(R.drawable.smileicon2);
imagebutton1.setImageResource(R.drawable.icon);
imageButton3.setImageResource(R.drawable.icon);
imageButton4.setImageResource(R.drawable.icon);
imageButton5.setImageResource(R.drawable.icon);
imageButton6.setImageResource(R.drawable.icon);
break;
case 3:
imageButton2.setImageResource(R.drawable.icon);
imagebutton1.setImageResource(R.drawable.icon);
imageButton3.setImageResource(R.drawable.smileicon2);
imageButton4.setImageResource(R.drawable.icon);
imageButton5.setImageResource(R.drawable.icon);
imageButton6.setImageResource(R.drawable.icon);
break;
case 4:
imagebutton1.setImageResource(R.drawable.icon);
imageButton2.setImageResource(R.drawable.icon);
imageButton3.setImageResource(R.drawable.icon);
imageButton4.setImageResource(R.drawable.smileicon2);
imageButton5.setImageResource(R.drawable.icon);
imageButton6.setImageResource(R.drawable.icon);

break;
case 5:
imagebutton1.setImageResource(R.drawable.icon);
imageButton2.setImageResource(R.drawable.icon);
imageButton3.setImageResource(R.drawable.icon);
imageButton4.setImageResource(R.drawable.icon);
imageButton5.setImageResource(R.drawable.smileicon2);
imageButton6.setImageResource(R.drawable.icon);

break;
case 6:
imagebutton1.setImageResource(R.drawable.icon);
imageButton2.setImageResource(R.drawable.icon);
imageButton3.setImageResource(R.drawable.icon);
imageButton4.setImageResource(R.drawable.icon);
imageButton5.setImageResource(R.drawable.icon);
imageButton6.setImageResource(R.drawable.smileicon2);

break;

}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic