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?
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)
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With