android studio: change caracteristics of buttons in runOnUiThread block these buttons

127 Views Asked by At

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??

2

There are 2 best solutions below

2
On

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.

0
On

To be actually more precise on the question, I tested another code similar to it, here it is:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

int a = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.sell_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(a==0) {
                findViewById(R.id.buy_button).setEnabled(false);
                Button sellButton = findViewById(R.id.sell_button);
                sellButton.setBackgroundColor(getResources().getColor(android.R.color.holo_red_dark));
                sellButton.setText("STOP SELLING");
                sellButton.setTextColor(getResources().getColor(android.R.color.white));
                a=1;
            }
            else{
                Button buyButton = findViewById(R.id.buy_button);
                buyButton.setEnabled(true);
                Button sellButton = findViewById(R.id.sell_button);
                sellButton.setBackgroundDrawable(buyButton.getBackground());
                sellButton.setText("SELL DATA");
                sellButton.setTextColor(buyButton.getTextColors().getDefaultColor());
                a=0;
            }
        }
    });

    findViewById(R.id.buy_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Button sellButton =  findViewById(R.id.sell_button);
            Button buyButton =  findViewById(R.id.buy_button);
            sellButton.setEnabled(false);
            buyButton.setEnabled(false);
        }
    });
}

}

Here there are no runOnThreadUi() but again if I click on Sell, then click again on Sell and then click on buy: The button buy is not enabled but the button sell is still enabled which sounds not logic!