0

I've got a fragment with GridView and each element of GridView have and ImageView and TextView. I want to create and image with the ImageView and TextView, is it possible?

This is the code for the element of GridView:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="15dp"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >

    <ImageView
        android:id="@+id/imgGrid"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="false"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="false"
        android:layout_marginTop="5dp"
        android:scaleType="centerInside"
        android:src="@drawable/myImg" />

    <TextView
        android:id="@+id/nameGrid"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imgGrid"
        android:layout_centerHorizontal="true"
        android:background="@drawable/border_blue"
        android:gravity="center"
        android:text="My image"
        android:textColor="#ffffff"
        android:textStyle="bold" />

</RelativeLayout>

I want that each element of GridView will be an image like this:

item GridView

I do the rounded image with the following code:

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, Context mContext) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    int radius = Math.min(h / 2, w / 2);
    Bitmap output = Bitmap.createBitmap(w + 8, h + 8, Bitmap.Config.ARGB_8888);

    Paint p = new Paint();
    p.setAntiAlias(true);

    Canvas c = new Canvas(output);
    c.drawARGB(0, 0, 0, 0);
    p.setStyle(Paint.Style.FILL);

    c.drawCircle((w / 2) + 4, (h / 2) + 4, radius, p);

    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    c.drawBitmap(bitmap, 4, 4, p);
    p.setXfermode(null);
    p.setStyle(Paint.Style.STROKE);
    p.setColor(mContext.getResources().getColor(R.color.myPrimaryColor));
    p.setStrokeWidth(4);
    c.drawCircle((w / 2) + 4, (h / 2) + 4, radius, p);

    return output;
}

Then I need to link the ImageView and TextView in one image to set the rounded image of the element in GridView. What can I do to achieve this effect?

Thanks in advance.

Pamela
  • 127
  • 1
  • 11
  • I really don't understand what do you want to do... – Aspicas Oct 08 '15 at 09:45
  • @Pamela, Can you please refer http://stackoverflow.com/questions/31313577/not-able-to-get-the-bitmap-from-view one? I had given answer. Please pass parent view (Relative layout) as argument – Hiren Patel Oct 08 '15 at 09:45
  • please explain more in your question. it seem too hard to understand – Linh Oct 08 '15 at 09:50
  • Please get rid of: the RelativeLayout and the ImageView. You can put the image **directly** into the TextView, as a **compound drawable**. This helpd flattening your layout and improves the performances. – Phantômaxx Oct 08 '15 at 09:54

1 Answers1

2

Try adding an imageview a a textview inside a relative layout like this

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/powered_by_google_light"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="your text here"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>
    </RelativeLayout>

here the entire relative layout acts as an imageview with a textview inside it.

Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55