Hello stackoverflow citizen!
I want start and stop 2 progressBars on 1 activity.
I think oll progressBars must start in new thread and join with others.
For 1 progressBar i am write this code.
startBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
handler.post(runnable);
}
});
stopBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
handler.removeCallbacks(runnable);
}
});
}
int prolength=0;
Runnable runnable=new Runnable() {
#Override
public void run() {
prolength = inProgressBar.getProgress() + 1;
inProgressBar.setProgress(prolength);
timeTextView.setText(String.valueOf(prolength));
if (prolength < 100) {
handler.postDelayed(runnable, 1000);
}
else {
inProgressBar.setProgress(0);
timeTextView.setText(String.valueOf(0));
handler.post(runnable);
}
}
};
Thank you advance for help!
I suggest you to use sendMessage Api instead of post Api.
You can pass some information using Message object in your case (contentProgressBar1 or 2 or3 view) and refactor you implementation to this design..
Make sure to cleanup handler in activity life cycle else you will introduce memory leak and unwanted bugs.
Related
I have an ImageView, an ArrayList and a Button. My ArrayList has multiple numbers. For example 1,2,2,4,3. When i push the button, i want to animate the ImageView with the numbers inside ArrayList.
According to sample ArrayList. ArrayList[0]=1 ImageView will animate up, ArrayList[1]=2 ImageView will animate down. The animation will continue with all ArrayList inside. To do this, i have to use "for loop".
But the animation does not continue, the animation loop does not work. Only ArrayList[0] is working and then stop.`When i push the button second time, ArrayList[2] is working the stop. I have to push the button all the time for working all ArrayList. I want to push the button one time and i want to work all ArrayList.
//imageViewPlay=button hareketler=ArrayList imageViewMario=ImageView
imageViewPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i=0;i<hareketler.size();i++){
if (hareketler.get(0)==1){
imageViewMario.animate().y(imageViewMario.getY() -imageViewEngel.getHeight()-7).setDuration(2000).withEndAction(new Runnable() {
#Override
public void run() {
hareketler.remove(0);
}
});
}
if (hareketler.get(0)==2){
imageViewMario.animate().y(imageViewMario.getY() +imageViewEngel.getHeight()+7).setDuration(2000).withEndAction(new Runnable() {
#Override
public void run() {
hareketler.remove(0);
}
});
}
if (hareketler.get(0)==3){
imageViewMario.animate().x(imageViewMario.getX() +imageViewEngel.getHeight()+7).setDuration(2000).withEndAction(new Runnable() {
#Override
public void run() {
hareketler.remove(0);
}
});
}
if (hareketler.get(0)==4){
imageViewMario.animate().x(imageViewMario.getX() -imageViewEngel.getHeight()-7).setDuration(2000).withEndAction(new Runnable() {
#Override
public void run() {
hareketler.remove(0);
}
});
}
}
}
});
'Only ArrayList[0] is working and then stop' That is because you hardcoded this line with the value 0 :
if (hareketler.get(0)==...)
this will always only get the first item of your array.
You are incrementing your for loop, but not the positions that you are getting from the array.
if (hareketler.get(i)==...)
that will use the i value of the for loop to also increment the position of your array.
And then of course :
hareketler.remove(i)
This is because withEndAction is not a synchronous call and for did not wait for the first animation to end. What you need is recursive function which will be called again on animation end. Like this:
private void playAnimation(){
if (hareketler.get(0)==1){
imageViewMario.animate().y(imageViewMario.getY() -imageViewEngel.getHeight()-7).setDuration(2000).withEndAction(runnable);
}else if .... and so on
Also create a runnable which will call the method again on animation end
private Runnable runnable = new Runnable() {
#Override
public void run() {
hareketler.remove(0);
playAnimation()
}
};
And in your click listener:
imageViewPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playAnimation()
}
});
Hope this will work.
I have a function that requests data from an api and fills an array list.
Then i use the data from the arraylist in a textView. The problem that occurs is that the function takes time to load the data and the code in which i set the text view gets executed before the arraylist is populated resulting in a crash...I have used Countdown latch to tackle this problem but it isnt working
i have used it wrong most probably.
apirequest function
private void RequestDataFromApi() {
DotaAPIEndpoints textApiService= API_Client.getClient().create(DotaAPIEndpoints.class);
Call<List<Heroes>> call2 =textApiService.getHeroes();
call2.enqueue(new Callback<List<Heroes>>() {
#Override
public void onResponse(Call<List<Heroes>> call, Response<List<Heroes>> response) {
hero_list.clear();
hero_list.addAll(response.body());
}
#Override
public void onFailure(Call<List<Heroes>> call, Throwable t) {
Toast.makeText(MainActivity.this, "hero_list call failed!", Toast.LENGTH_SHORT).show();
}
});
requestLatch.countDown();
}
setText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
requestLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
textt.setText(hero_list.get(0).getHeroImg());
}
});
setText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
requestLatch.await();
You cannot call await on the UI thread. Calling await at this point in the above code is telling the UI thread to wait - if the UI thread is waiting, it cannot draw the screen updates, so the system will crash with an Activity Not Responding error.
Perhaps this helps, this is a way to safely allow the button to be clicked and not crash if the data has not loaded yet. (No need for a CountdownLatch at all)
setText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(hero_list.isEmpty()) {
Toast.makeText(MainActivity.this, "List not ready", Toast.LENGTH_SHORT).show();
return;
}
textt.setText(hero_list.get(0).getHeroImg());
}
});
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
}
});
On Android when I am mapping an address on a Mapview I have to wait for that action to return and then my user is given control back so I was wondering how can I send that function of to another thread?
mapLocationButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mapCurrentAddress(); //Send this function to new thread
}
});
How can this be done? And how will the new thread respond when it's done?
Android provides the AsyncTask class to do that. Read http://developer.android.com/reference/android/os/AsyncTask.html
You can use an AsyncTask to do this work in the background. Check this example
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
p_dialog = ProgressDialog.show((activity_name).this, "", "Loading Image...");
new Thread(new Runnable() {
public void run() {
function();
}
}).start();
}
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);
}
});
}