I have two buttons: sell and buy.
1.When I click on sell I am calling setOnClickistener on the sell button. Inside I am creating a thread(I need it for a specific reason) and inside the thread I am using this code:
runOnUiThread(new Runnable() {
@Override
public void run() {
Button buyButton = findViewById(R.id.buy_button);
buyButton.setEnabled(true);
Button sellButton = findViewById(R.id.sell_button);
sellButton.setBackgroundDrawable(buyButton.getBackground());
sellButton.setText(getResources().getString(R.string.stop_selling));
sellButton.setTextColor(getResources().getColor(android.R.color.white));
}
});
2.I click again on sell Button in order to come back to normal. So I again setOnClickistener on the sell button. Inside I am creating again a thread and inside the thread I am using this code:
runOnUiThread(new Runnable() {
@Override
public void run() {
Button buyButton = findViewById(R.id.buy_button);
buyButton.setEnabled(true);
Button sellButton = findViewById(R.id.sell_button);
sellButton.setBackgroundDrawable(buyButton.getBackground());
sellButton.setText(getResources().getString (R.string.sell));
sellButton.setTextColor(resTextColorToChange);
}
});
Then I click on the buy button. I am calling setOnClickistener on the buy button. Inside I am creating a thread(I need it for a specific reason) and inside the thread I am using this code:
runOnUiThread(new Runnable() {
@Override
public void run() {
Button sellButton = findViewById(R.id.sell_button);
Button buyButton = findViewById(R.id.buy_button);
sellButton.setEnabled(false);
buyButton.setEnabled(false);
}
});
I expect the button sell and buy to become not enabled...The button buy is not enabled...but the button sell remains enabled! Do you have an idea why??
Pls refer to the documentation of
runOnUiThread
and you will see that if the current thread is not an UI thread, then the action is not executed immediately. Rather, its posted to the event queue.Now, this is true is your case. Therefore, it could be the case that either of your
Runnable
can get executed and in no particular order.What you could do is log the executions of Runnable, since you have many, and ensure that none of the unwanted ones are getting executed and not in unwanted order.