Using card view dynamically in android

I am creating a page in android studio where I need to use the concept of cards and, where the card contains a person's image and the person name below the image and the cards has to appear in a grid, for example android marshmallow's contact list where each contact appears as a tile in a grid-view.

I tried different possible ways but unable to rectify this please help me on this. This is the Image of how the final output should look like.


          android:paddingTop="5dp" />

          android:text="Mark Linonel Jr"
          android:background="#00000000" />

This how I generated a card, but I need four more cards in a tiled shape that should be generated automatically based on number of people.

Thanks you.


Use a RecyclerView with a GridLayoutManager (Android RecyclerView) like this:

RecyclerView recyclerView = findViewById(;
GridLayoutManager layoutManager = new GridLayoutManager(context, 2);

Then make your own class MyAdapter which extends RecyclerView.Adapter and inflates your cardview.xml layout and set it to the RecyclerView:

MyAdapter adapter = new MyAdapter(dataset);

For a nice guide with detailed explanation on how to do this, see this link

You can easily done this by using GridView . You need a custom adapter class where you will populate your cardview .

This is sample code of adapter class

public class CustomGrid extends BaseAdapter{
    private Context mContext;
    private final String[] web;
    private final int[] Imageid;

    public CustomGrid(Context c,String[] web,int[] Imageid ) {
        mContext = c;
        this.Imageid = Imageid;
        this.web = web;

    public int getCount() {
        // TODO Auto-generated method stub
        return web.length;

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext

        if (convertView == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.grid_single, null);
            TextView textView = (TextView) grid.findViewById(;
            ImageView imageView = (ImageView)grid.findViewById(;
        } else {
            grid = (View) convertView;

        return grid;

call this adapter class from you activity like this


public class MainActivity extends Activity {
    GridView grid;
    String[] name = {
    int[] imageId = {
    protected void onCreate(Bundle savedInstanceState) {
        CustomGrid adapter = new CustomGrid(MainActivity.this, name, imageId);
                grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(MainActivity.this, "You Clicked at " +name[+ position], Toast.LENGTH_SHORT).show();

follow this tutorial link 1 , link 2

Need Your Help

What am I doing wrong with my drop down menu?

html5 css3

I am trying to create my first drop down navigation menu with CSS, I have always been slightly opposed to drop down menus, so I haven't really given this a try before now.

Is there a way to get aocolumns from the server in datatable editable?

jquery ajax datatables jquery-datatables-editor

So, I am trying to make one generic page for any table that is being requested by user. For that I am trying to fetch all data from server and not having anything hardcoded on the clientside.