Android delay before the next action - android

in my new Android app I need to push a button to make visibile a progress bar but I need to make it invisible after 3 seconds.
I found this on the web but It does not work:
public void search(View view) throws InterruptedException {
ProgressBar progressbarSearch = (ProgressBar) findViewById(R.id.progressBar);
Button searchbutton = (Button) findViewById(R.id.button);
progressbarSearch.setVisibility(1);
searchbutton.setText("Searching devices...");
Handler myHandler = new Handler();
//this waits 3 seconds, then will call the run() method below.
myHandler.postDelayed(stopSearching, 3000);
}
private Runnable stopSearching = new Runnable()
{
#Override
public void run()
{
ProgressBar progressbarSearch = (ProgressBar) findViewById(R.id.progressBar);
progressbarSearch.setVisibility(0);
}
};
Where is the error? The progress bar starts after the button is pressed but it does not become invisible after 3 seconds.

progressbarSearch.setVisibility( View.INVISBLE );
You should use a constant here, it's really error prone and not maintainable to use a value. (Proof by example :))

Button searchbutton = (Button) findViewById(R.id.button);
searchbutton.setText("Searching devices...");
searchbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ProgressBar progressbarSearch = (ProgressBar) findViewById(R.id.progressBar1);
progressbarSearch.setVisibility(View.VISIBLE);
try {
search(v);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void search(View view) throws InterruptedException {
Handler myHandler = new Handler();
// this waits 3 seconds, then will call the run() method below.
myHandler.postDelayed(stopSearching, 3000);
}
private Runnable stopSearching = new Runnable() {
#Override
public void run() {
ProgressBar progressbarSearch = (ProgressBar) findViewById(R.id.progressBar1);
progressbarSearch.setVisibility(View.INVISIBLE);
}
};
change your code like this

Related

Inactive button for 5 seconds before displaying it

Goal:
When you start the android app, the button should not be displayed after 5 seconds.
Problem:
The code doesn't work and what part am I missing?
Info:
*Im new in android
*The code is inspired from this page Android - Hide button during an onClick action
Thank you!
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button2 = (Button) findViewById(R.id.btn_test);
button2.setVisibility(GONE);
new Thread(new Runnable() {
#Override
public void run() {
try
{
//dummy delay for 5 second
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
runOnUiThread(new Runnable() { //resetting the visibility of the button
#Override
public void run() {
//manipulating UI components from outside of the UI Thread require a call to runOnUiThread
button2.setVisibility(VISIBLE);
}
});
}
}).start();
}
}
This can be achieved in a simpler way. If your needed sequence is:
Start the app -> Display a button -> Wait 5 seconds -> Hide the button
final Button button2 = (Button) findViewById(R.id.btn_test);
button2.postDelayed(new Runnable() {
#Override
public void run() {
if (!isDestroyed() && !isFinishing()) {
button2.setVisibility(View.GONE);
}
}
},5000);
Otherwise, if you should display the button after 5 seconds after app launch, then just set button's visibility to GONE in your layout and change button2.setVisibility(View.GONE) to button2.setVisibility(View.VISIBLE) inside post delayed action
You need to set a listener to start your command, onCreate is the creation.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button button2 = (Button) findViewById(R.id.btn_test);
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
button2.setVisibility(GONE);
new Thread(new Runnable() {
#Override
public void run() {
try
{
//dummy delay for 5 second
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
runOnUiThread(new Runnable() { //resetting the visibility of the button
#Override
public void run() {
//manipulating UI components from outside of the UI Thread require a call to runOnUiThread
button2.setVisibility(VISIBLE);
}
});
}
}).start();
}
this code will hide the button AFTER onClick, start the thread, and after 5 seconds it will appear again.

buttons becoming visible sequentially

