Measure view in fragment - android

I need to know ImageView width and height. Is there a way to measure it in fragment ? In standard Activity I use this :
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
image.getWidth();
image.getHeight();
}
But where can I use image.getWidth(); and image.getHeight(); in fragment ?

Use the GlobalLayoutListener. You assign the listener to your ImageView in onCreateView() just like you do in that answer in the link. It also is more reliable than onWindowFocusChanged() for the main Activity so I recommend switching strategies.
EDIT
Example:
final View testView = findViewById(R.id.view_id);
ViewTreeObserver vto = testView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.d("TEST", "Height = " + testView.getHeight() + " Width = " + testView.getWidth());
ViewTreeObserver obs = testView.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});

Related

How to get Width/Height of Button in Fragment

How can i Get Height/Width of Any View (Button , TextView , RelativeLayout) in fragment, i try something like that
public static class FirstDemoFragment extends Fragment {
int width,height;
private Button button;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
button = (Button) view.findViewById(R.id.fragment_demo_button);
width = button.getWidth();
height = button.getHeight();
System.out.println("==== View Width : " + width);
System.out.println("==== View height : " + height);
width = button.getMeasuredWidth();
height = button.getMeasuredHeight();
System.out.println("==== View Width : " + width);
System.out.println("==== View height : " + height);
return view;
}
my layout.xml file is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/does_really_work_with_fragments"
android:id="#+id/fragment_demo_button"
android:layout_centerInParent="true" />
i have a little research on that, I have found Width/Height of Button in Activity using this method.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
relMain = (RelativeLayout) findViewById(R.id.layoutMain);
width = relMain.getMeasuredWidth();
height = relMain.getMeasuredHeight();
width = relMain.getWidth();
height = relMain.getHeight();
//System.out.println("=== Width : " + width);
//System.out.println("=== height : " + height);
}
but onWindowFocusChanged() is not available in android Fragment ?
In Fragment try below code in Fragment's onCreateView method:
button = (Button) view.findViewById(R.id.fragment_demo_button);
ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
button.getViewTreeObserver().removeOnPreDrawListener(this);
Log.d("", "Height: " + button.getMeasuredHeight()+ " Width: " + button.getMeasuredWidth());
return true;
}
});
In Fragment use below code in Fragment's onCreateView method
ViewTreeObserver mviewtreeobs = button.getViewTreeObserver();
mviewtreeobs .addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
width = button.getWidth();
height = button.getHeight();
}
});
You could add a layout listener to your button like so:
button.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int width = button.getWidth();
if(width > 0){
int height = button.getHeight();
// do something with your width and height
....
// remove layout listener
button.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}

getMeasuredHeight not gives exact height that should be when view is expended

I am trying to get the view's height that would be after expending it. I am using below View.MeasureSpec to measure.
public static void expand(View summary) {
//set Visible
summary.setVisibility(View.VISIBLE);
summary.measure(
View.MeasureSpec.makeMeasureSpec(((View)summary.getParent()).getMeasuredWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
ValueAnimator mAnimator = slideAnimator(0, summary.getMeasuredHeight(), summary);
mAnimator.start();
}
Folling this Android getMeasuredHeight returns wrong values ! and http://developer.android.com/reference/android/view/View.MeasureSpec.html
If I get the correct height the layout will be like below
But right now I am getting like below
Please suggest me some solution.
Try using the ViewTreeObserver to notify you when the View has finished measurments and layouting; it should be able to tell you it's exact size then.
Sample code:
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int bodyOffset = 0;
if (textPart != null) {
bodyOffset = textPart.getTop();
}
Log.d(Constants.LOGTAG, "body bottom = " + (body.getBottom() + bodyOffset));
Log.d(Constants.LOGTAG, "ok button top = " + okButton.getTop());
if (body.getBottom() + bodyOffset > okButton.getTop()) {
body.setVisibility(View.GONE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});

getHeight() of PreferenceFragment

I have multiple FrameLayout inside a ScrollView. In one of the FrameLayout I'm loading a PreferenceFragment. Now I need to set the height of the FrameLayout to the height of the PreferenceFragment to avoid nested scrolling (the PrefrenceFragment is scrollable too).
How can I get the height of the PreferenceFragment?
I tried to override onCreateView() in the PreferenceFragment class, and call View.getHeight(), but its just returning 0.
Setting the FrameLayout to wrap_content, isn't working as well, its just taking the space of a single preference then...
edit:
I tried now to get the height by using addOnGlobalLayoutListener() but the returned value is just the displayed height, not the full height of the PreferenceFragment. So I'm just getting the height of one preference for wrap_content or e.g. 300 if I set the height of the FrameLayout to 300 in the xml. I need the full height of all preferences (visibile and not).
#Override
public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
{
final View v = super.onCreateView(paramLayoutInflater, paramViewGroup, paramBundle);
if (v != null) {
ViewTreeObserver vto = v.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.d("TEST", "Height = " + v.getHeight() + " Width = " + v.getWidth());
ViewTreeObserver obs = v.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
}
return v;
}
You should wait for WindowFocusChange in your activity:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus)
//LAYOUT FRAGMENT NOW
}
try this
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
height = parentLayout.getheight();// the root layout of the fragment
//TODO:
}
}, 200);

