Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android gridview adjusting to screen size

The grid view of my application has 3 rows and 3 columns. I want this to fill the screen ,irrespective of the screen size of the device.

I tried getting window size and setting the layoutparams accordingly. But this is not giving a perfect alignment as it works with weightsum in linearlayout.

So can we use weightsum for gridview or is there any other android property that can be used. Thanks a lot for time and response.

<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:layout_below="@+id/text1view"
                android:background="@drawable/home_bg"
                android:gravity="center"
                android:horizontalSpacing="10dip"
                android:numColumns="3"
                android:stretchMode="columnWidth" />

each grid item is

<?xml version="1.0" encoding="utf-8"?>

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

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

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;

}

pls see the line

convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39));

i arrived at this value with some trials on different size emulator. But i dont think its a right way to define height and width.

like image 241
png Avatar asked Jan 02 '12 05:01

png


2 Answers

As far as i understood your question, as you have not posted any code, what you are trying to do is, you want gridview to appear on full screen.

Firstly you must be using some layout to put your gridview in. Do the following:

Make height and width of the layout to fill parent:

android:layout_width="fill_parent"
android:layout_height="fill_parent"

then, set height and width of your gridview to fill_parent as well.

Well, this is just a supposed solution that you might have been working this way. putting some code would be much better. Hope this works for you.

Update: Well the problem may lie here:

 convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39));

change to:

 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
 convertView.setLayoutParams(new GridView.LayoutParams(params));

Hope this works for you.

like image 135
Usama Sarwar Avatar answered Oct 31 '22 08:10

Usama Sarwar


@preetha: first of all look at this example i used this and my layout did not affect irrespective of devise height and width....

http://developer.android.com/resources/tutorials/views/hello-gridview.html

set the following in xml

android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"

hope this helps...

like image 44
optimus Avatar answered Oct 31 '22 10:10

optimus