ayman salah
ayman salah

Reputation: 73

Access MenuItem programmatically to change Textcolor

When User click on the item in the menu the onOptionsItemSelected fires passing MenuItem as an argument. this passed MenuItem extends the TextView and not the same that we can get from menu.findItem() method. To access this user needs to click on the item, I want to access this Object without user clicking on the item in the menu.

Upvotes: 2

Views: 309

Answers (2)

dkarmazi
dkarmazi

Reputation: 3259

I can't post comments in the answer above, so I'm writing another answer. A more reliable way to know when the view is actually placed on the screen is observing it's ViewTreeObserver such as:

public class ActivityMain extends Activity {
    // Instance variables
    OnLayoutReadyListener onLayoutReadyListener = new OnLayoutReadyListener();
    View v;

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

        v = findViewById(R.id.mServerTime);
        v.getViewTreeObserver().addOnGlobalLayoutListener(onLayoutReadyListener);
    }

    class OnLayoutReadyListener implements ViewTreeObserver.OnGlobalLayoutListener {
        @Override
        public void onGlobalLayout() {
            // this will be called once view V is placed and measured on the screen
            v.setTextColor(Color.WHITE);
            // remove this listener to prevent any additional callbacks
            v.getViewTreeObserver().removeOnGlobalLayoutListener(onLayoutReadyListener);
        }
    }
}

Upvotes: 0

user93865
user93865

Reputation: 139

in On resume just call findViewById and cast it to TextView. You need to make this in a Timer to postdelay your code until the view is rednered as this is rendered by menu. for me it works like this

  if (!created) { // for first resume only
        created = true;
        Timer t = new Timer();
           t.schedule(new TimerTask() { // Might be done in better way, dont  know yet
            @Override
            public void run() {
                try {
                    while true {
                        final Object o = findViewById(R.id.mServerTime);
                        if (o == null) {
                            try {
                                Thread.sleep(1000);
                            } catch (Exception exp) {
                            }
                        } else {
                            if (o != null && o instanceof TextView) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        ((TextView) o).setTextColor(Color.WHITE);
                                    }
                                });
                            }
                            break;
                        }
                    }
                }catch (Exception exp) {}
            }
        },1000);

Upvotes: 2

Related Questions