Search code examples
androidgridviewviewgroup

How to create custom gridview (Like matrix structure ) in Android


I want to create a matrix like gridview say 10x20 matrix

we want to specify the number of rows and column of the View ie 10x20

if the screen is low it should scroll to horizontally and vertically for example the image below describe the Matrix Gridview Each cell represent (0,0) (0,1) etc... (1,0) (1,1) etc..

How can generate this type of view? Advance Thanks ....!!!

enter image description here


Solution

  • This can be achieved by GridLayout and dynamic Cell creation

    /* * * Copyright 2012 Jess Anders * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.GridLayout;
    import android.widget.HorizontalScrollView;
    import android.widget.LinearLayout;
    import android.widget.ScrollView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        GridLayout gl;
        TextView[] text;
        int item;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            gl = new GridLayout(MainActivity.this);
            gl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT));
            gl.setOrientation(0);
            gl.setColumnCount(11);
            gl.setRowCount(3);
    
            text = new TextView[100];
            ScrollView sv = new ScrollView(this);
            sv.setScrollbarFadingEnabled(false);
            HorizontalScrollView scrolview = new HorizontalScrollView(this);
            scrolview.setScrollbarFadingEnabled(false);
            LinearLayout linearLayout = new LinearLayout(this);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            linearLayout.setLayoutParams(params);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            linearLayout.addView(sv);
            sv.addView(scrolview);
            scrolview.setHorizontalScrollBarEnabled(true);
            setContentView(linearLayout);
            for (int i = 0; i < 100; i++) {
                for (int j = 0; i < 10; j++) {
                    text[i] = new TextView(MainActivity.this);
                    text[i].setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    text[i].setText(String.valueOf(i) + "," + String.valueOf(j));
                    text[i].setTextSize(25);
                    text[i].setPadding(50, 25, 10, 25);
                    text[i].setOnClickListener(new View.OnClickListener() {
    
                        int pos = item;
    
                        public void onClick(View v) {
    
                            Toast.makeText(getBaseContext(), pos + " Clicked",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
                    gl.addView(text[i]);
                }
            }
    
            scrolview.addView(gl);
    
        }
    
     }
    

    This is not the straight solution but... i think i need to customize the GridView which i do`nt know currently