Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Gallery first item not displaying: parent.getWidth returns 0
    primarykey
    data
    text
    <p>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.</p> <p>So how can I set the item's width to be a fraction of its parent gallery's width?</p> <p>Here is the code:</p> <pre><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; } } } </code></pre> <p>layout/gallery.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;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"&gt;&lt;/Gallery&gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="3"&gt;&lt;/TextView&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload