Dynamically getting Height and Width of Layout - android

In my app I'm adding many views inside the Layout having below properties how shall i get the correct height of the layout at run time
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(840,LayoutParams.WRAP_CONTENT));
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(view1);
ll.addView(view2);
ll.getHeight();
how to get Height of layout at that point consider adding of view1 and view2 , irrespective of my property(LayoutParams.WRAP_CONTENT), beacause it always return 0 to me?

Try this way,hope this will help you to solve your problem.
int width;
int height;
ll.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
width = view.getMeasuredWidth();
height = view.getMeasuredHeight();
}
});

You can call
ll.post(new Runnable()
{
public void run()
{
ll.getHeight();
}
});
This will give you ll's height after adding the child views.

Related

How to set view to above of the keyboard?

I want to set my linear layout which contain in Relative layout exactly above the keyboard.
for this I set Linear Layout to alignParentBottom="true" and programmatically set bottom margin is equal to keyboard height, but its not working
multipleMediaRelLyaout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
multipleMediaRelLyaout.getWindowVisibleDisplayFrame(r);
int screenHeight = multipleMediaRelLyaout.getRootView().getHeight();
keyboardH = screenHeight - r.bottom;
final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)edittextLinLayout.getLayoutParams();
params.bottomMargin=keyboardH ;//not working
//params.bottomMargin=800; its working
edittextLinLayout.setLayoutParams(params);
}
});
Use this 'android:windowSoftInputMode="adjustPan" ' in your menifest file. Try the bellow solution
https://stackoverflow.com/a/63816981/3916792

Getting an android view button's width after setting it programatically

During my oncreate I have a button that is drawn. I come back and have it resized as part of the viewTreeObserver.OnGlobalLayoutListener() method. This works great. Within the same onGlobalLayouyListener method how can I come back and retrieve the width and height of the button. If I use btn.getwidth it returns the setting i have in my xml and not the value I just set. How can I retrieve the new width and height of the button.
mainlayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
Cyclebuttons(mainlayout);
getwidth();}
public void Cyclebuttons(ViewGroup parent) {
int btn2height= somenumber;
// set button width and height
ConstraintLayout.LayoutParams newLayoutParams = (ConstraintLayout.LayoutParams) btnx21.getLayoutParams();
newLayoutParams.width = btn2height;
newLayoutParams.height = btn2height;
btnx21.setLayoutParams(newLayoutParams);
}
public Void getwidth{
btnx21.getwidth();
}
thanks.
Didn't run the code, but the following might work:
public Void getwidth{
ConstraintLayout.LayoutParams newLayoutParams = (ConstraintLayout.LayoutParams) btnx21.getLayoutParams();
return newLayoutParams.width;
}

How to determine the final size of view before expand?

I have a LinearLayout containing a TextView. The initial height of LinearLayout is set to 20px where the content of TextView overflows its bounds. I want to animate LinearLayout to expand to wrap content (its Children height). How can I determine the final size of LinearLayout before expanding?
Currently I set Height of View to wrap_content for a moment and after measuring the final height reset the height to 20 again! obviously this cause an unwanted blink before animation and should be replaced by a better solution.
//I hate these two lines
view.getLayoutParams().height=ViewGroup.LayoutParams.WRAP_CONTENT;
view.requestLayout();
//End of hate!
view.post(new Runnable() {
#Override
public void run() {
int targetHeight = view.getMeasuredHeight();
view.getLayoutParams().height=20;
view.requestLayout();
}
});
There is a .measure() method for this:
view.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
int targetHeight = view.getMeasuredHeight();

Android: get width of Layout programatically having fill_parent in its xml

I have the a LinearLayout with width set in xml as fill_parent , now i need its width at runtime programatically. So i did this:
LinearLayout layoutGet=(LinearLayout) findViewById(R.id.GameField1);
LayoutParams layParamsGet= layoutGet.getLayoutParams();
int width=layParamsGet.width;
But width value on debugging was found to be -1, anybody with idea why can't i get the exact width at runtime for LinearLayout with fill_parent set in layout xml.
I got a simple approach working.
myLayout = (RelativeLayout) findViewById(R.id.my_layout);
myLayout.post(new Runnable()
{
#Override
public void run()
{
Log.i("TEST", "Layout width : "+ myLayout.getWidth());
}
});
Jens Vossnack's approach mentioned below works fine. However, I found that the onGlobalLayout() method of GlobalLayoutListener is called repeatedly, which may not be appropriate in certain cases.
You can try to listen to the globalLayout event, and get the width in there. You probably get the -1 because you are trying to get the width before the views are layed-out.
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//Do it here
LinearLayout layoutGet=(LinearLayout) findViewById(R.id.GameField1);
LayoutParams layParamsGet= layoutGet.getLayoutParams();
int width=layParamsGet.width;
removeOnGlobalLayoutListener(layoutGet, this); // Assuming layoutGet is the View which you got the ViewTreeObserver from
}
});
#SuppressLint("NewApi")
public static void removeOnGlobalLayoutListener(View v, ViewTreeObserver.OnGlobalLayoutListener listener){
if (Build.VERSION.SDK_INT < 16) v.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
else v.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
}
(vto is the view you want to get the width of)
If you look at the value for FILL_PARENT (you should be using MATCH_PARENT since FILL_PARENT is now deprecated) you will notice the value for it is -1. LayoutParams are simply attributes for how your view should look. Once the view is inflated and looks the way the params specify, the view does not go back and change those Params to reflect the actual values of the view (width/height/etc).
If you wanted to get the actual width of your view you would have to call getWidth() on your view once the layout has been inflated and displayed. Calling getWidth() before your layout has been displayed will result in 0.
LinearLayout layoutGet=(LinearLayout) findViewById(R.id.GameField1);
int width = layoutGet.getWidth();
First of all, you should use match_parent instead of fill_parent (deprecated).
When you do int width=layParamsGet.width;, you take the right result which is -1 corresponding at fill_parent.
If you wan't to get the real width, you need to wait onMesure() call and use
LinearLayout layoutGet=(LinearLayout) findViewById(R.id.GameField1);
int width = layoutGet. getMeasuredHeight();
layoutGet.getWidth() should work out

Detect if some part of the view is NOT visible?

Is it possible to detect if some part of the view is not visible on the screen?
This is used in a situation that view's width/height is bigger that its parent's width/height.
EDIT
I get that the height of a view is 0. Does anyone knows why? I fetch the height in onCreate.
LinearLayout lin = (LinearLayout) findViewById(R.id.linear_layout);
final int layoutHeight = lin.getHeight();
Toast.makeText(this,"LinLay height: "+layoutHeight,Toast.LENGTH_SHORT).show();
...
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int displayTextWidth = textView.getWidth();
if (displayTextWidth <= layoutHeight) {
textView.setTextSize(textView.getTextSize() + 1);
}
}
});
You could get the view's width and height by calling View.getWidth() and View.getHeight(), then get the device dimensions by these means: How do I get a device's maximal width and height in android
Then compare the two, and if the view's bounds are larger than your device's bounds, then some parts of the view are not visible.
In response to comments:
textView.post( new Runnable() {
#Override
public void run() {
int displayTextWidth = textView.getWidth();
// Code that uses width here...
}
});

Categories

Resources