Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity lifecycle - receiving notification that layout is complete

I have an activity in which I have 3 buttons placed alongside each other. I have used a subclass of Button that will resize the button text to prevent the text from wrapping. I would like the 3 buttons to share the same text size. In order to do this I intend to detect the button with the smallest text size and set the other 2 buttons to that text size.

The problem I have is knowing when the Activity has completed laying out its components so that I can reliably know that the resizing of the text has occurred. From the Android documentation it would appear that the latest notification in the lifecycle is onResume() but it appears that the layout hasn't completed at this point. Is there a way of receiving notification that the Activity layout has finished?

like image 761
kitson Avatar asked Jul 06 '11 11:07

kitson


People also ask

Which method of Android activity lifecycle is triggered when activity is finishing or activity is about to be destroy?

onDestroy() The system invokes this callback either because: the activity is finishing (due to the user completely dismissing the activity or due to finish() being called on the activity), or. the system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode)

Why do we need to call setContentView () in onCreate () of activity class?

As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.

What is the difference between onStop and onDestroy?

Once onStop() is called then onRestart() can be called. onDestroy() is last in the order after onStop(). onDestory() is called just before an activity is destroyed and after that it is gone it is not possible to resurrect this.

What is onSaveInstanceState () and onRestoreInstanceState () in activity?

The onSaveInstanceState() method allows you to add key/value pairs to the outState of the app. Then the onRestoreInstanceState() method will allow you to retrieve the value and set it back to the variable from which it was originally collected.


2 Answers

I've done something similar using Tree Observers. You may have to play around with it a bit to find which view(s) it should be attached too, but this will fire once the view is set. From there, you can get the size of the text, and make whatever changes are needed.

mTestButton.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        // make sure it is not called anymore 
        mTestButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);

        float size = mTestButton.getTextSize();
    }
});


EDIT:
The method #removeGlobalOnLayoutListener has been deprecated, but is still valid. To use both the new and old method, see this stackoverflow answer.

like image 187
Greg Avatar answered Sep 20 '22 00:09

Greg


From what I know there is not an event or something that tells you everything is loaded. What you maybe can do is at the end of the oncreate method start a AsyncTask which runs while the width of your last view is 0. When the layout is loaded, views will have an actually size instead of 0 so you know its loaded. In the callback from the ASyncTask you then can calculate the smallest button and determine the fontsize.

like image 38
matsjoe Avatar answered Sep 22 '22 00:09

matsjoe