Skip to content Skip to sidebar Skip to footer

How To Make Customadapter Fill Gridview Height

I am working on Sudoku app. The layout is 9x9 GridView. Each GridView include 9 Textviews by customAdapter. I want to make the 9 TextViews to fill the height of each GridView. How?

Solution 1:

You can set the height of each LinearLayout of TextView that you inflate in your GridView's adapter to screenHeight/3.

In getView after inflating the textView's xml, cast the inflated view to LinearLayout and set its height as:

LayoutInflaterli= getLayoutInflater();
LinearLayoutll= (LinearLayout) li.inflate(R.layout.menu_items, null);

intscreenHeight= ((Activity) context).getWindowManager()
                    .getDefaultDisplay().getHeight();

ll.setMinimumHeight(screenHeight/3);

Edit

activity_main.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent">

text_item.xml

<?xml version="1.0" encoding="UTF-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:orientation="vertical" ><TextViewandroid:id="@+id/grid_item_label"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"tools:ignore="InefficientWeight"android:padding="0dp"android:textSize="12sp" ></TextView></LinearLayout>

MainActivity.java

publicclassMainActivityextendsActivity {

    String[] str = { "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" };

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        GridViewgridView= (GridView) findViewById(R.id.gridView1);

        gridView.setAdapter(newmyGridAdapter(this, str));

        gridView.setOnItemClickListener(newOnItemClickListener() {
            publicvoidonItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

            }

        });

    }

    publicclassmyGridAdapterextendsBaseAdapter {

        private Context context;
        private String[] mobileValues;

        publicmyGridAdapter(MainActivity mainActivity, String[] arrayEmpty) {
            this.context = mainActivity;
            this.mobileValues = arrayEmpty;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflaterinflater= (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            LinearLayout gridView;

            if (convertView == null) {

                // get layout from text_item.xml
                gridView = (LinearLayout)inflater.inflate(R.layout.text_item, null);

                intscreenHeight= ((Activity) context).getWindowManager()
                        .getDefaultDisplay().getHeight();

                gridView.setLayoutParams(newGridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, screenHeight/9));


                //gridView.setMinimumHeight(screenHeight/9);// set value into textviewTextViewtextView= (TextView) gridView
                        .findViewById(R.id.grid_item_label);
                textView.setText(mobileValues[position]); 

            } else {
                gridView = (LinearLayout) convertView;
            }

            return gridView;
        }

        @OverridepublicintgetCount() {
            // TODO Auto-generated method stubreturn mobileValues.length;
        }

        @Overridepublic Object getItem(int position) {
            // TODO Auto-generated method stubreturnnull;
        }

        @OverridepubliclonggetItemId(int position) {
            // TODO Auto-generated method stubreturn0;
        }
    }

}

In the getView what i've done is i've inflated the text_item.xml for 9x9 times, in which i've dynamically set the layout height of text_item to screenHeight/9. Let me know if it work for you or not.

Edit 2

activity_main.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><GridViewandroid:id="@+id/gridView1"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:numColumns="9"android:padding="0dp"android:background="#000000"android:horizontalSpacing="3dp"android:verticalSpacing="3dp"android:stretchMode="columnWidth" ></GridView></LinearLayout>

text_item.xml

<?xml version="1.0" encoding="UTF-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:background="#FFFFFF"android:orientation="vertical" ><TextViewandroid:id="@+id/grid_item_label"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"tools:ignore="InefficientWeight"android:padding="0dp"android:textSize="12sp" ></TextView></LinearLayout>

Solution 2:

You can get device height width and for 3 row n col divide by 3 both and set programmtically height width may solve your problem.

Solution 3:

Try this Code

<GridViewandroid:id="@+id/gridView1"android:layout_width="match_parent"android:layout_height="match_parent"android:columnWidth="10dp"android:gravity="center"android:horizontalSpacing="10dp"android:numColumns="3"android:stretchMode="columnWidth"android:verticalSpacing="10dp" ></GridView>

This may help u

Solution 4:

Please have a look at this open source sample for soduku.

http://code.google.com/p/opensudoku-android/

This will help you to draw 9 X9 grdiview.

Hi this code.

<TextViewandroid:id="@+id/num"android:text="1"android:gravity="center"android:textStyle="bold"android:textColor="@android:color/darker_gray"android:textSize="25sp"android:layout_centerInParent="true"android:layout_width="fill_parent"android:layout_height="fill_parent"></TextView>

Solution 5:

Since GridView will function when your adapter is called and does not have a height property for each cell/child, your best option may be to create an image of the w/h that you want and set it as textView.SetBackGround...

This is what I used for my Sudoku app. It is on play store, please check out the screen and let me know your thoughts.

At the PlayStore - EasySudoku, free download - http://tinyurl.com/c4qra79

Post a Comment for "How To Make Customadapter Fill Gridview Height"