Chen Kinnrot
Chen Kinnrot

Reputation: 21015

Get the visible height of a view

Is it possible to know the visible height of my activity as soon as the status bar opens?

I want to know the visible height of my screen.

Upvotes: 3

Views: 5816

Answers (4)

Aditi Parikh
Aditi Parikh

Reputation: 1522

Try View treeobserver method.. Like :

   main_layout.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() 
                    {

                        Rect r = new Rect();
                        // r will be populated with the coordinates of your view
                        // that area still visible.
                        main_layout.getWindowVisibleDisplayFrame(r);

                        int heightDiff = main_layout.getRootView().getHeight()-(r.bottom -r.top);

                    }
    });

Replace main_layout with your desired View's object. Hope it can help.

Upvotes: 6

Zohaib
Zohaib

Reputation: 2865

Hopefully this line of code will work for you. i use this when i want height n width of the screen

// get screen width/height

          DisplayMetrics metrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(metrics); height =
          metrics.heightPixels; width = metrics.widthPixels;

Upvotes: -1

funkmaster
funkmaster

Reputation: 9

myLayout.getHeight(); would return the actual visible height of the view only after it shows up on the screen before that it would only be 0. if you are really interested in knowing myLayout.getHeight() then you might wanna check after the view is displayed on the screen

If you want to know the height/width of the screen then you might consider using the following code : DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm);

final int height = dm.heightPixels; final int width = dm.widthPixels;

Upvotes: -1

RufusInZen
RufusInZen

Reputation: 2189

Give an id to your main layout element:

<LinearLayout id="myLayout">
    ...
</LinearLayout>

Then get its height after setContentView():

View myLayout = (View) findViewById(R.id.myLayout);
int height = myLayout.getHeight();

Also see this question for more info.

Upvotes: -3

Related Questions