Guest User

Untitled

a guest
Jan 8th, 2015
429
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. **activity_main.xml**
  2.  
  3. <LinearLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="@color/cream_background" // #F8F9FA
  8. android:orientation="vertical">
  9.  
  10. <include layout="@layout/tool_bar"/>
  11.  
  12. <TextView
  13. android:id="@+id/title"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_gravity="center_horizontal"
  17. android:layout_marginTop="50dp"
  18. android:gravity="center_horizontal"
  19. android:text="WELCOME TO THE GRID"
  20. android:textSize="36sp"/>
  21.  
  22.  
  23. <TextView
  24. android:id="@+id/subTitle"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_gravity="center_horizontal"
  28. android:layout_marginBottom="10dp"
  29. android:layout_marginTop="15dp"
  30. android:gravity="center_horizontal"
  31. android:text="Please Select an Image"
  32. android:textSize="24sp"/>
  33.  
  34. <TextView
  35. android:id="@+id/description"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_gravity="center_horizontal"
  39. android:gravity="center_horizontal"
  40. android:text="Who has two index fingers and likes to press buttons? You do!"/>
  41.  
  42. <GridView
  43. android:id="@+id/sceneGrid"
  44. android:layout_marginTop="15dp"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:verticalSpacing="15dp"
  48. android:numColumns="5" />
  49.  
  50. </LinearLayout>
  51.  
  52. **custom_grid.xml**
  53.  
  54. <?xml version="1.0" encoding="utf-8"?>
  55. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:gravity="center_horizontal"
  59. android:orientation="horizontal">
  60.  
  61. <ImageView
  62. android:id="@+id/gridItem"
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content"
  65. android:background="@drawable/button_ripple"
  66. android:gravity="center_horizontal"/>
  67.  
  68. </LinearLayout>
  69.  
  70. **CustomGridAdapter.java**
  71.  
  72. public class CustomGridAdapter extends BaseAdapter {
  73. private Context mContext;
  74. private ArrayList<String> mBitmapList= null;
  75. private LayoutInflater mInflater;
  76.  
  77. public CustomGridAdapter(Context context, ArrayList<String> bitmapList) {
  78. setContext(context);
  79. setBitmapList(bitmapList);
  80. mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  81. }
  82.  
  83. public void setContext(Context context) {
  84. mContext = context;
  85. }
  86.  
  87. public void setBitmapList(ArrayList<String> bitmapList) {
  88. mBitmapList= bitmapList;
  89. }
  90.  
  91. @Override
  92. public int getCount() {
  93. return mBitmapList.size();
  94. }
  95.  
  96. @Override
  97. public Object getItem(int position) {
  98. return mBitmapList.get(position);
  99. }
  100.  
  101. @Override
  102. public long getItemId(int position) {
  103. return position;
  104. }
  105.  
  106. @Override
  107. public View getView(int position, View convertView, ViewGroup parent) {
  108. if (convertView == null) {
  109. convertView = mInflater.inflate(R.layout.custom_grid, null);
  110. }
  111.  
  112. ImageView image = (ImageView) convertView.findViewById(R.id.gridItem);
  113. // set your image however you like, from wherever your images are. Mine were from the external storage
  114. String path = mBitmapList.get(position);
  115. Bitmap thumbnail = getBitmap(path);
  116. image.setImageBitmap(thumbnail);
  117.  
  118. return convertView;
  119. }
  120.  
  121. public Bitmap getBitmap(String imagePath) {
  122. Bitmap bmp = null;
  123. try {
  124. imagePath = imagePath.replace("\\", "/");
  125. FileInputStream inputStream = new FileInputStream(imagePath);
  126. bmp = BitmapFactory.decodeStream(inputStream);
  127.  
  128. if (bmp != null) {
  129. bmp = Bitmap.createScaledBitmap(bmp, 300, 200, true);
  130. }
  131. } catch (IOException e) {
  132. e.printStackTrace();
  133. }
  134.  
  135. return bmp;
  136. }
  137. }
  138.  
  139. **button_ripple.xml**
  140.  
  141. <ripple
  142. xmlns:android="http://schemas.android.com/apk/res/android"
  143. android:color="@color/cream_background">
  144. <!-- Pressed -->
  145. <item android:state_pressed="true" android:drawable="@color/button_selected" />
  146. <!-- Selected -->
  147. <item android:state_selected="true" android:drawable="@color/button_selected" />
  148. <!-- Focus -->
  149. <item android:state_focused="true" android:drawable="@color/button_selected" />
  150. <!-- Default #00000000 -->
  151. <item android:drawable="@color/transparent"/>
  152. </ripple>
RAW Paste Data