Sahil Shokeen
Sahil Shokeen

Reputation: 336

Add grid titles to grid cards android

I am using CardView to images in grid and i want to add grid titles to images as in this image.Searched on internet but found simple adding of texts not this type of footer texts.

From google design site

Upvotes: 2

Views: 697

Answers (2)

CodingRat
CodingRat

Reputation: 1944

You need to create a card like this

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff0000">
    <ImageView
        android:id="@+id/overlayImage"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@drawable/education"
        />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="bottom"
        android:background="#70000000">
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/icon"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:src="@android:drawable/ic_dialog_info"
            />
        <TextView
            android:id="@+id/tvNewsContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@+id/icon"
            android:ellipsize="end"
            android:maxLines="3"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:text="Big CNG scam behind odd-even plan, says woman who threw ink at Kejriwal,plan, says woman who threw ink at Kejriwal"
            android:textColor="@color/white"
            android:textSize="@dimen/feed_name_size"
            android:textStyle="bold"
            android:gravity="center_vertical"

            />
    </RelativeLayout>

</FrameLayout>

your card will look like this

enter image description here

Upvotes: 1

Hemant Ukey
Hemant Ukey

Reputation: 328

set adapter to your grid view as given in the below code

public class FirstActivityAdapter extends BaseAdapter {

String[] result;
Context context;
int[] imageId;
Intent i;

private static LayoutInflater inflater = null;

public FirstActivityAdapter(Context mainActivity, String[] prgNameList, int[] prgImages) {

    result = prgNameList;
    context = mainActivity;
    imageId = prgImages;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
@Override
public int getCount() {
    return result.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

public class Holder {

    ImageView imageView;
    TextView textView;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Holder holder = new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.main_category_list_item, null);
    holder.imageView = (ImageView) rowView.findViewById(R.id.image_view_for_grid_view);
    holder.textView = (TextView) rowView.findViewById(R.id.text_view_for_image);
    holder.textView.setText(result[position]);
    holder.imageView.setImageResource(imageId[position]);
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (position) {
                case 0:
                  i = new Intent(context,SecondActivity.class);
                    context.startActivity(i);
                    break;
                case 1:
                    i = new Intent(context,SecondActivity.class);
                    context.startActivity(i);
                    break;
                case 2:
                    i = new Intent(context,SecondActivity.class);
                    context.startActivity(i);
                    break;
                case 4:
                    i = new Intent(context,SecondActivity.class);
                    context.startActivity(i);
                    break;
                case 5:
                    i = new Intent(context,SecondActivity.class);
                    context.startActivity(i);
                    break;
                case 6:
                    i = new Intent(context,SecondActivity.class);
                    context.startActivity(i);
                    break;
                case 7:
                    i = new Intent(context,SecondActivity.class);
                    context.startActivity(i);
                    break;
                case 8:
                    i = new Intent(context,SecondActivity.class);
                    context.startActivity(i);
                    break;
                case 9:
                    i = new Intent(context,SecondActivity.class);
                    context.startActivity(i);
                    break;


            }

        }


    });

    return rowView;

Upvotes: 0

Related Questions