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 7433561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:41:28+00:00 2026-05-29T09:41:28+00:00

I want to make number of rows in Gridview dynamic depending upon the screen

  • 0

I want to make number of rows in Gridview dynamic depending upon the screen size of the android phone. so i need height of the Gridview in onCreate() to count number of rows.(height / colums) here is a code snippet::

public class GridActivity extends Activity
{
     GridView gridView;
     GridAdapter adapter;'

     public void onCreate(Bundle savedInstance state)
     {
          setContentView(R.layout.gridview);
          gridView = (GridView) findViewById(R.id.grid);

          adapter = new GridAdapter(this);
          gridView.setAdapter(adapter);

          int gridHeight = gridView.getMeasuredHeight();
          Log.d("tag"," height :: " + gridHeight);
     }
 }

In log cat it shows — height :: 0
I don’t understand y it gives zero after properly inflating.

Is this a bug? Or I am missing something?

  • 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-05-29T09:41:28+00:00Added an answer on May 29, 2026 at 9:41 am

    GridView would return 0 measuredheight and 0 height unless it is drawn to window, so you can not get height of GridVIew at the point, A workaround of your problem may be:
    Add your Nth view to gridView by comparing total height of device with height of GridView occupied after n-1 views has been drawn to screen.

    public void addNCompareHeight(View view)
     {   
        ViewTreeObserver observer = grid.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //in here, place the code that requires you to know the dimensions.
                //this will be called as the layout is finished, prior to displaying.
                if(gridHeight<requiredHeight)
                {
                     grid.addView(view);
                     addNCompareHeight(next);
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new android app developer i want make app for tablets and phone
I want to make a dynamic textarea, it should increase in rows as the
I want to make a number selector like in alarm application, which has two
I want to make a program that will read some number in string format
I want to make sure the input is A)a number and B)has at most
I have a double number like 1,01 and I want to make it 2
I've N number of <li> 's I want to make jquery slider to work
I want to make a textbook where it starts out as a given width/height.
I am using jquery ui's tabs And i want to make it dynamic by
I am displaying a ListView that can have any number of rows. I want

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.