Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9180365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:01:54+00:00 2026-06-17T18:01:54+00:00

In my application i have gridview in linearlayout. Due to different sizes of screen,

  • 0

In my application i have gridview in linearlayout. Due to different sizes of screen, i want to calculate the width of the gridview’s container (which in linearlayout) and then i will decide, how many column should display on screen. My problem is. when i write

linearlayout.getwidth();

it returns 0 and its because its too early to get size of view in oncreate method.

What is the clean solution to get height or width of the view.

i tried

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int gridSize = display.getWidth();

but android says its deprecated 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:01:56+00:00Added an answer on June 17, 2026 at 6:01 pm

    Display.getWidth() and Display.getHeight() are both deprecated as of API 13 in favor of Display.getSize(Point). In order to be compatible with both, you have to version-check and pick the appropriate one.

    This is how I do so:

    Point size = new Point();
    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    if (Build.VERSION.SDK_INT >= 13) {
        display.getSize(size); // API 13+
    } else {
        size.x = display.getWidth();
        size.y = display.getHeight();
    }
    

    Then afterward, size.x is your width. (Don’t forget to precede your method signature with @TargetApi(13) @SuppressWarnings("deprecation").)

    Keep in mind, this is the unit’s display width; if you want to simply get the size of one view, you have to call it after the UI is laid out. That’s slightly unrelated to this answer, but you can find a solution in this thread’s answer instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my asp.net application i have a gridview control, in which i added a
I have gridview in my asp.net 3.5 application [C#]. Which looks like this: <asp:GridView
I am making Winforms application. I have put gridview in usercontrol. Which is another
I have a GridView and want to on an ASP.NET Web Application page and
I have developed an asp.net application that has a Gridview control. I want it
In my Application I have an Agenda build from a GridView. I want to
I have finished coding the logic of my application, which contains a GridView, a
i have web application in which i used gridview. In gridview i used radiobutton
I'm developing a web application and I'm using a gridview and sqldatasource. I have
My application have a service and an activity. Sometimes the service will send a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.