I'm trying to understand why it's occuring me the following problem.
I have an ImageView and i set an image to it, then i setup a onClickListener to it, so when you click to image it change the image (to a new image) by image01.setImageResource(R.drawable.newImage). After that i call a method where i check a condition, if it is true i change the image again to the default one.
But I can't see the change because it change immediately. I also insert a sleep to make it slower.
(By default in the xml code i setted the image to oldImage)
Ok... maybe it's not clear.. so let's see the CODE:
private void myMethod(){
ImageView image01 = (ImageView) findViewById(R.id.image01);
image01.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//When you click on image it change!
image01.setImageResource(R.drawable.newImage);
checkImg(image01);
}
});
}
private void checkGame(ImageView img){
try{
Thread.sleep(1000);
if(condition)
img.setImageResource(R.drawable.oldImage);
}catch (Exception e) {
e.printStackTrace();
}
}
I saw immediately the oldImage. What's the problem?
Is it possible that the view change are not applied in myMethod() until all methods inside it will terminate?
Thanks in advance
Using Thread.sleep() method, you are actually making wait to main UI thread. The main UI thread's methods are not synchronized. Be aware of that.
Please go through the developers.android site for using of threads painlessly... before seeing the your useful code.
private void myMethod(){
ImageView image01 = (ImageView) findViewById(R.id.image01);
image01.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//When you click on image it change!
image01.setImageResource(R.drawable.newImage);
checkGame(image01);
}
});
}
private void checkGame(ImageView img){
try{
// Thread.sleep(1000);
if(condition)
image01.postDelayed(new Runnable() {
#Override
public void run() {
image01.setImageResource(R.drawable.oldImage);
}
}, 2000);
}catch (Exception e) {
e.printStackTrace();
}
}
Related
This is the code to test the thread and how it works! (It's not really connecting just to look like one) I wanted to add a progress bar when the alert dialog is gone by pressing the yes button, but it doesn't work. I set the Visibility to gone before, and visible to after. What seems to be the problem? I done casting right, and there's nothing wrong elsewhere. The text doesn't change also..... please help :)
private void connectRequest() {
AlertDialog.Builder builder = new AlertDialog.Builder(ThreadActivity2.this);
AlertDialog dialog = builder.setMessage("Do you request Remote Access?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
accessTv.setText("Requesting Remote Access ...");
accessPgb.setVisibility(View.VISIBLE);
try {
Thread.sleep(5700);
} catch (InterruptedException e) {
e.printStackTrace();
}
accessTv.setText("Server Access Successful!");
accessPgb.setVisibility(View.GONE);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
Try using java.util.concurrent.TimeUnit:
TimeUnit.SECONDS.sleep(1);
To sleep for one second or
TimeUnit.MINUTES.sleep(1);
I am doing an app that creates an ethereum wallet and send some ether when you touch the button register, it takes about 1 minute to do this, while it happens I want to show a message saying: Creating a wallet, wait please.
When I show the message it won't create the wallet or it will create the wallet but it won't show the message.
PS: If someone knows how to down the time to do this, it will help me a lot.
Thanks
thanks for the help... and the negative.
I have solved it anyway, I leave my solution below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ActivityMain);
errorMsg = new AlertDialog.Builder(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyAsyncTasks().execute();
}
});
}
class MyAsyncTasks extends AsyncTask<Void, Void, String> {
// ProgressDialog dialog;
#Override
//It will show a message during your background
protected void onPreExecute() {
dialog = new ProgressDialog(mainActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setTitle("Add title");
dialog.setMessage("Add message");
dialog.setIndeterminate(true);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
#Override
protected void onPostExecute() {
dialog.dismiss(); // Close the Dialog
//Show a window with error or operation succesfully
if(hash.equals("Error")){
displayError("Error", "Error");
dialog.cancel();
}
else{
displayConfirmation("Operation Succesfully");
dialog.cancel();
}
}
#Override
protected String doInBackground(Void... voids) {
dialog.show();
// your code to do you background activity
}
}
Is there a way for a button to be executed in seconds without touching it.
The following code execute the button in one second but after i touch the button. My question is can this be achieved without touching the button?
View.OnClickListener mButtonStartListener = new View.OnClickListener() {
public void onClick(View v) {
try {
mHandler.removeCallbacks(hMyTimeTask);
// Parameters
// r The Runnable that will be executed.
// delayMillis The delay (in milliseconds) until the Runnable will be executed.
mHandler.postDelayed(hMyTimeTask, 1000); // delay 1 second
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private Runnable hMyTimeTask = new Runnable() {
public void run() {
nCounter++;
hTextView.setText("Hallo from thread counter: " + nCounter);
}
};
/**
*
*/
View.OnClickListener mButtonStopListener = new View.OnClickListener() {
public void onClick(View v) {
mHandler.removeCallbacks(hMyTimeTask);
}
};
You may simulate click event by following code
View.performClick();
originally posted at https://stackoverflow.com/a/4877526/5329101.
Try .onLoadCompleteListener instead of .onClickListener
I'm trying to first create an activity, then after 2 seconds, I want to hide the action bar and then after another 2 secs, I want to start another activity but this code - getActionBar().hide() isn't working. Here is my code -
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
//final ActionBar actionbar = getActionBar();
Thread timer1 = new Thread() {
public void run(){
try{
sleep(2000);
} catch(InterruptedException e){
e.printStackTrace();
} finally{
getActionBar().hide();
}
}
};
timer1.start();
Thread timer2 = new Thread() {
public void run(){
try{
sleep(2000);
} catch(InterruptedException e){
e.printStackTrace();
} finally{
Intent intent = new Intent("app.lost_and_found_0.STARTINGPOINT");
startActivity(intent);
}
}
};
timer2.start();
}
Is there something wrong I'm doing wrong?
My application theme is set to Theme.Holo.Light in manifest file.
Use getSupportActionBar() instead of getActionBar(). As on the class, you need to extend ActionBarActivity.
I want to disable a button for a specific time in JavaFX application. Is there any option to do this? If not, is there any work around for this?
Below is my code in application. I tried Thread.sleep, but i know this is not the good way to stop the user from clicking on next button.
nextButton.setDisable(true);
final Timeline animation = new Timeline(
new KeyFrame(Duration.seconds(delayTime),
new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
nextButton.setDisable(false);
}
}));
animation.setCycleCount(1);
animation.play();
You could use the simple approach of a thread that provides the relevant GUI calls (through runLater() of course):
new Thread() {
public void run() {
Platform.runLater(new Runnable() {
public void run() {
myButton.setDisable(true);
}
}
try {
Thread.sleep(5000); //5 seconds, obviously replace with your chosen time
}
catch(InterruptedException ex) {
}
Platform.runLater(new Runnable() {
public void run() {
myButton.setDisable(false);
}
}
}
}.start();
It's perhaps not the neatest way of achieving it, but works safely.
You could also be using the Timeline:
final Button myButton = new Button("Wait for " + delayTime + " seconds.");
myButton.setDisable(true);
final Timeline animation = new Timeline(
new KeyFrame(Duration.seconds(delayTime),
new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent actionEvent) {
myButton.setDisable(false);
}
}));
animation.setCycleCount(1);
animation.play();
The method to disable a JavaFX control is:
myButton.setDisable(true);
You can implement the time logic programmatically in any way you wish, either by polling a timer or by having this method invoked in response to some event.
If you have created this button instance through FXML in SceneBuilder, then you should assign the button an fx:id so that its reference is automatically injected into your controller object during the loading of the scene graph. This will make it easier for you to work with in your controller code.
If you have created this button programmatically, then you'll already have its reference available in your code.
Or you could use a Service and bind the running property to the disableProperty of the button do you want to disable.
public void start(Stage stage) throws Exception {
VBox vbox = new VBox(10.0);
vbox.setAlignment(Pos.CENTER);
final Button button = new Button("Your Button Name");
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Service<Void> service = new Service<Void>() {
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
protected Void call() throws Exception {
Thread.sleep(5000);//Waiting time
return null;
}
};
}
};
button.disableProperty().bind(service.runningProperty());
service.start();
}
});
vbox.getChildren().addAll(button);
Scene scene = new Scene(vbox, 300, 300);
stage.setScene(scene);
stage.show();
}
But the Timeline solution given by Uluk Biy, looks more elegant.