Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ImageButton's image change dynamically

 
iamiqbal.m Mohiuddin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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;

}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic