How to adjust gridview height for customAdapter

I am working on a Sudoku application. Layout 9x9 GridView. Each GridView includes 9 Textviews with a customAdapter. I want 9 TextViews to fill the height of each GridView. How?

This is what I have now enter image description here

Here is myGridAdapter:

public class myGridAdapter extends BaseAdapter { private Context context; private String[] mobileValues; public myGridAdapter(MainActivity mainActivity, String[] arrayEmpty) { this.context = mainActivity; this.mobileValues = arrayEmpty; } public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View gridView; if (convertView == null) { gridView = new View(context); // get layout from text_item.xml gridView = inflater.inflate(R.layout.text_item, null); // set value into textview TextView textView = (TextView) gridView .findViewById(R.id.grid_item_label); textView.setText(mobileValues[position]); } else { gridView = (View) convertView; } return gridView; } ..... } 

This is XML for each text element.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/grid_item_label" android:layout_width="match_parent" android:layout_height="fill_parent" android:background="@drawable/cell_shape" android:text="@string/_1" tools:ignore="InefficientWeight" android:textSize="12sp" > </TextView> </LinearLayout> 

This is the form for each TextView.

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <solid android:color="#FFFFFF"/> <stroke android:width="1dp" android:color="#777777" /> </shape> 

I want my gridview to be like this enter image description here

After applying the first answer, bakriOnFire

enter image description here

+4
source share
5 answers

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

In getView, after inflating the textView xml, draw an inflated view of LinearLayout and set its height as:

 LayoutInflater li = getLayoutInflater(); LinearLayout ll = (LinearLayout) li.inflate(R.layout.menu_items, null); int screenHeight = ((Activity) context).getWindowManager() .getDefaultDisplay().getHeight(); ll.setMinimumHeight(screenHeight/3); 

Edit

activity_main.xml

 <LinearLayout xmlns: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"?> <LinearLayout xmlns: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" > <TextView android: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

 public class MainActivity extends Activity { 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" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); GridView gridView = (GridView) findViewById(R.id.gridView1); gridView.setAdapter(new myGridAdapter(this, str)); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { } }); } public class myGridAdapter extends BaseAdapter { private Context context; private String[] mobileValues; public myGridAdapter(MainActivity mainActivity, String[] arrayEmpty) { this.context = mainActivity; this.mobileValues = arrayEmpty; } public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (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); int screenHeight = ((Activity) context).getWindowManager() .getDefaultDisplay().getHeight(); gridView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, screenHeight/9)); //gridView.setMinimumHeight(screenHeight/9); // set value into textview TextView textView = (TextView) gridView .findViewById(R.id.grid_item_label); textView.setText(mobileValues[position]); } else { gridView = (LinearLayout) convertView; } return gridView; } @Override public int getCount() { // TODO Auto-generated method stub return mobileValues.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } } } 

In getView, what I did was I blew text_item.xml 9x9 times in which I dynamically set the height of the text_item layout to screenHeight / 9. Let me know if this works for you or not.

Edit 2

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <GridView android: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"?> <LinearLayout xmlns: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" > <TextView android: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> 
+4
source

You can get the width of the device height for the 3rd row n col divide by 3, and setting the width of the program width can solve your problem.

+2
source

Try this code

 <GridView android: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> 

It may help u

+1
source

Please see this open source sample for soduku.

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

This will help you make 9 X9 grdiview.

Hi this code.

 <TextView android: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> 
+1
source

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

This is what I used for my sudoku app. This is in the game store, please check the screen and tell me your thoughts.

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

+1
source

Source: https://habr.com/ru/post/1479351/


All Articles