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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:46:38+00:00 2026-06-04T14:46:38+00:00

I need to center grid view horizontally in my android layout.xml. I have searched

  • 0

I need to center grid view horizontally in my android layout.xml. I have searched google for quite a while but didnt succeed in finding an answer.

I can only change gridview horizontal position by changing strechMode, but then my items are not near one another.
that i need is items be one near another (without spaces) and centered horizontally. I choose strechmode = none, so now my items are near one another, but they are on the left of the screen, i just want them to be centered horizontally.

Here is my layout xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent"
          android:background="@drawable/background" 
          android:gravity="center">

<GridView android:id="@+id/lw_gridview" 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:numColumns="3" 
          android:columnWidth="48dp"
          android:horizontalSpacing="0dp"
          android:verticalSpacing="0dp"
          android:stretchMode="none"
          />


</LinearLayout>

Here is fragment of an image which is set on gridview by image adapter:

imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(48,48)); //on medium density
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setPadding(0, 0, 0, 0);

How can i succeed?

  • 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-04T14:46:40+00:00Added an answer on June 4, 2026 at 2:46 pm

    I centered my grid view manually, by calculating and setting its padding. Here’s what my onCreate does:

    • find out width of the screen and screen density
    • convert constant values for item width and spacing from DIP to pixels
    • calculate number of columns I can fit.

    Equation looks like this, numColumns is the only unknown:

    numColumns * itemWidth + (numColumns - 1) * spacingBetweenItems + selectorPadding <= sreenWidth

    • having numColumns, calculate how much horizontal space my grid view will actually need
    • having actual width of grid view, set padding so that it ends up in the center of screen

    OK, and here’s some code:

    // Convert DIPs to pixels
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    mSizePx = (int) Math.floor(SIZE_DIP * metrics.scaledDensity);
    mSpacingPx = (int) Math.floor(SPACING_DIP * metrics.scaledDensity);
    
    GridView gridview = (GridView) findViewById(R.id.gridview);
    // Find out the extra space gridview uses for selector on its sides.
    Rect p = new Rect();
    gridview.getSelector().getPadding(p);
    int selectorPadding = p.left + p.right;
    
    // Determine the number of columns we can fit, given screen width,
    // thumbnail width, and spacing between thumbnails.
    int numColumns = (int) Math.floor(1f * (metrics.widthPixels - selectorPadding + mSpacingPx)
            / (mSizePx + mSpacingPx));
    
    int contentWidth = numColumns * mSizePx; // Width of items
    contentWidth += (numColumns - 1) * mSpacingPx; // Plus spaces between items
    contentWidth += selectorPadding; // Plus extra space for selector on sides
    
    // Now calculate amount of left and right margin so the grid gets
    // centered. This is what we
    // unfortunately cannot do with layout_width="wrap_content"
    // and layout_gravity="center_horizontal"
    int slack = metrics.widthPixels - contentWidth;
    
    gridview.setNumColumns(numColumns);
    gridview.setPadding(slack / 2, slack / 2, slack / 2, slack / 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started with Google's sample code for a Grid View but how would I
Need to display Update Progress at the center of a Grid view placed in
I need to something like <TextBlock Grid.Column=2 Text={Binding FirstName,LastName} VerticalAlignment=Center/> The Windows Phone sdk(silverlight)
I have an Extjs Grid with a Paging toolbar. I need to pass the
I have problem with text wrapping. Without StackPanel this TextBlock works, but i need
I have a grid view as under <asp:GridView ID=dgTask runat=server Width=100% AutoGenerateColumns=False onrowdatabound=dgTask_RowDataBound> <Columns>
I need to center a CSS menu that has an unknown width. I found
I am trying to center a Window to the owner window. I also need
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need to use own imaged markers instead built-in pins. I have several questions. 1.

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.