0

I would like to create a view in android that componsed of an ImageView and a TextView upon this ImageView. But the Custom views that we can create in Android contain attributes that can only be of certain types, as specified in this post : Defining custom attrs So I can't define a custom view with an ImageView attribute and a TextView attribute. I don't want to define it this way neither :

<FrameLayout android:id="@+id/frame_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

      <ImageView
         android:id="@+id/card_11"
         android:layout_width="@dimen/card_width"
         android:layout_height="@dimen/card_height"
         android:src="@drawable/carte_vierge_bleue"
         android:scaleType="fitXY"
         android:clickable="true" 
         android:visibility="visible"
         android:onClick="onCardClicked"/>

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1234 5678 1234 5678"
        android:layout_gravity="center"/>

</FrameLayout>

Because I would like to add other images on the first ImageView after that so I would like to have a custom view that I can modify however I want, and add on it as many ImageViews as I want, and this way of doing would make the layout file very long... Any idea ?

Community
  • 1
  • 1
akari
  • 617
  • 1
  • 11
  • 32

1 Answers1

2

If i understood correctly, you can use a RelativeLayout with as many ImageViews as you want. For example:

<RelativeLayout android:id="@+id/frame_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

      <ImageView
         android:id="@+id/card_11"
         android:layout_width="@dimen/card_width"
         android:layout_height="@dimen/card_height"
         android:src="@drawable/carte_vierge_bleue"
         android:scaleType="fitXY"
         android:clickable="true" 
         android:visibility="visible"
         android:onClick="onCardClicked"/>

     <ImageView
         android:id="@+id/card_12"
         android:layout_width="@dimen/card_width"
         android:layout_height="@dimen/card_height"
         android:src="@drawable/carte_vierge_bleue"
         android:scaleType="fitXY"
         android:clickable="true" 
         android:visibility="visible"
         android:onClick="onCardClicked"/>

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1234 5678 1234 5678"
        android:layout_gravity="center"/>

</RelativeLayout>

The TextView will be displayed on top of card12 ImageView, which will be displayed on top of card11 ImageView and so on.

Hope it helped, Mattia

  • Yes, you are right, it worked. I am just wondering how can I display the textView in the center of the view since it is displayed on the top of it, even with the attribute gravity set to center ? – akari May 09 '14 at 14:22
  • 1
    You are in a RelativeLayout you can't use the gravity attribute, you must you _centerInParent="true"_. – Mattia Franchetto May 09 '14 at 15:07