Android - Is there a callback function when a View is all set?

I have a custom View, in which I need to perform treatment that require that the size of the view is set (so onCreate doesn't works), but before displaying anything. So I wondered if there is a callback function tat I could override for that ?
Right now I'm using onLayout, but it seems to be called twice, so I'm assuming it's probably not the right use for this. I also tried doing it in onMeasure but it seems that getWidth() and getHeight still returns 0 in it.
Thkanks,
Robin.
Try to use OnGlobalLayoutListener inside onResume()
#Override
public void onResume(){
super.onResume();
ViewTreeObserver viewTreeObserver = yourView.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
viewWidth = yourView.getWidth();
viewHeight = yourView.getHeight();
yourView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
System.out.println(viewHeight + " on resume x" + yourView.getX());
}
});
}
}
You can use the callback OnSizeChanged.
use onSizeChanged,
#Override
protected void onSizeChanged(int width, int height, int oldwidth, int oldheight) {
super.onSizeChanged(width, height, oldwidth, oldheight);
// your code here.
}
Try those methods
1.
ViewTreeObserver vto2 = imageView.getViewTreeObserver();
vto2.addOnGlobalLayoutListener(new
OnGlobalLayoutListener() {
#Override
public void onGlobalLayout () {
imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int height = imageView.getHeight();
int width = imageView.getWidth();
textView.append("\n\n" + imageView.getHeight() + "," + imageView.getWidth());
}
}
);
2.
imageView.post(new Runnable() {
#Override
public void run () {
int h = imageView.getHeight();
int w = imageView.getWidth();
Log.i(TAG, "Height=" + h);
}
};

Wrong View Height/Width value on Orientation change?

I implemented onConfigurationChnage(..) to read value of view height and width on orientation configuration chnage
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Log.i(TAG, "onConfigurationChanged: "+newConfig.orientation+", "+height+", "+width + ", "+mTempeFrameLayout.getWidth());
}
but it always reply old value, if orientation get change it gave last orientation (probably)value.
Screen width/height using displaymetrics is coming right properly but same is not working for any view.
Any one have idea..how can get correct value on orientation Change?
getWidth() returns the width as the View is laid out, meaning you need to wait until it is drawn to the screen. onConfigurationChanged is called before redrawing the view for the new configuration and so I don't think you'll be able to get your new width until later.
#Override
public void onConfigurationChanged(Configuration newConfiguration) {
super.onConfigurationChanged(newConfiguration);
final View view = findViewById(R.id.scrollview);
ViewTreeObserver observer = view.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.v(TAG,
String.format("new width=%d; new height=%d", view.getWidth(),
view.getHeight()));
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
I had to use
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
configHome();
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
}
because the above answer was not working for me

Categories

Resources