View'S Getwidth() and Getheight() Returns 0

View's getWidth() and getHeight() returns 0

You are calling getWidth() too early. The UI has not been sized and laid out on the screen yet.

I doubt you want to be doing what you are doing, anyway -- widgets being animated do not change their clickable areas, and so the button will still respond to clicks in the original orientation regardless of how it has rotated.

That being said, you can use a dimension resource to define the button size, then reference that dimension resource from your layout file and your source code, to avoid this problem.

View's getWidth() and getHeight() returning 0

You need to use the onSizechanged() method.

This is called during layout when the size of this view has changed

getWidth() and getHeight() return 0 value

Use OnLayoutChangeListener:

myView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
myView.removeOnLayoutChangeListener(this);

// get dimensions now
}
});

This is a better approach than setting an arbitrary delay. You will get a callback when the bounds of the View changes due to layout processing. So you will get the proper dimensions inside the onLayoutChange callback. Don't forget the remove the listener after the first callback.

getHeight and getWidth for Custom View returning 0

I'm not sure in your case, but I've used this some time ago. Simply use a ViewObserver. In my case I've used something like this:

final ViewTreeObserver treeObs = dataView.getViewTreeObserver();

treeObs.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// This will be called when the layout is finished, just before displaying
...
}
}

getHeight returns 0 for all Android UI objects

In short, the views are not built yet in onCreate(), onStart(), or onResume(). Since they technically don't exist (as far as the ViewGroup is concerned), their dimensions are 0.

In long, you can go here for a better explanation on how to handle it.

How to retrieve the dimensions of a view?

getWidth() returns 0 if set by android:layout_width=match_parent

A view's size isn't available until after onMeasure(), particularly if its set to wrap_content, or fill_parent.

You should either access the size in or after onMeasure() in the View code, or in a a Layout Tree Observer:

LinearLayout layout = (LinearLayout)findViewById(R.id.mylayout);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();

}
});

You will also need to add android:id="@+id/mylayout" to your LinearLayout for the second one to work.

ImageButton.getWidth() and getHeight() returns 0

Exactly in the same way. You can do it in onCreateView or onViewCreated. The important part is that it has to be after you inflate your layout:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_test, container, false);
final View measuredView = rootView.findViewById(R.id.measured_view);
measuredView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
int width = measuredView.getWidth();
int height = measuredView.getHeight();
if (width > 0 && height > 0) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
measuredView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
measuredView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
//DO SOMETHING WITH NON-ZERO DIMENSIONS
}
}
});
return rootView;
}

getWidth and getHeight are returning a zero

The width and height are not defined until the view is actually rendered to the screen.

Use protected abstract void onLayout (boolean changed, int l, int t, int r, int b) (which you override in your activity) to know when the sizes are ready.



Related Topics



Leave a reply



Submit