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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















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










share|improve this question




















  • 1





    create custom GridView

    – Rustam
    Oct 22 '14 at 6:59











  • @Rustam can you please give some guidelines

    – kukku
    Oct 22 '14 at 7:22






  • 1





    see gist.github.com/pskink/4918ec060a244540dcca

    – pskink
    Oct 22 '14 at 7:50











  • @pskink plz guide me how to use this ? can u give me an example

    – kukku
    Oct 22 '14 at 8:46











  • sorry, all the test code i had is already deleted

    – pskink
    Oct 22 '14 at 9:12


















3















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










share|improve this question




















  • 1





    create custom GridView

    – Rustam
    Oct 22 '14 at 6:59











  • @Rustam can you please give some guidelines

    – kukku
    Oct 22 '14 at 7:22






  • 1





    see gist.github.com/pskink/4918ec060a244540dcca

    – pskink
    Oct 22 '14 at 7:50











  • @pskink plz guide me how to use this ? can u give me an example

    – kukku
    Oct 22 '14 at 8:46











  • sorry, all the test code i had is already deleted

    – pskink
    Oct 22 '14 at 9:12














3












3








3


2






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










share|improve this question
















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







android gridview viewgroup






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 22 '14 at 7:21







kukku

















asked Oct 22 '14 at 6:47









kukkukukku

416317




416317








  • 1





    create custom GridView

    – Rustam
    Oct 22 '14 at 6:59











  • @Rustam can you please give some guidelines

    – kukku
    Oct 22 '14 at 7:22






  • 1





    see gist.github.com/pskink/4918ec060a244540dcca

    – pskink
    Oct 22 '14 at 7:50











  • @pskink plz guide me how to use this ? can u give me an example

    – kukku
    Oct 22 '14 at 8:46











  • sorry, all the test code i had is already deleted

    – pskink
    Oct 22 '14 at 9:12














  • 1





    create custom GridView

    – Rustam
    Oct 22 '14 at 6:59











  • @Rustam can you please give some guidelines

    – kukku
    Oct 22 '14 at 7:22






  • 1





    see gist.github.com/pskink/4918ec060a244540dcca

    – pskink
    Oct 22 '14 at 7:50











  • @pskink plz guide me how to use this ? can u give me an example

    – kukku
    Oct 22 '14 at 8:46











  • sorry, all the test code i had is already deleted

    – pskink
    Oct 22 '14 at 9:12








1




1





create custom GridView

– Rustam
Oct 22 '14 at 6:59





create custom GridView

– Rustam
Oct 22 '14 at 6:59













@Rustam can you please give some guidelines

– kukku
Oct 22 '14 at 7:22





@Rustam can you please give some guidelines

– kukku
Oct 22 '14 at 7:22




1




1





see gist.github.com/pskink/4918ec060a244540dcca

– pskink
Oct 22 '14 at 7:50





see gist.github.com/pskink/4918ec060a244540dcca

– pskink
Oct 22 '14 at 7:50













@pskink plz guide me how to use this ? can u give me an example

– kukku
Oct 22 '14 at 8:46





@pskink plz guide me how to use this ? can u give me an example

– kukku
Oct 22 '14 at 8:46













sorry, all the test code i had is already deleted

– pskink
Oct 22 '14 at 9:12





sorry, all the test code i had is already deleted

– pskink
Oct 22 '14 at 9:12












3 Answers
3






active

oldest

votes


















5














try this:



GridViewCustomAdapter



import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

public class GridViewCustomAdapter extends BaseAdapter {

ArrayList<String> items;

static Activity mActivity;

private static LayoutInflater inflater = null;

public GridViewCustomAdapter(Activity activity, ArrayList<String> tempTitle) {
mActivity = activity;
items = tempTitle;

inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public final int getCount() {

return items.size();

}

@Override
public final Object getItem(int position) {
return items.get(position);
}

@Override
public final long getItemId(int position) {

return position;
}

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

View v = null;

v = inflater.inflate(R.layout.item, null);

Button tv = (Button) v.findViewById(R.id.button);
tv.setText(items.get(position));

return v;
}

}


gridview.xml



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@android:color/white"
android:orientation="vertical" >

<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="20dip"
android:gravity="center"
android:horizontalSpacing="2dp"
android:verticalSpacing="2dp"
android:numColumns="20"
android:stretchMode="columnWidth" >
</GridView>

</LinearLayout>


