I have a button in xml. On button press I wish to rapidly change the background and Text on that button.
I normally would use a code like this for the final result:
String rndm[] = {"A","B","C","D"};
{rnd = rndm[(int) (Math.random() * rndm.length)];}
{Button btn = (Button) findViewById(R.id.button1);
btn.setText(String.valueOf(rnd));
btn.setTextColor(Color.parseColor("#000000"));}
Before that is called though I would like perhaps a second or two of a "shuffling" effect.
I have tried using java.util.timer like this:
#Override
public void onClick(View v) {
new java.util.Timer().schedule(
new java.util.TimerTask() {
#Override
public void run() {
String rndm[] = {"A","B","C","D"};
{rnd = rndm[(int) (Math.random() * rndm.length)];}
{Button btn = (Button) findViewById(R.id.button1);
btn.setText(String.valueOf(rnd));
btn.setTextColor(Color.parseColor("#000000"));}
}}}, 100 );
Then making a few of these with different backgrounds to fire one after the other. I just can't seem to get the hang of it.
I may need a whole new method to do what I want to do, but I am not sure what the best wat to accomplish what I need is.
You should use Handler and make sure that the code that you want to run is in the runnable method :-)
Try something like this from your Activity
Handler handler = new Handler();
....
#Override
public void onClick(View v) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
// set your button color here, no need to use runOnUiThread()
// as this run() method is executed on Main thread
}
}, 100);
}
Related
I'm creating a button to stop a light sensor after grabbing two equal light values for a duration of 2 seconds using the handler's delay function. It works fine at first but when I press the button again, the delay seems to get shorter and shorter, and eventually doesn't happen at all.
stateLx.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onResume();
stateLx.setEnabled(false);
doub2x.setText(Double.toString(0));
doub1x.setText(Double.toString(0));
//Stabilization Handlers
m_handler = new Handler();
m_handlerTask = new Runnable() {
public void run() {
doub1 = movingValue[0];
doub1x.setText(Double.toString(doub1));
m_handler.postDelayed(this, 1000);
}
};
m2_handler = new Handler();
m2_handlerTask = new Runnable() {
public void run() {
doub2 = movingValue[0];
doub2x.setText(Double.toString(doub2));
m2_handler.postDelayed(this, 2000);
if (doub1 != doub2) {
instructions.setText("unequal");
} else {
instructions.setText("equal");
getInitialLightValue();
reCalculateInitial();
beerLambert(volValues, lxValuesNew);
}
}
};
//Handlers for stabilizer
m_handler.postDelayed(m_handlerTask, 1000);
m2_handler.postDelayed(m2_handlerTask, 3000);
lame.setText(Double.toString(absorbValues[0]));
stateLx.setEnabled(true);
}
});
I've tried putting the handlers both in and out of the onclick function, as well as including the onResume() function at the beginning of the button press, but I'm having no luck. Is there something wrong with my code?
You keep recreating your Handlers and Runnables. This is part of the problem, you should check to see if you are already polling before starting again.
Also, this is a side note -- but it's odd to call lifecycle methods, like onResume() yourself, since it calls up to the super.
I want to solve a problem that I have been trying to do so the last couple of days but I dont have that much experience and I couldnt find the solution enywhere else.
Anyway,
In my app I have a button in wich I have implemented the onClickClistener in order to respond to touch and inside that I have added a Handler which adds a delay and after that some code is being executed. My problem is that i want to detect any tap of the button whilst the delay is happening and the postDelyed function doesn't allow me to do so. How can I actually do that?
I've posted my code that is related on that.
Thanks in advance!
P.S(I dont mind not using this postDelayed thing.)
Button button = findViewById(R.id.myButtonId);
button.setOnClickListener(new View.OnClickListener){
#Override
public void onClick(View v) {
.......
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do some thing after the delay
}
}, randomDelay);
//Do other things
}
});
boolean previousTapDetected;
...
button.setOnClickListener(new View.OnClickListener){
#Override
public void onClick(View v) {
.......
if(previousTapDetected) {
//We got a tap during the delay
}
Handler handler = new Handler();
previousTapDetected = true;
handler.postDelayed(new Runnable() {
#Override
public void run() {
previousTapDetected = false;
//Do some thing after the delay
}
}, randomDelay);
//Do other things
}
});
I have the following click listener in android:
this.startButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
flag.set(false);
Runnable myRunnable = new Runnable() {
int updateInterval = 4000; //=4 second
ImageView currentImage = (ImageView)findViewById(R.id.picture);
public void x(){//dosomething}
#Override
public void run() {
if(!flag.get()) {
currentImage.postDelayed(this, updateInterval);
picture.setImageResource(images[current_image_index]);
caption.setText(captions[current_image_index]);
current_image_index++;
current_image_index = current_image_index % images.length;
}
}
};
myRunnable.run();
}
}
);
However I have another onClickListener that need to call the method x() from myRunnable. However I seem to not be able to do so. The context here is that when startButton is clicked, it causes images to be changed every 4 seconds, but I have another button called nextButton that when clicked changes the image and must reset the update interval, otherwise the next image will change inconsistently when the nextButton is pressed, thanks!
The program would not run as expected. You are overriding a method inside an
overriden method. Better still create a separate method to handle that event and
then call it up in your eventListener. Hope this helps
So I have some simple code but it seems to not be working.. any suggestions?
I just want an image to show after a button is pressed then become invisible after 2 seconds.
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
firstImage.setVisibility(ImageView.VISIBLE);
// delay of some sort
firstImage.setVisibility(ImageView.INVISIBLE);
}
}
The image never shows, it always stays invisible, should I be implementing this in another way? I've tried handlers.. but it didn't work, unless I did it wrong.
Never make your UI thread sleep!
Do this:
final Handler handler = new Handler();
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
firstImage.setVisibility(ImageView.VISIBLE);
handler.postDelayed(new Runnable(){
public void run(){
firstImage.setVisibility(ImageView.INVISIBLE);
}
}, DELAY);
}
}
Where you would set DELAY as 2000 (ms).
Well, you will need to add a delay between the two lines. Use a thread or a timer to do this.
Start a thread on click of a button. In the run method, change the ImageView's visibility to VISIBLE, then put the thread to sleep for n secs, and then change then make it invisible.
To call the imageView's setvisibility method, you will need a hanlder here.
Handler handler = new Handler();
handler.post(new Runnable() {
public void run() {
image.setVisibiliy(VISIBLE);
Thread.sleep(200);
image.setVisibility(INVISIBLE);
}
});
I know this question has already been answered, but I thought I would add an answer for people who like me, stumbled across this looking for a similar result where the delay was caused by a process rather than a "sleep"
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
firstImage.setVisibility(ImageView.VISIBLE);
// Run the operation on a new thread
new Thread(new Runnable(){
public void run(){
myMethod();
returnVisibility();
}
}).start();
}
}
private void myMethod() {
// Perform the operation you wish to do before restoring visibility
}
private void returnVisibility() {
// Restore visibility to the object being run on the main UI thread.
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
firstImage.setVisibility(ImageView.INVISIBLE);
}
});
}
I have the following code that responds to a button click, changes the view and then after 5 seconds switches the view back:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
Button test = (Button)findViewById(R.id.browseLocation);
test.setOnClickListener(testListener);
}
private TimerTask revert = new TimerTask(){
#Override
public void run() {
setContentView(R.layout.menu);
}
};
private OnClickListener testListener = new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.test);
Timer tim = new Timer();
tim.schedule(revert, 5000);
}
};
However this code does not work. The run method of the timetask is hit but setContentView fails. I assume it has something to do with scope inside the timetask.
How can I achieve the desired result?
Try yourActivityName.this.setContentView(). Do you know if revert is being called at all (i.e. using Logging)?
Found on another post that setContentView cannot be called from a non-UI thread.
Can achieve the desired affect using runOnUiThread, but not recommended.