In Android, like
ListView
, GridView
has attribute GridView.CHOICE_MODE_MULTIPLE_MODAL
. But I'm not recommend this solution. Through this post, I will customizing the GridView
items view and make it better than the default style.Customizing GridView items view
GridItemView.java
And it layout file:
package info.devexchanges.gridviewmultiplechoices;
import android.content.Context;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TextView;
public class GridItemView extends FrameLayout {
private TextView textView;
public GridItemView(Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.item_grid, this);
textView = (TextView) getRootView().findViewById(R.id.text);
}
public void display(String text, boolean isSelected) {
textView.setText(text);
display(isSelected);
}
public void display(boolean isSelected) {
textView.setBackgroundResource(isSelected ? R.drawable.green_square : R.drawable.gray_square);
}
}
item_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>
Customizing GridView adapter
GridViewAdapter.java
package info.devexchanges.gridviewmultiplechoices;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.List;
public class GridViewAdapter extends BaseAdapter {
private Activity activity;
private String[] strings;
public List selectedPositions;
public GridViewAdapter(String[] strings, Activity activity) {
this.strings = strings;
this.activity = activity;
selectedPositions = new ArrayList<>();
}
@Override
public int getCount() {
return strings.length;
}
@Override
public Object getItem(int position) {
return strings[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
GridItemView customView = (convertView == null) ? new GridItemView(activity) : (GridItemView) convertView;
customView.display(strings[position], selectedPositions.contains(position));
return customView;
}
}
Using in Activities
Activity
which contains only a GridView
and handling each item clicked event. Soure code of it is so simple:
MainActivity.java
And it's layout:package info.devexchanges.gridviewmultiplechoices;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private GridView gridView;
private View btnGo;
private ArrayList<String> selectedStrings;
private static final String[] numbers = new String[]{
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.grid);
btnGo = findViewById(R.id.button);
selectedStrings = new ArrayList<>();
final GridViewAdapter adapter = new GridViewAdapter(numbers, this);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int selectedIndex = adapter.selectedPositions.indexOf(position);
if (selectedIndex > -1) {
adapter.selectedPositions.remove(selectedIndex);
((GridItemView) v).display(false);
selectedStrings.remove((String) parent.getItemAtPosition(position));
} else {
adapter.selectedPositions.add(position);
((GridItemView) v).display(true);
selectedStrings.add((String) parent.getItemAtPosition(position));
}
}
});
//set listener for Button event
btnGo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SelectedItemsActivity.class);
intent.putStringArrayListExtra("SELECTED_LETTER", selectedStrings);
startActivity(intent);
}
});
}
}
As you see at the code above, when clicking a button in main activity, we'll go to other activity with an
ArrayList
of selected data. So, in this Activity
code, get data from Intent
and show it to view:
SelectedItemsActivity.java
After running, we have a grid view that user can make a multiple choices in it:package info.devexchanges.gridviewmultiplechoices;
import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class SelectedItemsActivity extends AppCompatActivity {
private TextView textView;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView = new TextView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(params);
textView.setPadding(10, 10, 10, 10);
textView.setTypeface(null, Typeface.BOLD);
textView.setText("You selected: ");
setContentView(textView);
//get data from intent
getIntentData();
}
@SuppressLint("SetTextI18n")
public void getIntentData() {
ArrayList<String> stringArrayList = getIntent().getStringArrayListExtra("SELECTED_LETTER");
assert stringArrayList != null;
if (stringArrayList.size() > 0) {
for (int i = 0; i < stringArrayList.size(); i++) {
if (i < stringArrayList.size() - 1) {
textView.setText(textView.getText() + stringArrayList.get(i) + ", ");
} else {
textView.setText(textView.getText() + stringArrayList.get(i) +".");
}
}
}
}
}
Clicking at "Go to Selected Activity":