I am trying to perform sort of a stop light function on a button press but instead there are two of every color. The desired functionality would be red, red, yellow, yellow, green, and green. I have tried to set a delay in between each color but it doesn't happen sequentially it just adds up the delays and they all show up at once.
Here is the snippet of the activity that I have tried to do this:
Button previewButton = (Button) findViewById(R.id.preview_visual_button);
final Button redButton1 = (Button) findViewById(R.id.red_button_1);
final Button redButton2 = (Button) findViewById(R.id.red_button_2);
final Button yellowButton1 = (Button) findViewById(R.id.yellow_button_1);
final Button yellowButton2 = (Button) findViewById(R.id.yellow_button_2);
final Button greenButton1 = (Button) findViewById(R.id.green_button_1);
final Button greenButton2 = (Button) findViewById(R.id.green_button_2);
redButton1.setVisibility(View.GONE);
redButton2.setVisibility(View.GONE);
yellowButton1.setVisibility(View.GONE);
yellowButton2.setVisibility(View.GONE);
greenButton1.setVisibility(View.GONE);
greenButton2.setVisibility(View.GONE);
if(previewButton != null) {
previewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int time = 100;
delay(time);
}
});
}
}
public void delay(final int c){
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(c);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Button redButton1 = (Button) findViewById(R.id.red_button_1);
Button redButton2 = (Button) findViewById(R.id.red_button_2);
Button yellowButton1 = (Button) findViewById(R.id.yellow_button_1);
Button yellowButton2 = (Button) findViewById(R.id.yellow_button_2);
Button greenButton1 = (Button) findViewById(R.id.green_button_1);
Button greenButton2 = (Button) findViewById(R.id.green_button_2);
redButton1.setVisibility(View.VISIBLE);
redButton2.setVisibility(View.VISIBLE);
yellowButton1.setVisibility(View.VISIBLE);
yellowButton2.setVisibility(View.VISIBLE);
greenButton1.setVisibility(View.VISIBLE);
greenButton2.setVisibility(View.VISIBLE);
}
}, c);
}
EDIT:
previewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int time = 1000;
redButton1.setVisibility(View.VISIBLE);
delay(time);
redButton2.setVisibility(View.VISIBLE);
delay(time);
yellowButton1.setVisibility(View.VISIBLE);
delay(time);
yellowButton2.setVisibility(View.VISIBLE);
delay(time);
greenButton1.setVisibility(View.VISIBLE);
delay(time);
greenButton2.setVisibility(View.VISIBLE);
}
});
}
public void delay(final int time) {
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
Because that's basically what the postDelayed method does. It runs the code in the runnable after the specified time. So it delays for 100ms then runs just turns all the buttons on at once. Also I'm not sure what you are trying to do by calling Thread.sleep() on your UI thread. I would suggest writing a new method that just runs a Thread.sleep() inside a separate thread and call it between each pair of button toggle commands. Kind of like this
public void pause(int time) {
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
Then basically call this in between each pair of button toggle statements. This should do the trick.
Update UI with Thread sleep
This will help answer the question if anyone visits in the future.

Android dev't using thread with textview: why this is crashing?

My goal is when the user tap start button, letters "o" "n" "o" "m" and so forth will appear at the center of the screen. "o" will appear first then after a few seconds will be replaced by "n" then "o" and so forth.
note: for brevity, i just make the guessword = onomatopoeia, first. In reality, guessword will changes every time i tap the start bottom.
this is the code:
private String guessword = "onomatopoeia";
private TextView showchar;
private int n = guessword.length();
private char letArray[]= guessword.toCharArray();;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
addStartListener();
}
public void addStartListener(){
Button start = (Button) findViewById(R.id.start);
showchar = (TextView) findViewById (R.id.charView);
start.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Thread thread = new Thread()
{
#Override
public void run() {
try {
for(int i = 0 ; i < n ; i++) {
sleep(1000);
showchar.setText(letArray[i]);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
});
}
thanks for the help
I decided to implement runonuithread but still it crashes:
this is the updated version:
private String guessword = "onomatopoeia";
private TextView showchar;
private int n = guessword.length();
private char letArray[]= guessword.toCharArray();
private Handler handler;
private int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
handler = new Handler();
showchar = (TextView) findViewById (R.id.charView);
}
public void startGame(View view){
new Thread() {
public void run() {
while(i++ < n) {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
showchar.setText(letArray[i]);
}
});
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
use this code for setting the text in your textview
runOnUiThread(new Runnable() {
#Override
public void run() {
showchar.setText(letArray[i]);
}
});
You are updating ui from a thread which is not possible.
showchar.setText(letArray[i]);
UI must be updated ui thread.
All you are doing is repeatedly setting value to TextView you can use Handler with a delay for this purpose.
You could use runOnUiThread also but i don't see the need for a thread for what you are doing.
Use a Handler. You can find an example #
Android Thread for a timer

Bitmap wallpaper change every 10 seconds android

Hello I'm trying to create bitmap wallpaper. But this bitmap changes every 10 seconds. How can I accomplish this?
This is what I have tried:
// I have declared
int[] images = {R.drawable.donna, R.drawable.donna1, R.drawable.marian,
R.drawable.marian1, R.drawable.marian};
Handler mHandler = new Handler();
ImageView imgView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgView = (ImageView) findViewById(R.id.imageView1);
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(10000);
mHandler.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
// Write your code here to update the UI.
Random ran = new Random();
imgView.setImageResource(images[ran.nextInt(images.length)]);
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();
But my question is how do I integrate the function/method for setting this as wallpaper?
Any help is truly appreciated. Thanks.
You can use postDelayed() to change your image wthin a specified timeframe:
Handler mHandler = new Handler();
Runnable __runnable = new Runnable()
{
#Override
public void run()
{
Random ran = new Random();
imgView.setImageResource(images[ran.nextInt(images.length)]);
mHandler.postDelayed(this, 10000);
}
};
new Thread(__runnable).start();
For your second question, see the link below:
how to set image as wallpaper from the ImageViev

I am trying to make a simple thread that changes a textview after a time

The problem is that when i click the button for the second my application crashes and i don't understand why. When i clink the button for the second time is suppose to the same all over again.
Any help is appreciated.
public class main extends Activity {
Boolean grabar = false;
TextView texto;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
texto = (TextView) findViewById(R.id.texto);
Button startbtn = (Button) findViewById(R.id.button);
startbtn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
background.start();
}
});
}
Thread background = new Thread (new Runnable() {
public void run() {
try {
Thread.sleep(3000);
cambiarHandler.sendEmptyMessage(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Handler cambiarHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
texto.setText("ok");
}
};
}
The problem I see is that threads are designed to run once. You need to create a new instance of the thread each time you wish to run it.
This may well have something to do with Android's policy of only allowing the UI thread to modify the UI.
You should be using ASyncTask instead if you want to modify the UI from a separate thread.
See here:
Update UI from Thread
I don't know which exception is thrown. I guess it is NPE thrown. When you try to set text to the TextView. Try instantiating the TextView within the handleMessage() method:
Handler cambiarHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
texto = (TextView) findViewById(R.id.texto);
texto.setText("ok");
}
};
And in the onClick method do this to have new instance of the thread each time button is clicked.
public void onClick(View v){
new Thread (new Runnable() {
public void run() {
try {
Thread.sleep(3000);
cambiarHandler.sendEmptyMessage(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}

Categories

Resources