Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android How to stretch rows in the gridview to fill screen?

I have simple GridView (with one column and six rows), that displays ImageView with TextView in the cell (I create adapter). How to stretch rows to fill entire screen height?? Now I have some space below cells...

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout android:layout_height="fill_parent"
    android:layout_width="150dp" 
    android:orientation="vertical"
    android:id="@+id/left">
    <include layout="@layout/menu_grid"></include>
</LinearLayout>
<LinearLayout android:layout_height="fill_parent"
    android:layout_width="fill_parent" 
    android:orientation="vertical"
    android:id="@+id/right">
    <ImageView android:src="@drawable/image"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:id="@+id/imageMain" 
        android:layout_gravity="center">
    </ImageView>
</LinearLayout>

menu_grid.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<GridView android:layout_height="wrap_content"
    android:layout_width="fill_parent" 
    android:id="@+id/gridView"
    android:padding="10dp" 
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp" 
    android:numColumns="1"
    android:columnWidth="100dp" 
    android:stretchMode="columnWidth"
    android:gravity="center"></GridView>

item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="fill_horizontal">
<ImageView android:src="@drawable/icon"
    android:layout_height="70dp" android:id="@+id/imageIcon"
    android:layout_width="70dp" android:layout_gravity="center_horizontal"></ImageView>
<TextView android:id="@+id/textIcon" android:text="TextView"
    android:layout_gravity="center" android:layout_height="wrap_content"
    android:layout_width="wrap_content"></TextView>

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
//....some code
//....

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    if (convertView == null) {  
        LayoutInflater inflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.item, null);

        TextView text = (TextView) view.findViewById(R.id.textIcon);
        text.setText(labels[position]);

        ImageView image = (ImageView) view.findViewById(R.id.imageIcon);
        image.setImageResource(icons[position]);        

    } else {
        view = convertView;
    }

    return view;
}

}

like image 798
snil Avatar asked Aug 15 '11 16:08

snil


2 Answers

As of what i understood, the question is- if i have 6 rows of data to be filled in the gridView, how can i make these 6 rows fill the whole screen, instead of showing 6 rows and some empty space under the last row.

To achieve this you must set minimum height of the gridView item layout (in your case its item.xml) to (height of the screen)/6, where 6 is the (no.of rows you have to fill). This minimum height can be set in the adapter you are using.

To find the height of the screen of the device:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

int width = metrics.widthPixels;
int height = metrics.heightPixels;

find these values in the Activity and make them public static and use them in the adapter, i had a problem in finding these values in the adapter class

then to set minimum height:

view = inflater.inflate(R.layout.item, null);
view.setMinimumHeight(GridViewActivity.height/6);

I tried this after seeing the post, and it worked perfectly.

like image 178
Archie.bpgc Avatar answered Sep 21 '22 23:09

Archie.bpgc


If you just want to increase the space between the lines You can use padding it should work. you can set padding through xml or programmatically based on your requirement.

// this you can set inside your view.
android:paddingLeft="5px"
android:paddingRight="5px"
android:paddingTop="10px"
android:paddingBottom="10px"

// this you can set to your any widgets like TextView/Layouts/Buttons etc..
setPadding(top, left, bottom, right);
like image 35
Daud Arfin Avatar answered Sep 21 '22 23:09

Daud Arfin