Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends Activity {
- static final String[] alphabets = 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"};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, alphabets);
- // create a RelativeLayout
- RelativeLayout relativeLayout = new RelativeLayout(this);
- // define the RelativeLayout layout parameters.
- RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(
- RelativeLayout.LayoutParams.FILL_PARENT,
- RelativeLayout.LayoutParams.FILL_PARENT);
- // create a gridview
- GridView gridView= new GridView(this);
- gridView.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
- gridView.setNumColumns(4);
- gridView.setMinimumHeight(300);
- gridView.setMinimumWidth(300);
- gridView.setColumnWidth(300);
- gridView.setAdapter(adapter);
- relativeLayout.setPadding(50, 50, 50, 50);
- // Adding the gridview to the RelativeLayout as a child
- relativeLayout.addView(gridView);
- // set the RelativeLayout as our content view
- setContentView(relativeLayout, relativeLayoutParams);
- }
- public static class CustomArrayAdapter extends ArrayAdapter<String> {
- private List<String> strings;
- /**
- *
- * @param context
- * @param resource
- * @param objects
- * your strings to show
- */
- public CustomArrayAdapter(Context context, int resource,
- List<java.lang.String> objects) {
- super(context, resource, objects);
- // TODO Auto-generated constructor stub
- }
- /**
- * Another constructor that takes Array instead of List
- *
- * @param context
- * @param resource
- * @param objects
- * your strings to show
- */
- public CustomArrayAdapter(Context context, int resource,
- String[] objects) {
- super(context, resource, objects);
- // TODO Auto-generated constructor stub
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View gridItem = inflater.inflate(R.layout.grid_view_item, parent, false);
- TextView tv = (TextView) gridItem.findViewById(R.id.item);
- tv.setText((CharSequence) strings.get(position));
- return gridItem;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/item_container"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/item"
- android:textColor="#FFDDFF99"
- android:layout_width="200dp"
- android:layout_height="200dp"
- android:layout_margin="5dp"
- android:text="Item" />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement