I know this is kind of a duplicate but the answers for other question didn't help me. always my ImageView height returns zero. here is what I tried:
dart.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
int finalHeight = dart.getMeasuredHeight();
int finalWidth = dart.getMeasuredWidth();
Toast.makeText(MainActivity.this," "+finalHeight,Toast.LENGTH_SHORT).show();
return true;
}
});
ALSO -
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
height = dart.getHeight();
width = dart.getWidth();
Toast.makeText(MainActivity.this," "+height,Toast.LENGTH_SHORT).show();
}
And I also tried dart.getHeight() inside the onCreate but I understood it can't work. What can I do?
Add addonPreDrawListener to ViewTreeObserver and call dart.getViewTreeObserver().removeOnPreDrawListener(this); in your onPreDraw() method
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
dart.getViewTreeObserver().removeOnPreDrawListener(this);
finalHeight = dart.getMeasuredHeight();
finalWidth = dart.getMeasuredWidth();
Toast.makeText(MainActivity.this," "+finalHeight,Toast.LENGTH_SHORT).show();
return true;
}
});
Related
I have not understood how to return values from the method below, I have tried to return int but it shows me a weird error which is not possible to solve. This is the code:
private void showTheEnemy() {
ViewTreeObserver vto = radarImage.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
radarImage.getViewTreeObserver().removeOnPreDrawListener(this);
yradar_height = radarImage.getMeasuredHeight();
xradar_width = radarImage.getMeasuredWidth();
return true;
}
});
coordinateText.setText(yradar_height + " " + xradar_width);
}
I have understood that values are not being updated in the method because when I use setText() method then int values are 0.
You get zeros because the inner-method is not triggered before you set the text. The values are set at whatever they were when you last initialized them.
Set the text from the callback.
private TextView coordinateText;
private int xradar_width = 0;
private int yradar_height = 0;
private void showTheEnemy() {
ViewTreeObserver vto = radarImage.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
yradar_height = radarImage.getMeasuredHeight();
xradar_width = radarImage.getMeasuredWidth();
coordinateText.setText(String.format("%d %d",
yradar_height,
xradar_width
));
radarImage.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
});
// Your values are still 0 here
}
Or pass to another method.
private TextView coordinateText;
private void showTheEnemy() {
ViewTreeObserver vto = radarImage.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
setCoordinate(coordinateText,
radarImage.getMeasuredHeight(),
radarImage.getMeasuredWidth() ));
radarImage.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
});
}
private static void setCoordinate(TextView tv, int xPos, int yPos) {
tv.setText(String.format("%d %d", xPos, yPos));
}
Set values when you get radarImage width and height:
private void showTheEnemy() {
ViewTreeObserver vto = radarImage.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
radarImage.getViewTreeObserver().removeOnPreDrawListener(this);
yradar_height = radarImage.getMeasuredHeight();
xradar_width = radarImage.getMeasuredWidth();
coordinateText.setText(yradar_height + " " + xradar_width);
return true;
}
});
coordinateText.setText(yradar_height + " " + xradar_width);
}
EDIT:
Create class fields and when new values are provided pass those values where you need them.
I have a segment Android code:
final int[] finalWidth = new int[1];
final ImageView iv = (ImageView)findViewById(R.id.imageView);
textView = (TextView)findViewById(R.id.textView);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
iv.getViewTreeObserver().removeOnPreDrawListener(this);
finalWidth[0] = iv.getMeasuredWidth();
textView.setText(Integer.toString(finalWidth[0]));
return true;
}
});
imageView = (ImageView)findViewById(R.id.imageView);
imageView.getLayoutParam().height = 1.6*finalWidth[0];
but , ImageView is invisiable after the app was built. Help me ! thanks you!
Assuming that gifImageView is your imageView.
gifImageView.post(new Runnable() {
#Override
public void run() {
int __w = gifImageView.getWidth();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(__w, (int)(1.6 * __w)); // Since you can not apply float height
gifImageView.setLayoutParams(params);
gifImageView.invalidate();
}
});
Hope this helps.
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);
}
}
});
My imageView in xml:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ivManagePhoto"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
My code:
ImageView ivManagePhoto;
ivManagePhoto = (ImageView)findViewById(R.id.ivManagePhoto);
double ivw = ivManagePhoto.getWidth();
When i try to get ivManagePhoto.getWidth(); that return 0, but i need to get mathparent size.
You can try this:
imageView.post(new Runnable() {
#Override
public void run() {
imageView.getWidth();
}
});
or, try this:
ViewTreeObserver viewTreeObserver = rootLayout.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
viewWidth = view.getWidth();
}
});
}
Hope this code will help you.
You can wait to onwindowfocuschanged event. At this time UI items are already loaded
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
//get sizes you want
Point screen_size=new Point();
getWindowManager().getDefaultDisplay().getSize(screen_size);
int width = screen_size.x;
int height = screen_size.y;
//...
}
//...
}
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);
}
});