4

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?

This is what I am having 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 the XML for each text item

<?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 shape 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 one enter image description here

After applying the first answer of bakriOnFire

enter image description here

Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66

5 Answers5

6

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:

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

<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>
tvkanters
  • 3,519
  • 4
  • 29
  • 45
bakriOnFire
  • 2,685
  • 1
  • 15
  • 27
  • Can you please, modify your code to my example. I cant understand it well – Sami Eltamawy May 06 '13 at 13:08
  • Note that I have 9 Gridviews in the MainLayout, so I think I can`t use ScreenHight directly – Sami Eltamawy May 06 '13 at 13:10
  • Can I do this: int screenHeight = gridView.getHeight()/3; – Sami Eltamawy May 06 '13 at 13:13
  • 1
    you'll get the gridView's height as 0 until it is painted..you can do it all by calculation..give the height of each gridViews as screenHeight/3 and give the height of each cells of each gridview as screenHeight/9. – bakriOnFire May 06 '13 at 15:40
  • An exception, I think this will not work with my layout, I have buttons and Gridviews of Text views ,so getting screen size will not be a good solution. Thank you BTW – Sami Eltamawy May 06 '13 at 16:05
  • 1
    let me see if i can do something with your code..will let u know if i come up with something. – bakriOnFire May 06 '13 at 16:08
  • i was wondering, why do u need 9 GridViews with 3x3 cells. why not having a single grid with 9x9 cells..check my edit..its working.. – bakriOnFire May 07 '13 at 06:48
  • That`s a very good idea, but how about highlighting every 9x9 cells? is is possible? This is the main reason that made me try to use 9 Gridviews – Sami Eltamawy May 07 '13 at 11:27
  • 1
    change activity_main.xml and text_item.xml as in my **Edit 2**..you'll get the borders. – bakriOnFire May 07 '13 at 13:59
  • First, Thank you very much for your efforts. Second, Your edits works fine. Third, I am now suffering from the same problem. I want to Stretch the Height of the Cells to fill the Parent. I am now having 9x9 cells but filling only 50% of the screen and the rest is empty. I want it to fill the screen – Sami Eltamawy May 07 '13 at 21:57
  • We are now at the same initial problem, how to stretch the GridView Items to fill parent? – Sami Eltamawy May 07 '13 at 22:01
  • I have attached a snapshot of the current layout in your edits. Check it out – Sami Eltamawy May 07 '13 at 22:05
  • 1
    i can't see any attachement...the lines **int screenHeight = ((Activity) context).getWindowManager() .getDefaultDisplay().getHeight(); gridView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, screenHeight/9));** will strech the grid cells to fill the screen..i've checked it myself...you r doing something wrong..just chk it.. – bakriOnFire May 08 '13 at 05:18
  • I am using your MainActivity and your text_item.xml and your Adapter I think there is nothing more I can do. Can you please send me a screenshot of you layout if it works, or Send to me the project you have created to test this. If you works as a project I will modify on it and Accept you Answer. Thank you – Sami Eltamawy May 08 '13 at 08:16
  • can u debug an tell me what is the value of dis variable **int screenHeight** in the getView. – bakriOnFire May 10 '13 at 14:55
  • `getHeight()` is deprecated – Nima Aug 24 '13 at 18:05
  • I face the exact same problem. Definitely an improvement of my previous situation but still the problem is not resolved. – George Daramouskas May 24 '15 at 09:28
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.

Taryn
  • 242,637
  • 56
  • 362
  • 405
Manmohan Badaya
  • 2,326
  • 1
  • 22
  • 35
  • Note that I have 9 Gridviews in the MainLayout, so I think I can`t use ScreenHight directly. Is there is a way to get Gridview height ? – Sami Eltamawy May 06 '13 at 13:14
  • 1
    in getView where u r inflating.u r generating nine view named gridView in ur class.i am talking about xml for each item.as per your code u can do like this gridView.setLayoutParams(new LinearLayout.LayoutParams(width,height)); – Manmohan Badaya May 06 '13 at 13:33
1

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>

This may help u

Joy Helina
  • 378
  • 1
  • 5
  • 17
  • Thank you for your help, but the problem still exist. This code only made the spacing bigger but the Cell size still the same – Sami Eltamawy May 06 '13 at 12:21
1

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.

<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>
itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31
  • Thank you for your help, but I think I am looking for just one line of code that make the TextViews fill the height of the GridView. If you can help me with this I would be grateful for that. – Sami Eltamawy May 06 '13 at 13:01
  • hi have a look at this idea. http://code.google.com/p/starsudoku/source/browse/trunk/StarSudoku/res/layout/game.xml?r=8 – itsrajesh4uguys May 06 '13 at 13:16
1

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

user2347763
  • 469
  • 2
  • 10
  • That is the functionality, but what about the interface? I wanted to separate each Square of 9 Textviews to look better to the user – Sami Eltamawy May 07 '13 at 11:29