33

How can I know the visible size of my activity?

I'm trying to get the Activity real size,
not the height and width from getHeight() and getWidth(),
which gives me the screen full size.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
Tsimmi
  • 1,828
  • 6
  • 23
  • 35
  • 1
    Why do you want to do that? In the most cases this is not necessary and the most things you could need it for can be done with better techniques that come with android to handle different screen sizes. – Janusz Jun 17 '10 at 10:18
  • 3
    I really need to know how many pixels the activity has in its width and height. It has to do with cartographic precision. – Tsimmi Jun 17 '10 at 10:23
  • Are you using a mapview or a custom view for displaying the map? – Janusz Jun 18 '10 at 10:51
  • 1
    Yes, I am creating a map view from scratch for proprietary cartographic info. I've found that it's easier to implement it myself than to use code from others (at least for me :-)). – Tsimmi Jul 19 '10 at 15:16

8 Answers8

44

I think you'll want to look at Activity.getWindow() and Window.getDecorView(), and get the width/height of that.

Dave
  • 6,064
  • 4
  • 31
  • 38
  • 13
    int height = this.getWindow().getDecorView().getHeight(); – fireydude Apr 05 '14 at 11:35
  • 5
    I still get 0, strange. – nomnom Dec 02 '15 at 14:39
  • 4
    You are calling it too early it doesn't have a height during onCreate(). – Tatarize Dec 25 '15 at 00:32
  • I think the best is to add a preDrawListener to the TreeObserver sceneView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { sceneView.getViewTreeObserver().removeOnPreDrawListener(this); //do code here that has window) – Tatarize Mar 29 '17 at 00:27
21

I like time-saving answers more:

myActivity.getWindow().getDecorView().getHeight();
myActivity.getWindow().getDecorView().getWidth();
Zon
  • 18,610
  • 7
  • 91
  • 99
  • This is wrong. The height will include the height of Android's status bar and navigation bar at the bottom of the screen. – Johann Apr 02 '21 at 15:59
  • Why do you think that your status bar and navigation bar are not on the window? – Zon Apr 03 '21 at 07:20
16

I've found a way to know the exact height of my activity: if you have a view filling all the available screen behind the title bar and the task bar, just use View.getBottom() to get the real height of that activity.

JE_Muc
  • 5,403
  • 2
  • 26
  • 41
Tsimmi
  • 1,828
  • 6
  • 23
  • 35
  • 1
    getBottom() of what? What class has this method? – Jared Price Jun 24 '14 at 19:20
  • 1
    Any View : http://developer.android.com/reference/android/view/View.html#getBottom() it is relative to the parent which is why you need it to be a view that fills all the available screen in this case. – Mike Sep 19 '14 at 17:43
  • 1
    int height = this.getWindow().getDecorView().getBottom(); – kurt Jun 22 '16 at 17:10
  • 1
    getWindow().getDecorView().getBottom() returns the height of the screen in pixels. – LukeWaggoner Jun 29 '16 at 17:13
6

I did that using my root layout, the problem is that you can't obtain this info (width and height) in your onCreate/onStart/onResume methods when they are running by the first time.

Any moment after that, the size can be retrieved from your root layout (LinearLayout/FrameLayout/TableLayout/etc), in my case I was using FrameLayout:

FrameLayout f = (FrameLayout) findViewById(R.id.layout_root);
Log.d(TAG, "width " + f.getMeasuredWidth() );
Log.d(TAG, "height " + f.getMeasuredHeight() );

or:

FrameLayout f = (FrameLayout) findViewById(R.id.layout_root);
Log.d(TAG, "width " + f.getRight() );
Log.d(TAG, "height " + f.getBottom() );

Edited from here --------------------------------------------------------

I found a way to get this info on the onCreted method, but only if your have more then one activity. On my case I have a main activity that calls second activity.

On main_activity before I call the second_activity, by a new Itent, I can add extra info for the second_activity. Here is the code you need on the first activity:

Intent i = new Intent(this, SecondActivity.class);
i.putExtra("width", f.getMeasuredWidth());
i.putExtra("height", f.getMeasuredHeight());
startActivity(i);

After that on your second activity you can retrieve this info on the onCreate method of your second activity doing that:

int width = getIntent().getExtras().getInt("width");
int height = getIntent().getExtras().getInt("height");
Derzu
  • 7,011
  • 3
  • 57
  • 60
3

There is simple way to get right View size using getDefaultSize function. In case of View spanning on entire activity, you can use next approach to get activity client region dimensions:

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    int viewWidth = getDefaultSize(getWidth(), widthMeasureSpec); 
    int viewHeight = getDefaultSize(getHeight(), heightMeasureSpec);
    ...
}

To make View extending on entire activity:

MyView testView = new MyView(this);
LinearLayout testViewGroup = new LinearLayout(this);
testView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                          LayoutParams.FILL_PARENT);
testViewGroup.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                               LayoutParams.FILL_PARENT);
testViewGroup.addView(testView);
setContentView(testViewGroup);
mbinette
  • 5,094
  • 3
  • 24
  • 32
2

If you are using DataBinding (which you should be), you can call binding.getRoot().getHeight() where binding is your ViewDataBinding created with DataBindingUtil.setContentView() or other such DataBindingUtil method. You'll need to wait until the layout has size, which you can do like this:

binding.getRoot().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        binding.getRoot().getViewTreeObserver().removeOnGlobalLayoutListener(this);

        int height = binding.getRoot().getHeight();
    }
});

Note that you have to remove the OnGlobalLayoutListener or else it'll be running this every time anything changes in your layout.

LukeWaggoner
  • 8,869
  • 1
  • 29
  • 28
1

Get the height of the activity:

public static int getActivityHeight(Activity activity)
{
   return activity.findViewById(android.R.id.content).getHeight();
}

Get the width of the activity

public static int getActivityWidth(Activity activity)
{
    return activity.findViewById(android.R.id.content).getWidth();
}
Hans
  • 1,886
  • 24
  • 18
1

You will get height and width as 0 at onCreate()/onStart()/onResume() methods. Reason is size is not set yet. If you want to know current size you'd better use observer in one of this methods and do what you want when you get current size.

constraint_layout is layout root element in code bellow:

ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.constraint_layout);
constraintLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout()  {
        constraintLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        int width = constraintLayout.getWidth();
        int height = constraintLayout.getHeight();
        doSmthMethod(width, height);
    }
});
  • This is a valid answer if you're trying to get the dimensions of a layout right after the layout happened, and before the views are rendered – xav Feb 14 '21 at 03:24