Android - Need callback when View is shown - android

Is there a method equivalent to Dialog.setOnShowListener() for the View class in android ?
I need it because when I call getLocationOnScreen on a View (say foo) in the line immediately following someViewGroup.addView(foo), I get location = (0,0).
I cannot use any lifecycle method of the activity, because addition of foo is on run (press of a button). Any help would be highly appreciated. Thanks in advance.

You should use a ViewTreeObserver:
someViewGroup.addView(foo)
ViewTreeObserver vto = someViewGroup.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
///getLocationOnScreen here
ViewTreeObserver obs = someViewGroup.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});

Related

Get the location of an imageview

Hi so I'm trying to get the location of an imageview in android using
xPosition = view.getLeft();
yPosition = view.getTop();
But it keeps returning 0 no matter where the imageview happens to be. The same also occurs when using
view.getLocationOnScreen(int[] location);
Does anybody have any ideas why this might be?
You can use those methods after onCreate has completed. One way to do it is this inside the onCreate method is this:
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//do your thnig here.....
if (Build.VERSION.SDK_INT < 16) {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
else {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
The globalLayoutListener will be notified when the layout is built and run the onGlobalLayout method. Be sure to remove the globalLayoutListener after you do what you want to do.

getHeight in a fragment

im trying to measure a ImageView, after my fragment is displayed, or if possible before.
I heared, that its not yet possible in the onActivityCreated. But somehow it works with the global Layout listener. -But how? -I have a method, wich measures and does some code, i just don't know when to call the method.
Can somebody gice an example?
the beginning of the measure method:
public void skalierung() {
InputStream dots=getResources().openRawResource(R.drawable.amountofdots);
Bitmap dotsBmp = BitmapFactory.decodeStream(dots);
View mainframe=(View)getActivity().findViewById(R.id.mainframe);
int breite=mainframe.getWidth();
Thanks!
To set a GlobalLayoutListener you have to get ViewTreeObserverof your View using the view.getViewTreeObserver() method which :
Returns the ViewTreeObserver for this view's hierarchy. The view tree observer can be used to get notifications when global events, like layout, happen.
After doing that well , you can addOnGlobalLayoutListener on your ViewTreeObserever
OnGlobalLayoutListener : Interface definition for a callback to be invoked when the global layout state or the visibility of views within the view tree changes.
and inside the onGlobalLayoutmethod you can call the getWidth on the your desired view, here's an example :
View mainframe=(View)getActivity().findViewById(R.id.mainframe);
ViewTreeObserver vto = mainframe.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
skalierung(); // here you can call the getWidth and getHeight methods
ViewTreeObserver obs = mainframe.getViewTreeObserver();
// you have to reset the ViewTreeObserver each time to ensure the reuse of the OnGlobalLayoutListener
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
}
});
Hope it helps.

getMeasuredWidth() not working

This code is not working well for my nexus 7.First time when i install(debugge)its give me value
"1286" but after close the app and again i start it then give me width "1542" .
i know this one correct code but it is not worked why it give wrong value???
ViewTreeObserver vto = horizontalLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
horizontalLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
Log.i("WIDTH","Width "+horizontalLayout.getMeasuredWidth()+" width "+horizontalLayout.getWidth());
}
});
any idea why it is happened???

waiting for setContentView() method

I have problem with getting information about views after calling setContentView(). I'm changing layout of activity after click on button. Here is the example of my code.
public void onClickButton1(View v) {
setContentView(R.layout.activity_main);
setScreen();
}
But durring execution setScreen() method, layout still is not rendered, so I cannot call getWidth() on some view intended in layout (respectively I can but I always get 0). How I can wait until setContentView() is finished? Please notice that using of this:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
setScreen();
}
is not sufficient for me. I need to change layout independently on activity cycle.
You need to attach a ViewTreeObserver to your layout:
setContentView(R.layout.yourLayoutName);
View yourLayout = findViewById(R.id.ID_OF_YOUR_LAYOUT);
ViewTreeObserver vto = yourLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//Do your work
vto.removeOnGlobalLayoutListener(this);
}
});
There's no easy way to use setContentView() to get the result you want. You either need to entirely remove all views and then inflate the new layout, use a ViewFlipper to switch between different views, or, which is IMHO the best approach, rework your app to use Fragments

Why view width and height 0 in post() runnable?

I'm getting height and width of a view, inflated in the getView() method. It's a grid item.
Usually I use post() on the view to get the size, after it was attached to the layout. But it returns 0.
final View convertViewFinal = convertView;
convertView.post(new Runnable() {
#Override
public void run() {
doSomethingWithConvertView(convertViewFinal);
}
});
...
doSomethingWithConvertView(View v) {v.getWidth(); /*returns 0*/};
How do I get the size?
While using a viewTreeObserver definitely works, I have found that calling measurements requirements on inflated views can be done reliably using a runnable from the activity's view. i.e.
someActivityInstance.getWindow().getDecorView().post(new Runnable() {
#override
public void run() {
// someMeasurements
}
});
The thing is that convertview most likely is not drawn on the phone screen yet, so you have to wait until it is ready.
You need to use a ViewTreeObserver, to know exactly when the view has been drawn.
Check this answer for more info:
When Can I First Measure a View?
You probably is calling this at onCreate or onStart or onResume, methods which runs before layout measure.
But there is a lot of work arounds, this is one good option:
ViewTreeObserver vto = rootView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
v.getWidth();//already measured...
}
});
Where rootView may be any viewGroup in a higher level than the one you want the width.
But be aware that this listner may run more than once.

Categories

Resources