Android: How to get a view's height - android

For example, this is mButton:
<Button
android:id="#+id/mbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="mButton" />
This is how I tried to get the height:
int height = mButton.getLayoutParams.height;
But when I logged it, it says the height is -2. I think this might be the int value of "wrap_content". So how can I get the actual height? Thx!

If you want to get the width or height of a view in activty
you can get in this method..
yourview.getHeight();
returns zero(0) after initialization of the button because after adding it to the window only it has a width..in the below method you can height and width of a view..
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
yourview.getHeight();
yourview.getWidth();
}

After layout has happened call View.getHeight():
public final int getHeight()
Return the height of your view.
Returns
The height of your view, in pixels.
As you guessed, layoutParams.height is just the value of wrap_content in your case, which is -2. You could set layoutParams.height to a desired height, but even then it's not necessarily the height that the view will actually end up with after all layout is done.

override the method onWindowFocusChanged(boolean hasFocus);
inside write your code,
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mbtn.getHeight();
mbtn.getWidth();
}
it will give the correct result of the view dimensions.

Related

Get weight in android

I am new Android development, and I have a question about get width of View in Android.
I set weight of view = 1
<TextView
android:id="#+id/tvSTT"
android:layout_width="0dp"
android:layout_weight="1dp"
android:layout_height="match_parent"/>
And I try to find way to get width of this in java code. How should I do?
Sorry about my English.
Your TextView will get a width at the moment it is drawn. Before that moment, it is not clear what its actual width will be (because of many different screen sizes).
You can add an OnGlobalLayoutListener to the viewTreeObserver to be notified when it is drawn.
You can perform getWidth() in the listener and it should not be 0 anymore.
Add an OnGlobalLayoutListener like so
final TextView tvSTTtextView = (TextView)findViewById(R.id.tvSTT);
textView.getViewTreeObserver().addOnGlobalFocusChangeListener(new ViewTreeObserver.OnGlobalFocusChangeListener() {
#Override
public void onGlobalFocusChanged(View view, View view2) {
int width = tvSTTtextView.getWidth(); //width should not be 0
}
});
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
//Here you can get the width! use `getWidth()`
}
You have to get the view's layout params and cast it to TableLayout.LayoutParams, one you have it you can access to the view weight
((TableLayout.LayoutParams) view.getLayoutParams()).weight;// this is the weight

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

Dimension of my Scaled ImageView

How can I determine the dimensions of my Scaled ImageView? I tried using getWidth() & getHeight it always returns 0. My xml layout for the ImageView is as follows:
<ImageView
android:id="#+id/imgView"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/my_image" />
Please suggest something.
You are probably calling getWidth() method too early. The width and height of your ImageView will not be available until the View has been rendered on the screen. Try getting them within onWindowFocusChanged:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int width = iv.getWidth();
int height = iv.getHeight();
// ...
}
You won't get those dimensions in onCreate. The view isn't measured yet and the hierarchy is not set. Those will work but you need to use them somewhere after the app has drawn the screen or the alternative is to set a ViewTreeObserver to notify you when the layout is set.ViewTreeObserver
Here is how its done:
call getVieeTreeObserver() to get a reference to the viewTreeObserver in your activity.
then add a onGlobalLayoutListener:
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
ViewTreeObserver obs = MyActivity.this.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
Inside this onGlobalLayout method you can use your view and get its width and height and do whatever with them.

How to get a layout's dimension or more like the height and witdth in pixels?

I want to get a layout's width in pixels.
I tried this:
LinearLayout galleryContainer = (LinearLayout)findViewById(R.id.galleryContainerLayout);
int galleryWidth= galleryContainer.getLayoutParams().width;
if i toast out galleryWidth, it is says "-1".
I also tried
galleryContainer.getWidth()
but it is gives 0 at the first time, then 480 (the actual size) at second time, so it is like i got the pixels BEFORE my layout infiltrate its size.
What the hell?
How can i really get a layouts width in pixels ? Or some exact values like dip?
Use measure() before call getWidth()
galleryContainer.measure(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
The layout is inflated but not yet drawn.In order to get width or height, you will have to use the below method.
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
System.out.println("...Width..."+galleryContainer.getWidth());
}
Not sure for you but we can code something like this too, check out if it helps.
Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Have fun.

Getting width during build of an Activity

I have a problem, and I can't seem to figure out what to do about it.
I'm new to developing with Android, but I have experience with Java.
During the onCreate(bundle), onPostCreate(bundle) or any other similar method, I can't get the correct width of a View. It returns 0. I also made a simple button for debug purposses, and that button returns the correct value.
How do I call a method that gets the width of a View after the onCreate method?
If that's not possible, what is a workaround?
Thanks in advance,
Gerralt
The problem is because, you are trying to find out the width as soon as you you start your activity.
But at this position the actual transformation of your view wouldn't have occurred. Android provides a special callback to find the width and height of all individual views. To access it, you have to override the onWindowFoucsChanged() method. Here is a sample,
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
System.out.println("...Height..."+textview.getMeasuredWidth());
}
This line textview.getMeasuredWidth() helps you to find the actual width of the textView at runtime. Similarly you can find the width and height for any view using this method.
As i think, you are in the onCreate(Bundle) method:
public void onCreate(Bundle xxx) {
super(xxx);
// init some stuff here
setContentView(R.layout.my_layout);
// from THIS point you can get sizes of Views
}
Sorry if i'm wrong, but on my project it works. OK, i have to say i use often
custom views and use sizes not in my activity code but in View code.
Do this is onWindowFocusChanged() like this:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus && llfullscreen!=null){
int layoutHeight = llfullscreen.getHeight();
ViewGroup.LayoutParams params = lv.getLayoutParams();
params.height = lv/2; //Set to half the screens size
this.lv.setLayoutParams(params);
this.lv.invalidate();
}
}

Categories

Resources