I am starting to use ButterKnife and I got it is working inside fragment with views that are inflated.
But I have a question, is it possible to get views which aren't inflated?
For example, I have a toolbar inside of my MainActivity, but not inside the fragment. Can I access to this toolbar using ButterKnife?.
And other question. I tried to get the toolbar height using:
Toolbar toolbar = getActivity().findViewById(R.id.toolbar);
int toolbarHeight = toolbar.getHeight();
But this return always 0. How can I get the toolbar size from the view directly? Now I am using:
getActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, typedValueToolbarHeight, true);
But really I would like to get the toolbar from the view.
Initialize the view and call this method in on create method
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
//now we can retrieve the width and height
int width = view.getWidth();
int height = view.getHeight();
//... //do whatever you want with them //... //this is an important step not to keep receiving callbacks: //we should remove this listener //I use the function to remove it based on the api level!
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
else
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} });