How to create custom gridview (like matrix structure) in android
I want to create a matrix like gridview, for example 10x20 matrix
we want to specify the number of rows and column View, i.e. 10x20
if the screen is low it should scroll horizontally and vertically eg the image below describes the Matrix Gridview Each cell represents (0,0) (0,1) etc .... (1,0) (1,1) and etc.
How do you create this type of view? Advance Thank you .... !!!
source to share
This can be achieved with a GridLayout and dynamic cell creation
/ * * * Copyright 2012 Jess Anders * * Apache License Version 2.0 ("License"); * you may not use this file except in accordance with the License. * You can obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless otherwise provided by applicable law or agreed in writing, the software * is licensed to cover "AS IS "BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. * See the specific language license for permissions and * license restrictions. * /
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 a direct solution, but ... I think I need to customize the GridView which I do not currently know
source to share
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:
source to share