I want to get height of linear layout that is set to wrap content. but it is zero.
i try this codes:
final LinearLayout below= (LinearLayout) findViewById(R.id.below);
ViewTreeObserver vto = below.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
width = below.getMeasuredWidth();
height = below.getMeasuredHeight();
}
});
and :
LinearLayout below= (LinearLayout) findViewById(R.id.below);
final int belowh=below.getHeight();
using code:
final LinearLayout below= (LinearLayout) findViewById(R.id.below);
ViewTreeObserver vto = below.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if(below.getMeasuredHeight()> 0){
this.below.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = below.getMeasuredWidth();
int height = below.getMeasuredHeight();
}
}
})
;
click here to see
Try this code for getting layout width and height after your view is created
final LinearLayout below= (LinearLayout) findViewById(R.id.below);
below.post(new Runnable()
{
#Override
public void run()
{
Log.i("TAG", "Layout width :"+ below.getWidth());
Log.i("TAG", "Layout height :"+ below.getHeight());
}
});
Hope this help
Try this
final LinearLayout below= (LinearLayout) findViewById(R.id.below);
ViewTreeObserver vto = below.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if(below.getMeasuredHeight()> 0){
this.below.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = below.getMeasuredWidth();
int height = below.getMeasuredHeight();
}
}
})
;
#coder android
If you still stack on this.... please see this. I think this will work perfectly.
The following code is found How to retrieve the dimensions of a view?
final TextView tv = (TextView)findViewById(R.id.image_test);
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
LayerDrawable ld = (LayerDrawable)tv.getBackground();
ld.setLayerInset(1, 0, tv.getHeight() / 2, 0, 0);
ViewTreeObserver obs = tv.getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
}
});
Related
fixed : 2018-02-09 , 13:00 (GMT+9)
I change word isDrawing to !isDrawn
I want to use get width, height after view drawn
but I want to know isDrawn too
below code can earn view drawn status
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// after view drawn callback
}
});
actually I want to mechanism like below.
if(!isDrawn){
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
delete_this_listener()
// after view drawn callback
mustCallAfterDrawn()
}
});
} else {
mustCallAfterDrawn()
}
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);
}
}
});
Here is my Code to return Height of view:
int height = 0;
public int getViewHeight(final View v) {
v.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#SuppressLint("NewApi")
#SuppressWarnings("deprecation")
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
v.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
} else {
v.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
height = v.getMeasuredHeight();
//
Log.v(" height", v.getMeasuredHeight() + "");
}
});
Log.v(" return height", v.getMeasuredHeight() + "");
return height;
}
First It return 0. then the height value changes.
Can anyone let me know how to return height of view when it measured in onGlobalLayout. Return statement not working in overrided method.
OR
Is there any other way to make a static method which return height of view by passing View instance.
***try this way*
i get View Height and Width as per View**
public class Test extends Activity {
private RelativeLayout layout;
private int temp;
ViewTreeObserver vto;
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
sharedPreferences = getSharedPreferences("savePref",
Context.MODE_PRIVATE);
layout = (RelativeLayout) findViewById(R.id.layout);
temp = set(layout);
Log.e("temp", "" + sharedPreferences.getInt("key", 0));
}
private int set(final RelativeLayout layout) {
vto = layout.getViewTreeObserver();
final Editor editor = sharedPreferences.edit();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#SuppressWarnings("deprecation")
#Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
temp = layout.getMeasuredHeight();
editor.putInt("key", temp);
editor.commit();
}
});
if (sharedPreferences.contains("key")) {
Log.e("contains", "" + sharedPreferences.getInt("key", 0));
}
return sharedPreferences.getInt("key", 0);
}}
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);
}
});