2

i want a grid item like this: enter image description here

i tried to make a grid view item with this code:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/gridImage"
        android:layout_width="233dp"
        android:layout_height="197dp"

        android:background="@drawable/breakfast_background"
        android:scaleType="center"
        android:src="@drawable/frame" >

        </ImageView>

    <TextView
        android:id="@+id/alertCount"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginLeft="95dip"
        android:background="@drawable/ic_launcher"
        android:paddingLeft="25dip"
        android:paddingTop="18dip"
        android:text="10"
        android:textColor="@color/black"
        android:textSize="30sp"
        android:textStyle="bold"
        android:visibility="gone"

        />

    <TextView
        android:id="@+id/tv_TableName"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:textColor="#ffffff"
        android:layout_marginTop="155dp"
        android:singleLine="true"
        android:textSize="10pt"
        android:gravity="center"
        android:text="TextView" />

</RelativeLayout>

enter image description here

but background of image is not showing properly.

please suggest.

2
  • 1
    The image view is sized by its source, not by its background
    – mihail
    Jun 10, 2013 at 15:44
  • Thanks but what is the solution ? i need a frame and image should be replaced dynamically. Jun 10, 2013 at 15:57

1 Answer 1

3

i guess you should use 2 ImageView instead of one :

<ImageView
    android:id="@+id/backgroundImage"
    android:layout_width="233dp"
    android:layout_height="197dp"
    android:scaleType="fitXY"
    android:src="@drawable/shaped_breakfast_background" />

<ImageView
    android:id="@+id/frameImage"
    android:layout_width="233dp"
    android:layout_height="197dp"
    android:scaleType="fitXY"
    android:src="@drawable/frame" />

with shaped_breakfast_background.xml in your drawable/ folder, being a shape (look up android res shape) that will take your image and round the corners by the same radius as your frame. I admit I never tried it. One drawback is : using bitmaps inside xml resources doesn't give you access to all the interesting scaleTypes ImageView handles (centerInside, centerCrop and so on), which might be a problem

However, if using a shape doesn't solve your problem you could of course use a breakfast_background already shaped the same way as your frame, this would save you a lot of time. And you could then get along with your "1 ImageView" solution.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.