Tsimmi
Tsimmi

Reputation: 1868

How to get the visible size on an Activity?

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.

Upvotes: 35

Views: 70487

Answers (9)

Rahim .H
Rahim .H

Reputation: 98

By experimenting with things, i figured out that We have the updated screen size in Resources.displayMetrics even before the DecorView is completely initialized.

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        .....
        // Width and Height is in Pixels
        val width = resources.displayMetrics.widthPixels
        val height = resources.displayMetrics.heightPixels
}

So with this code we can get the size of the Activity every time it is recreated. And it Works fine for me!

Upvotes: 0

Tsimmi
Tsimmi

Reputation: 1868

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.

Upvotes: 17

Serg.Stankov
Serg.Stankov

Reputation: 49

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

Upvotes: 1

Zon
Zon

Reputation: 19918

I like time-saving answers more:

myActivity.getWindow().getDecorView().getHeight();
myActivity.getWindow().getDecorView().getWidth();

Upvotes: 24

LukeWaggoner
LukeWaggoner

Reputation: 8909

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.

Upvotes: 2

Hans
Hans

Reputation: 1915

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

Upvotes: 1

Weaknespase
Weaknespase

Reputation: 31

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

Upvotes: 3

Derzu
Derzu

Reputation: 7156

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

Upvotes: 7

Dave
Dave

Reputation: 6104

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

Upvotes: 46

Related Questions