1 Replies - 4697 Views - Last Post: 23 August 2012 - 10:29 PM

#1 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

How to make gridview fit screen

Posted 23 August 2012 - 09:03 PM

Hey guys, I'm fairly new to Android development, and I've been working on displaying images in a Gridview. I can now display the images in the Gridview, but now I can't make the Gridview fit the entire screen. I do use "fill_parent" for the width attribute, but that still only makes it fill half the screen. I've tried setting the width to about 300 which is about the full screen size, but it will push the views above it to the right more. Here's the xml file:
        <TableRow 
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            
            <TextView android:layout_width = "wrap_content"
                android:layout_height="wrap_content"
                android:textSize = "16dp"
                android:text ="@string/lblMinutes"/>

            <EditText
                android:id="@+id/txtMinutes"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:inputType="number" >

            </EditText>

         	 <Button
            	android:id="@+id/btnMinutes"
            	android:layout_width="wrap_content"
           	 	android:layout_height="40dp"
            	android:text="@string/lblBtnMinutes" />
        </TableRow>


        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >


            <GridView
                android:id="@+id/gvImages"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:columnWidth="180dp"
                android:horizontalSpacing="10dp"
                android:numColumns="auto_fit"
                android:padding="10dp"
                android:stretchMode="columnWidth"
                android:verticalSpacing="10dp">

            </GridView>

        </TableRow>



As you can see, I use a table layout because I wanted three views on one line, and then the gridview below that. If there's a better way to do this, please let me know. Anyway, only my EditText and Button are moved. I don't know why this happens, I think it should be separate since they are different rows, but apparently that's not how it works. Does anyone know how I could solve this problem?

Is This A Good Question/Topic? 0
  • +

Replies To: How to make gridview fit screen

#2 miguel879  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 40
  • Joined: 20-July 12

Re: How to make gridview fit screen

Posted 23 August 2012 - 10:29 PM

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview" android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
         android:numColumns="3"
        android:stretchMode="columnWidth"
        android:layout_below="@+id/text1view" 
                     android:gravity="center"
                     android:horizontalSpacing="10dip"
                    android:background="@drawable/home_bg"

                                />




each grid item is

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/GridItem"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:gravity="center_horizontal"
  >

   <ImageView android:id="@+id/grid_item_image"
      android:layout_width="wrap_content"
      android:layout_height="75dip"
      android:padding="0dip"
      android:layout_margin="0dip"
      >
   </ImageView>

   <TextView android:id="@+id/grid_item_text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
           android:textColor="#000000"
           android:padding="0dip"
      android:gravity="center_horizontal"
      android:layout_below="@+id/grid_item_image"
      android:layout_alignParentBottom="true"
      android:textSize="10sp"
      >
   </TextView>

</LinearLayout>




code of the adapter

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.griditems, null);
        holder = new ViewHolder();
        holder.text1 = (TextView) convertView
                .findViewById(R.id.grid_item_text);

        holder.image = (ImageView) convertView
                .findViewById(R.id.grid_item_image);

        // if it's not recycled, initialize some attributes
        holder.image.setImageResource(gridItemIds[position]);
        holder.text1.setText(gridTitles[position]);
        int h = mContext.getResources().getDisplayMetrics().densityDpi;

        //holder.image.setLayoutParams(new LayoutParams(h-50,h-50));
        convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39));
        //holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        //holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
        // holder.image.setPadding(20, 20, 20, 20);

        // convertView.setLayoutParams(new GridView.LayoutParams(Utils
        // .getLayoutParameter(), Utils.getLayoutParameter()+50));

        holder.image.setonclickListener(new View.onclickListener() {

            @Override
            public void onclick(View view) {
                        new ModuleManager().startModuleACtivity(position, mContext);
            }
        });
        convertView.setTag(holder);
    } else {

        holder = (ViewHolder) convertView.getTag();

    }

    return convertView;
}

static class ViewHolder {
    TextView text1;
    ImageView image;

}





Hope this help you..

miguel
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1