1

How do you implement a custom view in Android so that it properly supports, or works with, the messaging queue ?

I'm trying to emulate the behavior of the built-in views so that I can properly / normally update a custom view with data within onCreate.

Currently, my custom view has ad-hoc set/update functions to put data in them. The problem with this is that my view's children views are not initialized until the first time onMeasure is called, which is after onCreate exits (which I think is how the built-ins do it).

Therefore, I want to know what the general strategy is (ie, what methods to override) to update a custom view from onCreate in such a way that the updates go into the message queue and reach the view after they are properly instantiated (just like the built-ins) ?

Thanks.

samus
  • 6,102
  • 6
  • 31
  • 69

1 Answers1

1

Look at View.post():

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView hello = ((TextView) findViewById(R.id.hello));
    hello.post(new Runnable() {
        @Override
        public void run() {
            hello.setText("Hello World!");
        }
    });
}
pawelzieba
  • 16,082
  • 3
  • 46
  • 72
  • Nice! Does onMeasure always fire before the posted runnable (when onCreate or onResume exits) ? – samus Oct 18 '12 at 16:26