Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2.  
  3. static final String[] alphabets = new String[] {
  4. "A", "B", "C", "D", "E",
  5. "F", "G", "H", "I", "J",
  6. "K", "L", "M", "N", "O",
  7. "P", "Q", "R", "S", "T",
  8. "U", "V", "W", "X"};
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13.  
  14. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
  15. android.R.layout.simple_list_item_1, alphabets);
  16.  
  17. // create a RelativeLayout
  18. RelativeLayout relativeLayout = new RelativeLayout(this);
  19.  
  20. // define the RelativeLayout layout parameters.
  21. RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(
  22. RelativeLayout.LayoutParams.FILL_PARENT,
  23. RelativeLayout.LayoutParams.FILL_PARENT);
  24.  
  25. // create a gridview
  26. GridView gridView= new GridView(this);
  27.  
  28. gridView.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  29. gridView.setNumColumns(4);
  30.  
  31. gridView.setMinimumHeight(300);
  32. gridView.setMinimumWidth(300);
  33. gridView.setColumnWidth(300);
  34.  
  35. gridView.setAdapter(adapter);
  36. relativeLayout.setPadding(50, 50, 50, 50);
  37. // Adding the gridview to the RelativeLayout as a child
  38. relativeLayout.addView(gridView);
  39.  
  40. // set the RelativeLayout as our content view
  41. setContentView(relativeLayout, relativeLayoutParams);
  42.  
  43.  
  44.  
  45. }
  46.  
  47. public static class CustomArrayAdapter extends ArrayAdapter<String> {
  48.  
  49. private List<String> strings;
  50.  
  51. /**
  52. *
  53. * @param context
  54. * @param resource
  55. * @param objects
  56. * your strings to show
  57. */
  58. public CustomArrayAdapter(Context context, int resource,
  59. List<java.lang.String> objects) {
  60. super(context, resource, objects);
  61. // TODO Auto-generated constructor stub
  62. }
  63.  
  64. /**
  65. * Another constructor that takes Array instead of List
  66. *
  67. * @param context
  68. * @param resource
  69. * @param objects
  70. * your strings to show
  71. */
  72. public CustomArrayAdapter(Context context, int resource,
  73. String[] objects) {
  74. super(context, resource, objects);
  75. // TODO Auto-generated constructor stub
  76. }
  77.  
  78. @Override
  79. public View getView(int position, View convertView, ViewGroup parent) {
  80. // TODO Auto-generated method stub
  81.  
  82. LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  83. View gridItem = inflater.inflate(R.layout.grid_view_item, parent, false);
  84. TextView tv = (TextView) gridItem.findViewById(R.id.item);
  85. tv.setText((CharSequence) strings.get(position));
  86.  
  87. return gridItem;
  88.  
  89. }
  90.  
  91. }
  92.  
  93. <?xml version="1.0" encoding="utf-8"?>
  94. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  95. android:id="@+id/item_container"
  96. android:layout_width="match_parent"
  97. android:layout_height="match_parent"
  98. android:orientation="vertical" >
  99.  
  100. <TextView
  101. android:id="@+id/item"
  102. android:textColor="#FFDDFF99"
  103. android:layout_width="200dp"
  104. android:layout_height="200dp"
  105. android:layout_margin="5dp"
  106. android:text="Item" />
  107.  
  108.  
  109. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement