GridView with Gallery Android Application

Hello friends
today we are going to learn about grid view in android. so first we need to know about android gridview so. when we need to create a layout look like a gallery at that time grid layout used.
so without wasting time let's take an example of gridview in android. Into this example we are creating one gallery android application, we see all the images in grid formate like table or rows and columns base formate.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        android:layout_marginTop="50dp"
    tools:context=".MainActivity">

    <GridView
        android:id="@+id/gridview_gallary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="auto_fit"
        android:columnWidth="100dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:gravity="center"
        android:stretchMode="columnWidth" />
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView gridview_gallary = (GridView) findViewById(R.id.gridview_gallary);
        gridview_gallary.setAdapter(new ImgAdapter(this));
        gridview_gallary.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Intent i = new Intent(getApplicationContext(), ImgViewClass.class);
                i.putExtra("id", position);
                startActivity(i);
            }
        });
    }
}

Now, we are going to create on the image click image open in full view so below code for that. take a look.

img.view.xml

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView android:id="@+id/full_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>

ImgViewClass.java

public class ImgViewClass extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.img_view);
        Intent intent = getIntent();
        int position = intent.getExtras().getInt("id");
        ImgAdapter imageAdapter = new ImgAdapter(this);
        ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
        imageView.setImageResource(imageAdapter.gallaryimages[position]);
    }
}

ImgAdapter.java

public class ImgAdapter extends BaseAdapter {
    private Context context;
    public Integer[] gallaryimages = {
            R.drawable.image1, R.drawable.image2,
            R.drawable.image3, R.drawable.image4,
            R.drawable.image5, R.drawable.image1,
            R.drawable.image2, R.drawable.image3,
            R.drawable.image4, R.drawable.image5,

    };
    public ImgAdapter(Context c) {
        context = c;
    }
    @Override
    public int getCount() {
        return gallaryimages.length;
    }
    @Override
    public Object getItem(int position) {
        return gallaryimages[position];
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imgview = new ImageView(context);
        imgview.setImageResource(gallaryimages[position]);
        imgview.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imgview.setLayoutParams(new GridView.LayoutParams(300, 300));
        return imgview;
    }
}


Comments