item.xml



<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button"
android:layout_width="80dip"
android:layout_height="80dip"
android:textSize="10sp"
android:background="@android:color/holo_blue_light"
android:textColor="@android:color/black"
android:textStyle="bold" />


GridViewActivity



public class GridViewActivity extends Activity {



private GridView list;
ArrayList<String> data = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
for (int i = 0; i < 10; i++) {
for(int j=0;j<20;j++)
data.add(i+"-"+j);
}
GridViewCustomAdapter adapter = new GridViewCustomAdapter(this, data);

list = (GridView) findViewById(R.id.grid_view);
list.setAdapter(adapter);

}


}



output :



enter image description here






share|improve this answer
























  • thank you for the answer,i knw this but the gridview only show the column as u specified within the screen width which is lesser it wil take that so that is an issue with me

    – kukku
    Oct 22 '14 at 13:05



















2














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






share|improve this answer































    0














    hi this is one code that i've created to do one matrix of buttons "x"



    package com.example.andre.aplicacaocalibracaov2;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.GridView;
    import android.widget.Toast;

    import java.util.ArrayList;
    import java.util.List;

    public class MainActivity extends AppCompatActivity {
    GridView gridView;
    List<String> lstSource = new ArrayList<>();

    String array_caracteres={
    "x"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setUpList();
    GridView gridView = (GridView) findViewById(R.id.gridView);
    //é a classe gridadapter
    GridAdapter adapter = new GridAdapter(lstSource,MainActivity.this);
    gridView.setAdapter(adapter);


    }
    public void setUpList() {
    for (String item : array_caracteres)
    for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++)
    lstSource.add(item);
    }
    }
    }


    GridAdapter.java

    package com.example.andre.aplicacaocalibracaov2;

    import android.content.Context;
    import android.graphics.Color;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.GridLayout;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.Toast;

    import java.util.List;

    public class GridAdapter extends BaseAdapter {

    private Context context;
    List<String> lstSource;

    public GridAdapter(List<String>lstSource, Context context){
    this.lstSource = lstSource;
    this.context=context;


    }
    @Override
    public int getCount() {

    return lstSource.size();
    }

    @Override
    public Object getItem(int position) {

    return lstSource.get(position);
    }

    @Override
    public long getItemId(int position) {
    return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    final Button button;
    if(convertView ==null){
    button = new Button(context);
    button.setLayoutParams(new GridView.LayoutParams(85,85));
    button.setPadding(8,8,8,8);
    button.setText(lstSource.get(position));
    button.setBackgroundColor(Color.YELLOW);
    button.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
    Toast.makeText(context,button.getText().toString(),Toast.LENGTH_SHORT).show();
    }
    });


    }
    else
    button=(Button)convertView;
    return button;
    }
    }


    the xml



    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity"

    >

    <GridView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gridView"
    android:columnWidth="30dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="4dp"
    android:horizontalSpacing="4dp"
    android:gravity="center"
    android:layout_centerInParent="true"
    />


    </RelativeLayout>


    Good luck !!






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26501752%2fhow-to-create-custom-gridview-like-matrix-structure-in-android%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      try this:



      GridViewCustomAdapter



      import java.util.ArrayList;

      import android.app.Activity;
      import android.content.Context;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.BaseAdapter;
      import android.widget.Button;

      public class GridViewCustomAdapter extends BaseAdapter {

      ArrayList<String> items;

      static Activity mActivity;

      private static LayoutInflater inflater = null;

      public GridViewCustomAdapter(Activity activity, ArrayList<String> tempTitle) {
      mActivity = activity;
      items = tempTitle;

      inflater = (LayoutInflater) activity
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }

      @Override
      public final int getCount() {

      return items.size();

      }

      @Override
      public final Object getItem(int position) {
      return items.get(position);
      }

      @Override
      public final long getItemId(int position) {

      return position;
      }

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

      View v = null;

      v = inflater.inflate(R.layout.item, null);

      Button tv = (Button) v.findViewById(R.id.button);
      tv.setText(items.get(position));

      return v;
      }

      }


      gridview.xml



      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center"
      android:background="@android:color/white"
      android:orientation="vertical" >

      <GridView
      android:id="@+id/grid_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:columnWidth="20dip"
      android:gravity="center"
      android:horizontalSpacing="2dp"
      android:verticalSpacing="2dp"
      android:numColumns="20"
      android:stretchMode="columnWidth" >
      </GridView>

      </LinearLayout>


      item.xml



      <?xml version="1.0" encoding="utf-8"?>
      <Button xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/button"
      android:layout_width="80dip"
      android:layout_height="80dip"
      android:textSize="10sp"
      android:background="@android:color/holo_blue_light"
      android:textColor="@android:color/black"
      android:textStyle="bold" />


      GridViewActivity



      public class GridViewActivity extends Activity {



      private GridView list;
      ArrayList<String> data = new ArrayList<>();


      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.gridview);
      for (int i = 0; i < 10; i++) {
      for(int j=0;j<20;j++)
      data.add(i+"-"+j);
      }
      GridViewCustomAdapter adapter = new GridViewCustomAdapter(this, data);

      list = (GridView) findViewById(R.id.grid_view);
      list.setAdapter(adapter);

      }


      }



      output :



      enter image description here






      share|improve this answer
























      • thank you for the answer,i knw this but the gridview only show the column as u specified within the screen width which is lesser it wil take that so that is an issue with me

        – kukku
        Oct 22 '14 at 13:05
















      5














      try this:



      GridViewCustomAdapter



      import java.util.ArrayList;

      import android.app.Activity;
      import android.content.Context;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.BaseAdapter;
      import android.widget.Button;

      public class GridViewCustomAdapter extends BaseAdapter {

      ArrayList<String> items;

      static Activity mActivity;

      private static LayoutInflater inflater = null;

      public GridViewCustomAdapter(Activity activity, ArrayList<String> tempTitle) {
      mActivity = activity;
      items = tempTitle;

      inflater = (LayoutInflater) activity
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }

      @Override
      public final int getCount() {

      return items.size();

      }

      @Override
      public final Object getItem(int position) {
      return items.get(position);
      }

      @Override
      public final long getItemId(int position) {

      return position;
      }

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

      View v = null;

      v = inflater.inflate(R.layout.item, null);

      Button tv = (Button) v.findViewById(R.id.button);
      tv.setText(items.get(position));

      return v;
      }

      }


      gridview.xml



      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center"
      android:background="@android:color/white"
      android:orientation="vertical" >

      <GridView
      android:id="@+id/grid_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:columnWidth="20dip"
      android:gravity="center"
      android:horizontalSpacing="2dp"
      android:verticalSpacing="2dp"
      android:numColumns="20"
      android:stretchMode="columnWidth" >
      </GridView>

      </LinearLayout>


      item.xml



      <?xml version="1.0" encoding="utf-8"?>
      <Button xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/button"
      android:layout_width="80dip"
      android:layout_height="80dip"
      android:textSize="10sp"
      android:background="@android:color/holo_blue_light"
      android:textColor="@android:color/black"
      android:textStyle="bold" />


      GridViewActivity



      public class GridViewActivity extends Activity {



      private GridView list;
      ArrayList<String> data = new ArrayList<>();


      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.gridview);
      for (int i = 0; i < 10; i++) {
      for(int j=0;j<20;j++)
      data.add(i+"-"+j);
      }
      GridViewCustomAdapter adapter = new GridViewCustomAdapter(this, data);

      list = (GridView) findViewById(R.id.grid_view);
      list.setAdapter(adapter);

      }


      }



      output :



      enter image description here






      share|improve this answer
























      • thank you for the answer,i knw this but the gridview only show the column as u specified within the screen width which is lesser it wil take that so that is an issue with me

        – kukku
        Oct 22 '14 at 13:05














      5












      5








      5







      try this:



      GridViewCustomAdapter



      import java.util.ArrayList;

      import android.app.Activity;
      import android.content.Context;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.BaseAdapter;
      import android.widget.Button;

      public class GridViewCustomAdapter extends BaseAdapter {

      ArrayList<String> items;

      static Activity mActivity;

      private static LayoutInflater inflater = null;

      public GridViewCustomAdapter(Activity activity, ArrayList<String> tempTitle) {
      mActivity = activity;
      items = tempTitle;

      inflater = (LayoutInflater) activity
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }

      @Override
      public final int getCount() {

      return items.size();

      }

      @Override
      public final Object getItem(int position) {
      return items.get(position);
      }

      @Override
      public final long getItemId(int position) {

      return position;
      }

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

      View v = null;

      v = inflater.inflate(R.layout.item, null);

      Button tv = (Button) v.findViewById(R.id.button);
      tv.setText(items.get(position));

      return v;
      }

      }


      gridview.xml



      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center"
      android:background="@android:color/white"
      android:orientation="vertical" >

      <GridView
      android:id="@+id/grid_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:columnWidth="20dip"
      android:gravity="center"
      android:horizontalSpacing="2dp"
      android:verticalSpacing="2dp"
      android:numColumns="20"
      android:stretchMode="columnWidth" >
      </GridView>

      </LinearLayout>


      item.xml



      <?xml version="1.0" encoding="utf-8"?>
      <Button xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/button"
      android:layout_width="80dip"
      android:layout_height="80dip"
      android:textSize="10sp"
      android:background="@android:color/holo_blue_light"
      android:textColor="@android:color/black"
      android:textStyle="bold" />


      GridViewActivity



      public class GridViewActivity extends Activity {



      private GridView list;
      ArrayList<String> data = new ArrayList<>();


      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.gridview);
      for (int i = 0; i < 10; i++) {
      for(int j=0;j<20;j++)
      data.add(i+"-"+j);
      }
      GridViewCustomAdapter adapter = new GridViewCustomAdapter(this, data);

      list = (GridView) findViewById(R.id.grid_view);
      list.setAdapter(adapter);

      }


      }



      output :



      enter image description here






      share|improve this answer













      try this:



      GridViewCustomAdapter



      import java.util.ArrayList;

      import android.app.Activity;
      import android.content.Context;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.BaseAdapter;
      import android.widget.Button;

      public class GridViewCustomAdapter extends BaseAdapter {

      ArrayList<String> items;

      static Activity mActivity;

      private static LayoutInflater inflater = null;

      public GridViewCustomAdapter(Activity activity, ArrayList<String> tempTitle) {
      mActivity = activity;
      items = tempTitle;

      inflater = (LayoutInflater) activity
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }

      @Override
      public final int getCount() {

      return items.size();

      }

      @Override
      public final Object getItem(int position) {
      return items.get(position);
      }

      @Override
      public final long getItemId(int position) {

      return position;
      }

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

      View v = null;

      v = inflater.inflate(R.layout.item, null);

      Button tv = (Button) v.findViewById(R.id.button);
      tv.setText(items.get(position));

      return v;
      }

      }


      gridview.xml



      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center"
      android:background="@android:color/white"
      android:orientation="vertical" >

      <GridView
      android:id="@+id/grid_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:columnWidth="20dip"
      android:gravity="center"
      android:horizontalSpacing="2dp"
      android:verticalSpacing="2dp"
      android:numColumns="20"
      android:stretchMode="columnWidth" >
      </GridView>

      </LinearLayout>


      item.xml



      <?xml version="1.0" encoding="utf-8"?>
      <Button xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/button"
      android:layout_width="80dip"
      android:layout_height="80dip"
      android:textSize="10sp"
      android:background="@android:color/holo_blue_light"
      android:textColor="@android:color/black"
      android:textStyle="bold" />


      GridViewActivity



      public class GridViewActivity extends Activity {



      private GridView list;
      ArrayList<String> data = new ArrayList<>();


      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.gridview);
      for (int i = 0; i < 10; i++) {
      for(int j=0;j<20;j++)
      data.add(i+"-"+j);
      }
      GridViewCustomAdapter adapter = new GridViewCustomAdapter(this, data);

      list = (GridView) findViewById(R.id.grid_view);
      list.setAdapter(adapter);

      }


      }



      output :



      enter image description here







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Oct 22 '14 at 10:44









      RustamRustam

      5,91111825




      5,91111825













      • thank you for the answer,i knw this but the gridview only show the column as u specified within the screen width which is lesser it wil take that so that is an issue with me

        – kukku
        Oct 22 '14 at 13:05



















      • thank you for the answer,i knw this but the gridview only show the column as u specified within the screen width which is lesser it wil take that so that is an issue with me

        – kukku
        Oct 22 '14 at 13:05

















      thank you for the answer,i knw this but the gridview only show the column as u specified within the screen width which is lesser it wil take that so that is an issue with me

      – kukku
      Oct 22 '14 at 13:05





      thank you for the answer,i knw this but the gridview only show the column as u specified within the screen width which is lesser it wil take that so that is an issue with me

      – kukku
      Oct 22 '14 at 13:05













      2














      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






      share|improve this answer




























        2














        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






        share|improve this answer


























          2












          2








          2







          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






          share|improve this answer













          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 23 '14 at 10:25









          kukkukukku

          416317




          416317























              0














              hi this is one code that i've created to do one matrix of buttons "x"



              package com.example.andre.aplicacaocalibracaov2;

              import android.support.v7.app.AppCompatActivity;
              import android.os.Bundle;
              import android.view.View;
              import android.widget.AdapterView;
              import android.widget.GridView;
              import android.widget.Toast;

              import java.util.ArrayList;
              import java.util.List;

              public class MainActivity extends AppCompatActivity {
              GridView gridView;
              List<String> lstSource = new ArrayList<>();

              String array_caracteres={
              "x"
              };

              @Override
              protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              setUpList();
              GridView gridView = (GridView) findViewById(R.id.gridView);
              //é a classe gridadapter
              GridAdapter adapter = new GridAdapter(lstSource,MainActivity.this);
              gridView.setAdapter(adapter);


              }
              public void setUpList() {
              for (String item : array_caracteres)
              for (int i = 0; i < 10; i++) {
              for (int j = 0; j < 10; j++)
              lstSource.add(item);
              }
              }
              }


              GridAdapter.java

              package com.example.andre.aplicacaocalibracaov2;

              import android.content.Context;
              import android.graphics.Color;
              import android.view.LayoutInflater;
              import android.view.View;
              import android.view.ViewGroup;
              import android.widget.BaseAdapter;
              import android.widget.Button;
              import android.widget.GridLayout;
              import android.widget.GridView;
              import android.widget.ImageView;
              import android.widget.Toast;

              import java.util.List;

              public class GridAdapter extends BaseAdapter {

              private Context context;
              List<String> lstSource;

              public GridAdapter(List<String>lstSource, Context context){
              this.lstSource = lstSource;
              this.context=context;


              }
              @Override
              public int getCount() {

              return lstSource.size();
              }

              @Override
              public Object getItem(int position) {

              return lstSource.get(position);
              }

              @Override
              public long getItemId(int position) {
              return position;
              }

              @Override
              public View getView(int position, View convertView, ViewGroup parent) {
              final Button button;
              if(convertView ==null){
              button = new Button(context);
              button.setLayoutParams(new GridView.LayoutParams(85,85));
              button.setPadding(8,8,8,8);
              button.setText(lstSource.get(position));
              button.setBackgroundColor(Color.YELLOW);
              button.setOnClickListener(new View.OnClickListener(){
              public void onClick(View v){
              Toast.makeText(context,button.getText().toString(),Toast.LENGTH_SHORT).show();
              }
              });


              }
              else
              button=(Button)convertView;
              return button;
              }
              }


              the xml



              <?xml version="1.0" encoding="utf-8"?>
              <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingTop="16dp"
              android:paddingLeft="16dp"
              android:paddingRight="16dp"
              android:paddingBottom="16dp"
              tools:context=".MainActivity"

              >

              <GridView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/gridView"
              android:columnWidth="30dp"
              android:numColumns="auto_fit"
              android:stretchMode="columnWidth"
              android:verticalSpacing="4dp"
              android:horizontalSpacing="4dp"
              android:gravity="center"
              android:layout_centerInParent="true"
              />


              </RelativeLayout>


              Good luck !!






              share|improve this answer




























                0














                hi this is one code that i've created to do one matrix of buttons "x"



                package com.example.andre.aplicacaocalibracaov2;

                import android.support.v7.app.AppCompatActivity;
                import android.os.Bundle;
                import android.view.View;
                import android.widget.AdapterView;
                import android.widget.GridView;
                import android.widget.Toast;

                import java.util.ArrayList;
                import java.util.List;

                public class MainActivity extends AppCompatActivity {
                GridView gridView;
                List<String> lstSource = new ArrayList<>();

                String array_caracteres={
                "x"
                };

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                setUpList();
                GridView gridView = (GridView) findViewById(R.id.gridView);
                //é a classe gridadapter
                GridAdapter adapter = new GridAdapter(lstSource,MainActivity.this);
                gridView.setAdapter(adapter);


                }
                public void setUpList() {
                for (String item : array_caracteres)
                for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++)
                lstSource.add(item);
                }
                }
                }


                GridAdapter.java

                package com.example.andre.aplicacaocalibracaov2;

                import android.content.Context;
                import android.graphics.Color;
                import android.view.LayoutInflater;
                import android.view.View;
                import android.view.ViewGroup;
                import android.widget.BaseAdapter;
                import android.widget.Button;
                import android.widget.GridLayout;
                import android.widget.GridView;
                import android.widget.ImageView;
                import android.widget.Toast;

                import java.util.List;

                public class GridAdapter extends BaseAdapter {

                private Context context;
                List<String> lstSource;

                public GridAdapter(List<String>lstSource, Context context){
                this.lstSource = lstSource;
                this.context=context;


                }
                @Override
                public int getCount() {

                return lstSource.size();
                }

                @Override
                public Object getItem(int position) {

                return lstSource.get(position);
                }

                @Override
                public long getItemId(int position) {
                return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                final Button button;
                if(convertView ==null){
                button = new Button(context);
                button.setLayoutParams(new GridView.LayoutParams(85,85));
                button.setPadding(8,8,8,8);
                button.setText(lstSource.get(position));
                button.setBackgroundColor(Color.YELLOW);
                button.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v){
                Toast.makeText(context,button.getText().toString(),Toast.LENGTH_SHORT).show();
                }
                });


                }
                else
                button=(Button)convertView;
                return button;
                }
                }


                the xml



                <?xml version="1.0" encoding="utf-8"?>
                <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingTop="16dp"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:paddingBottom="16dp"
                tools:context=".MainActivity"

                >

                <GridView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/gridView"
                android:columnWidth="30dp"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth"
                android:verticalSpacing="4dp"
                android:horizontalSpacing="4dp"
                android:gravity="center"
                android:layout_centerInParent="true"
                />


                </RelativeLayout>


                Good luck !!






                share|improve this answer


























                  0












                  0








                  0







                  hi this is one code that i've created to do one matrix of buttons "x"



                  package com.example.andre.aplicacaocalibracaov2;

                  import android.support.v7.app.AppCompatActivity;
                  import android.os.Bundle;
                  import android.view.View;
                  import android.widget.AdapterView;
                  import android.widget.GridView;
                  import android.widget.Toast;

                  import java.util.ArrayList;
                  import java.util.List;

                  public class MainActivity extends AppCompatActivity {
                  GridView gridView;
                  List<String> lstSource = new ArrayList<>();

                  String array_caracteres={
                  "x"
                  };

                  @Override
                  protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  setUpList();
                  GridView gridView = (GridView) findViewById(R.id.gridView);
                  //é a classe gridadapter
                  GridAdapter adapter = new GridAdapter(lstSource,MainActivity.this);
                  gridView.setAdapter(adapter);


                  }
                  public void setUpList() {
                  for (String item : array_caracteres)
                  for (int i = 0; i < 10; i++) {
                  for (int j = 0; j < 10; j++)
                  lstSource.add(item);
                  }
                  }
                  }


                  GridAdapter.java

                  package com.example.andre.aplicacaocalibracaov2;

                  import android.content.Context;
                  import android.graphics.Color;
                  import android.view.LayoutInflater;
                  import android.view.View;
                  import android.view.ViewGroup;
                  import android.widget.BaseAdapter;
                  import android.widget.Button;
                  import android.widget.GridLayout;
                  import android.widget.GridView;
                  import android.widget.ImageView;
                  import android.widget.Toast;

                  import java.util.List;

                  public class GridAdapter extends BaseAdapter {

                  private Context context;
                  List<String> lstSource;

                  public GridAdapter(List<String>lstSource, Context context){
                  this.lstSource = lstSource;
                  this.context=context;


                  }
                  @Override
                  public int getCount() {

                  return lstSource.size();
                  }

                  @Override
                  public Object getItem(int position) {

                  return lstSource.get(position);
                  }

                  @Override
                  public long getItemId(int position) {
                  return position;
                  }

                  @Override
                  public View getView(int position, View convertView, ViewGroup parent) {
                  final Button button;
                  if(convertView ==null){
                  button = new Button(context);
                  button.setLayoutParams(new GridView.LayoutParams(85,85));
                  button.setPadding(8,8,8,8);
                  button.setText(lstSource.get(position));
                  button.setBackgroundColor(Color.YELLOW);
                  button.setOnClickListener(new View.OnClickListener(){
                  public void onClick(View v){
                  Toast.makeText(context,button.getText().toString(),Toast.LENGTH_SHORT).show();
                  }
                  });


                  }
                  else
                  button=(Button)convertView;
                  return button;
                  }
                  }


                  the xml



                  <?xml version="1.0" encoding="utf-8"?>
                  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:app="http://schemas.android.com/apk/res-auto"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:paddingTop="16dp"
                  android:paddingLeft="16dp"
                  android:paddingRight="16dp"
                  android:paddingBottom="16dp"
                  tools:context=".MainActivity"

                  >

                  <GridView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:id="@+id/gridView"
                  android:columnWidth="30dp"
                  android:numColumns="auto_fit"
                  android:stretchMode="columnWidth"
                  android:verticalSpacing="4dp"
                  android:horizontalSpacing="4dp"
                  android:gravity="center"
                  android:layout_centerInParent="true"
                  />


                  </RelativeLayout>


                  Good luck !!






                  share|improve this answer













                  hi this is one code that i've created to do one matrix of buttons "x"



                  package com.example.andre.aplicacaocalibracaov2;

                  import android.support.v7.app.AppCompatActivity;
                  import android.os.Bundle;
                  import android.view.View;
                  import android.widget.AdapterView;
                  import android.widget.GridView;
                  import android.widget.Toast;

                  import java.util.ArrayList;
                  import java.util.List;

                  public class MainActivity extends AppCompatActivity {
                  GridView gridView;
                  List<String> lstSource = new ArrayList<>();

                  String array_caracteres={
                  "x"
                  };

                  @Override
                  protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  setUpList();
                  GridView gridView = (GridView) findViewById(R.id.gridView);
                  //é a classe gridadapter
                  GridAdapter adapter = new GridAdapter(lstSource,MainActivity.this);
                  gridView.setAdapter(adapter);


                  }
                  public void setUpList() {
                  for (String item : array_caracteres)
                  for (int i = 0; i < 10; i++) {
                  for (int j = 0; j < 10; j++)
                  lstSource.add(item);
                  }
                  }
                  }


                  GridAdapter.java

                  package com.example.andre.aplicacaocalibracaov2;

                  import android.content.Context;
                  import android.graphics.Color;
                  import android.view.LayoutInflater;
                  import android.view.View;
                  import android.view.ViewGroup;
                  import android.widget.BaseAdapter;
                  import android.widget.Button;
                  import android.widget.GridLayout;
                  import android.widget.GridView;
                  import android.widget.ImageView;
                  import android.widget.Toast;

                  import java.util.List;

                  public class GridAdapter extends BaseAdapter {

                  private Context context;
                  List<String> lstSource;

                  public GridAdapter(List<String>lstSource, Context context){
                  this.lstSource = lstSource;
                  this.context=context;


                  }
                  @Override
                  public int getCount() {

                  return lstSource.size();
                  }

                  @Override
                  public Object getItem(int position) {

                  return lstSource.get(position);
                  }

                  @Override
                  public long getItemId(int position) {
                  return position;
                  }

                  @Override
                  public View getView(int position, View convertView, ViewGroup parent) {
                  final Button button;
                  if(convertView ==null){
                  button = new Button(context);
                  button.setLayoutParams(new GridView.LayoutParams(85,85));
                  button.setPadding(8,8,8,8);
                  button.setText(lstSource.get(position));
                  button.setBackgroundColor(Color.YELLOW);
                  button.setOnClickListener(new View.OnClickListener(){
                  public void onClick(View v){
                  Toast.makeText(context,button.getText().toString(),Toast.LENGTH_SHORT).show();
                  }
                  });


                  }
                  else
                  button=(Button)convertView;
                  return button;
                  }
                  }


                  the xml



                  <?xml version="1.0" encoding="utf-8"?>
                  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:app="http://schemas.android.com/apk/res-auto"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:paddingTop="16dp"
                  android:paddingLeft="16dp"
                  android:paddingRight="16dp"
                  android:paddingBottom="16dp"
                  tools:context=".MainActivity"

                  >

                  <GridView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:id="@+id/gridView"
                  android:columnWidth="30dp"
                  android:numColumns="auto_fit"
                  android:stretchMode="columnWidth"
                  android:verticalSpacing="4dp"
                  android:horizontalSpacing="4dp"
                  android:gravity="center"
                  android:layout_centerInParent="true"
                  />


                  </RelativeLayout>


                  Good luck !!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 23:30









                  AndréAndré

                  145




                  145






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26501752%2fhow-to-create-custom-gridview-like-matrix-structure-in-android%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python