snil
snil

Reputation: 181

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;
}

}

Upvotes: 18

Views: 35024

Answers (4)

Edu
Edu

Reputation: 31

Extend a layout say Framelayout then override the onMeasure method.

public class CameraView extends FrameLayout 

public TextView titleView;

public CameraView(Context context,AttributeSet attrs) {
    super(context, attrs)
    titleView = rootView.findViewById(R.id.titleView);
}


@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}

You can then use this view in your BaseAdapter and set the layout_width and layout_height to match_parent

Upvotes: 0

Daud Arfin
Daud Arfin

Reputation: 2499

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);

Upvotes: 3

Archie.bpgc
Archie.bpgc

Reputation: 24012

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.

Upvotes: 32

Quintin Robinson
Quintin Robinson

Reputation: 82375

I hope I am understanding you correctly, if so it is as easy as changing your GridView to fill_parent instead of wrap_content.

Change

<GridView android:layout_height="wrap_content"

to

<GridView android:layout_height="fill_parent"

If this is not the case then it is probably the layout height on the LinearLayout of the item.xml that you want to modify.

Upvotes: -1

Related Questions