Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.


Solution 2:

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


Solution 3:

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

Post a Comment for "Android Gridview Adjusting To Screen Size"