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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:21:25+00:00 2026-05-20T11:21:25+00:00

I have a Gallery displaying TextViews. When it’s shown, the 1st item is not

  • 0

I have a Gallery displaying TextViews. When it’s shown, the 1st item is not displayed. This is because parent.getWidth() returns 0 at the first call.

So how can I set the item’s width to be a fraction of its parent gallery’s width?

Here is the code:

public class GalleryActivity extends Activity {
  private Gallery gallery;
  public void onCreate (Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.gallery);
    gallery = (Gallery) findViewById (R.id.gallery);
    gallery.setAdapter (new Adapter ());
  }

  private class Adapter extends BaseAdapter {
    private String[] items = {"A", "B", "C", "D", "E", "F"};
    public int getCount () {
      return items.length;
    }
    public Object getItem (int position) {
      return items[position];
    }
    public long getItemId (int position) {
      return position;
    }
    public View getView (int position, View convertView, ViewGroup parent) {
      TextView view = new TextView (GalleryActivity.this);
      view.setText (getItem (position).toString ());
      view.setBackgroundColor (Color.GRAY);
      view.setGravity (Gravity.CENTER);
      view.setLayoutParams (new Gallery.LayoutParams (parent.getWidth () / 3,
      LayoutParams.FILL_PARENT));
      return view;
    }
  }
}

layout/gallery.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_weight="1"
    android:scrollbars="horizontal" android:spacing="1dp"></Gallery>
  <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_weight="3"></TextView>
</LinearLayout>
  • 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-20T11:21:26+00:00Added an answer on May 20, 2026 at 11:21 am

    OK don’t quote me on this but I believe the width won’t actually get set until some contents have been drawn, so getWidth will always return 0 before that. If so, and you know what width and height you want anyway you can manually set width and height ahead of time with setWidth() and setHeight()…

    If all you want is the width and height of the actually display screen, you can use…

                    Display display = windowManager.getDefaultDisplay();
                    if (display != null) {
                        this.setHeight(display.getHeight());
                        this.setWidth(display.getWidth());
                    }
    

    And disclaimer: I’ve never worked with Galleries before and wouldn’t be surprised if there’s a better, “best practice” way to do what you’re trying…

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

Sidebar

Related Questions

I have a gallery that's loaded via a BaseAdapter . This works fine. I
friends, i have a page on which i am displaying image from gallery then
Helo folks., i am using jquery.cycle plugin for displaying image gallery.. i have a
I use jQuery colorbox plugin when displaying images from gallery. So, when i have
I have a problem displaying most popular tags from database in view. I'm not
I have photo gallery code that does image re-sizing and thumbnail creation. I use
I have custom gallery. Gallery represents items that are frame layout. There are one
I have a gallery of images on my website. Under each one, I'd like
Hi I have a gallery in my app which shows the image in an
Jquery, AJAX and URL changes... I have a gallery, and when the image change

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.