I have an imageView with an image, over that image I want to place a text. How can I achieve that?
You could possibly
onDraw
. Call super.onDraw()
in that method first and if you do it that way, you can use this as a single Layout Component which makes it easier to layout together with other components.
You can use a TextView and change its background to the image you want to use
You may want to take if from a diffrent side: It seems easier to have a TextView with a drawable on the background:
<TextView
android:id="@+id/text"
android:background="@drawable/rounded_rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</TextView>
You want to use a FrameLayout or a Merge layout to achieve this. Android dev guide has a great example of this here: Android Layout Tricks #3: Optimize by merging.
Try the below code this will help you`
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/gallery1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#7ad7d7d7"
android:gravity="center"
android:text="Juneja Art Gallery"
android:textColor="#000000"
android:textSize="15sp"/>
</RelativeLayout>
For this you can use only one TextView with android:drawableLeft/Right/Top/Bottom
to position a Image to the TextView. Furthermore you can use some padding between the TextView and the drawable with android:drawablePadding=""
Use it like this:
<TextView
android:id="@+id/textAndImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="@drawable/yourDrawable"
android:drawablePadding="10dp"
android:text="Look at the drawable below"/>
With this you don't need an extra ImageView. It's also possible to use two drawables on more than one side of the TextView.
The only problem you will face by using this, is that the drawable can't be scaled the way of an ImageView.