Home » Android » android – How to get the visible size on an Activity?

android – How to get the visible size on an Activity?

Posted by: admin April 23, 2020 Leave a comment


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.

How to&Answers:

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


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.


I like time-saving answers more:



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() );


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());

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");


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,
testViewGroup.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,


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() {
    public void onGlobalLayout() {

        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.


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();


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() {
    public void onGlobalLayout()  {
        int width = constraintLayout.getWidth();
        int height = constraintLayout.getHeight();
        doSmthMethod(width, height);