22

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.

Priyanka
  • 677
  • 5
  • 20
png
  • 4,368
  • 7
  • 69
  • 118

4 Answers4

15

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.

Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80
  • i have added. lease have a look. Thanks for ur time and help – png Jan 03 '12 at 00:51
  • 2
    @ Pareetha..Pleasure is all mine. This is what this forum is meant for. – Usama Sarwar Jan 04 '12 at 05:02
  • by setting as u said i am acheiving the same effect i got with convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39)); for all screen. But still i face an issue . The complete grid view is not exactly fitting the screen. – png Jan 04 '12 at 05:25
  • here note that height = 75dip i have mentioned. If i make it as wrap_content the grid will overflow the scree and need to scroll to see full.. CAn you suggest how to make the scroll view exactly fit in to the screen – png Jan 04 '12 at 05:26
  • Ok.. that may be because of the image you are using is greater in height...and when you wrap content the image it takes more place as that of the image. Use the images according to the size of the one cell of the grid. – Usama Sarwar Jan 04 '12 at 05:41
  • thats exactly my point, size of one cell will differ depending on screen size right, so how do we define it so that it looks same for all screen size – png Jan 04 '12 at 06:29
  • Yes, for different screen resolutions you have to change drawbles accordingly, please refer to this link for details. http://developer.android.com/guide/practices/screens_support.html – Usama Sarwar Jan 04 '12 at 08:05
  • ok Thank you. This densitey, pixels, dpi always confuses me alot. i will stick to keeping the height harcoded for the time being :) Thanks for the responses – png Jan 04 '12 at 09:01
  • @UsamaSarwar can you please help me to solve this https://stackoverflow.com/questions/49957819/gridview-and-it-is-images-are-not-automatically-fit-for-all-the-screen-sizes-in – Nikson Apr 21 '18 at 16:39
12

@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...

optimus
  • 729
  • 2
  • 12
  • 36
  • 1
    Thank you.i had tried this, not working. For me, no: cols must be three. Also my grid is an imageview + textview – png Jan 03 '12 at 02:29
0

This is my gridviewHere I only changed width and height as match parent and wrap content.

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:id="@+id/textView5"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingTop="5dp"
            android:text="Category"
            android:textSize="20dp" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="170dp"
            android:layout_height="170dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp"
            android:src="@drawable/iconclothcolor" />


        <TextView
            android:layout_width="wrap_content"
            android:id="@+id/textView3"
            android:layout_marginTop="2dp"
            android:textSize="16dp"
            android:textColor="#F37E98"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Shirt" />


        <TextView
            android:layout_width="wrap_content"
            android:id="@+id/textView4"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Gender - Male" />

        <TextView
            android:layout_width="wrap_content"
            android:id="@+id/textView8"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Quantity - 10" />

        <TextView
            android:layout_width="wrap_content"
            android:id="@+id/textView6"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Size - S" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:id="@+id/textView10"
            android:textSize="20dp"
            android:text="Offer" />

        <TextView
            android:layout_width="wrap_content"
            android:id="@+id/textView7"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="20dp"
            android:textColor="#F37E98"
            android:text="Price - 1500 Rs" />


    </LinearLayout>

</androidx.cardview.widget.CardView>

And in the Mainclass add this -->

GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2);
        recyclerView.setLayoutManager(gridLayoutManager);
FGH
  • 2,900
  • 6
  • 26
  • 59
0

To adjusting Gridview to screen size

In onCreate take rowHeight :

root = binding.root

view.onGlobalLayout {
            rowHeight =
                root.measuredHeight / currentPageNumberOfRow
            fillView(appsList)
        }

In adapter:

          override fun getView(position: Int, ConvertView: View?, parent: ViewGroup?): View? {
            var mConvertView = ConvertView
            if (layoutInflater == null) {
                layoutInflater =
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as 
                        layoutInflater!!.inflate(R.layout.item_app_shortcut, null)
          }

           if (mConvertView == null) {
                mConvertView =
                layoutInflater!!.inflate(R.layout.item_child_launcher_app, null)
               
        //Add this: 

         mConvertView.layoutParams = AbsListView.LayoutParams(GridView.AUTO_FIT, rowHeight)

            }

Dont forget set row image:

     android:layout_width="0dp"
    android:layout_height="0dp"

To fit on screen

AmirMohamamd
  • 301
  • 3
  • 14