I am having a problem trying to refresh a View in an Android application. I have a button that have a image and what I need to do is to change the image when someone clicked the button.
Where is the problem? The image don't refresh until the activity finished proccessing the code. Any idea how I can refresh the image as soon as It execute the instruction
buttton1.setBackgroundDrawable(getResources().getDrawable(R.drawable.f1));
Have you considered using the xml side and have the drawables as selectors as then the selectors will get chosen by the particular key/touch event to display the correct graphic..
Try running your method that does your processing from a thread.
ficha1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button bot = (Button) findViewById(R.id.boton1);
bot.setBackgroundDrawable(getResources().getDrawable(R.drawable.f2));
//ficha.setText(fichas.get("boton1").toString());
new Thread(
new Runnable() {
public void run() {
controlJugada(fichas.get("boton1").toString(), bot);
}
}
).start();
}
});
I solved a similar problem by putting the offending code in a post delayed handler with a zero delay.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// code slowing things down here
}
}, 0);
The first thing you do in the onclick listner is change the backgrount of the button
Related
I am using picasso to save image to disk on tap of a button by the user and I want to generate a feedback in the form of a Toast to the user that the image has been downloaded.
For this, I am trying to run a toast on the UI Thread by using the following code::
((AppCompatActivity)context).runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
});
Which is not running. The image is downloaded and is also visible in the gallery of my app, but the Toast Does not show up. Can someone tell me if I am actually doing this right or should it be done some other way?
FYI: This code is being run in the onBitmapLoaded() method of Target object that I am passing to Picasso to download the Image into; The 'context' object here refers to the current Activity's context.
Any help would be appreciated :)
After doing a lot of search on net I got the answer. You need to display toast on main thread not on background thread. Following code will do the work
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
//Display toast here
}
});
Use the method that has a callback, there you can define a message for success and error.
final ImageView view = new ImageView(this);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(view, new Callback() {
#Override
public void onSuccess() {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
#Override
public void onError() {
}
});
Edit: Add line of the placeholder
I want to refresh my tile after clicking on a Download all button. I m using GridAdapter to display a grids for multiple videos. So If i click on Download all Button tile grid should show a progress bar, But currently it is not showing without refreshing the entire page.
We have tried below mentioned code on button click event but application getting crashed.....
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
File tempfile = new File(mContext.getExternalFilesDir(course_name),productCode);
if(tempfile.exists()){
viewHolder.mProgressBar.setVisibility(View.VISIBLE);
iewHolder.mImageView.setVisibility(View.INVISIBLE);
}else{
File tempthumbfile = new File(mContext.getExternalFilesDir(course_name),fname);
Log.d("tempthumbfile-->",""+tempthumbfile);
if(tempthumbfile.exists()){
viewHolder.mImageView.setBackgroundDrawable(Drawable.createFromPath(new File(mContext.getExternalFilesDir(course_name),fname).getAbsolutePath()));
}
viewHolder.mProgressBar.setVisibility(View.INVISIBLE);
viewHolder.mImageView.setVisibility(View.VISIBLE);
}
handler.postDelayed(this, 2000);
}
}, 1500);
Your question is not very specific, but try the following two approaches:
1) First and foremost, if you want to refresh a specific tile in your grid. Just call TileAdapter.notifyDataSetChanged();
2) If you are trying to e.g. blur the entire Grid and show a loading bar on top of it. Find a reference to the View hosting the grid and call view.invalidate();
try this :
Runnable task = new Runnable() {
public void run() {
this.runOnUIThread(new Runnable() {
public void run() {
// TODO: add your job/functionality here
}
});
}
}
I just want to call a function after every 3secs on click of a button
What is going wrong here-
galleryBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final Handler handler = new Handler();
for(int i = 0;i<3;i++){
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Do something after 5s = 5000ms
viewAnimator.showNext();
}
}, 3000);
}
}
});
You don't actually say what goes wrong but I'll take a wild guess that nothing happens (i.e. no animations) and the reason for that is probably that your Handler is being GC'd long before it gets to handle anything. Try keeping moving 'handlers' scope from local variable to class member.
(Also note that, even when it works, all 3 of your functions will run at more or less the same time. If you want them to run 3 seconds apart you should change the '3000' to 'i*3000'.)
I am having a problem I've been working for too long now. I'm trying to show a button, and after a delay, hide it.
birdBubble.setVisibility(vis);
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
birdBubble.setText("blalb alba");
handler.postDelayed(this, 2000);
birdBubble.setVisibility(invis);
}
});
birdBubble.setVisibility(invis);
i am doing all of this in a AsyncTask because i need to show a sequence of buttons. What it happens is that at the beginning the button is shown and after 2 seconds, the text is changed, but the button doesn't turn INVISIBLE. Any ideas? If you need more code, let me know. Thanks!
set the visilibility like following
birdBubble.setVisibility(View.VISIBLE);
birdBubble.setVisibility(View.INVISIBLE);
birdBubble.setVisibility(View.GONE);
After pushing a button, i want to close an activity. But, I’d like to wait some seconds before closing it, because users have to read a short message displayed on that activity.
I tried using Thread inside the onClick event
try{
Thread.sleep(2000);
finish();
}
catch(Exception e){}
But, when I push the button, the entire objects are freeze (for example, the button stay pushed).
Then I used a simple Timer
timer.schedule(task(), 2000);
And it seems to work well. Is it correct to use a Timer in this situation, or should I use a Thread or something else?
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Activity.this.finish();
}
}, 2000) ;
The easiest way is probably to use a Handler
private Handler h = new Handler();
...
h.postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
}, 2000);