Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **activity_main.xml**
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/cream_background" // #F8F9FA
- android:orientation="vertical">
- <include layout="@layout/tool_bar"/>
- <TextView
- android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:layout_marginTop="50dp"
- android:gravity="center_horizontal"
- android:text="WELCOME TO THE GRID"
- android:textSize="36sp"/>
- <TextView
- android:id="@+id/subTitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:layout_marginBottom="10dp"
- android:layout_marginTop="15dp"
- android:gravity="center_horizontal"
- android:text="Please Select an Image"
- android:textSize="24sp"/>
- <TextView
- android:id="@+id/description"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:gravity="center_horizontal"
- android:text="Who has two index fingers and likes to press buttons? You do!"/>
- <GridView
- android:id="@+id/sceneGrid"
- android:layout_marginTop="15dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:verticalSpacing="15dp"
- android:numColumns="5" />
- </LinearLayout>
- **custom_grid.xml**
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:orientation="horizontal">
- <ImageView
- android:id="@+id/gridItem"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/button_ripple"
- android:gravity="center_horizontal"/>
- </LinearLayout>
- **CustomGridAdapter.java**
- public class CustomGridAdapter extends BaseAdapter {
- private Context mContext;
- private ArrayList<String> mBitmapList= null;
- private LayoutInflater mInflater;
- public CustomGridAdapter(Context context, ArrayList<String> bitmapList) {
- setContext(context);
- setBitmapList(bitmapList);
- mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- public void setContext(Context context) {
- mContext = context;
- }
- public void setBitmapList(ArrayList<String> bitmapList) {
- mBitmapList= bitmapList;
- }
- @Override
- public int getCount() {
- return mBitmapList.size();
- }
- @Override
- public Object getItem(int position) {
- return mBitmapList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- if (convertView == null) {
- convertView = mInflater.inflate(R.layout.custom_grid, null);
- }
- ImageView image = (ImageView) convertView.findViewById(R.id.gridItem);
- // set your image however you like, from wherever your images are. Mine were from the external storage
- String path = mBitmapList.get(position);
- Bitmap thumbnail = getBitmap(path);
- image.setImageBitmap(thumbnail);
- return convertView;
- }
- public Bitmap getBitmap(String imagePath) {
- Bitmap bmp = null;
- try {
- imagePath = imagePath.replace("\\", "/");
- FileInputStream inputStream = new FileInputStream(imagePath);
- bmp = BitmapFactory.decodeStream(inputStream);
- if (bmp != null) {
- bmp = Bitmap.createScaledBitmap(bmp, 300, 200, true);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return bmp;
- }
- }
- **button_ripple.xml**
- <ripple
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:color="@color/cream_background">
- <!-- Pressed -->
- <item android:state_pressed="true" android:drawable="@color/button_selected" />
- <!-- Selected -->
- <item android:state_selected="true" android:drawable="@color/button_selected" />
- <!-- Focus -->
- <item android:state_focused="true" android:drawable="@color/button_selected" />
- <!-- Default #00000000 -->
- <item android:drawable="@color/transparent"/>
- </ripple>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement