Placing a Textview on Top of Imageview in Android

Placing a textview on top of imageview in android

This should give you the required layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
android:id="@+id/flag"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true" />

</RelativeLayout>

Play with the android:layout_marginTop="20dp" to see which one suits you better. Use the id textview to dynamically set the android:text value.

Since a RelativeLayout stacks its children, defining the TextView after ImageView puts it 'over' the ImageView.

NOTE: Similar results can be obtained using a FrameLayout as the parent, along with the efficiency gain over using any other android container. Thanks to Igor Ganapolsky(see comment below) for pointing out that this answer needs an update.

How to place a TextView on top of a ImageVew in ConstraintLayout

If your app has a minimum SDK of 21 or higher, you can add this to whichever UI element you want to appear above the other: android:elevation="4dp"

Doesn't have to be 4dp, it can be higher or lower depending on what works in your project.

TextView on top of ImageView in Android

Change the RelativeLayout width and height to wrap_content.

Use android:layout:centerInParent="true" in your TextView.

Add TextView over ImageView (Android)

You may use FrameLayout, AbsoluteLayout (deprecated) or RelativeLayout (most common of these). An example of RelativeLayout, for centering is used android:layout_centerInParent="true", set for both childs

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/splash_imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20dip"
android:layout_centerInParent="true"
android:background="#AA000000"
android:padding="12dip"
android:text="SomeText"
android:textColor="#ffffffff" />

</RelativeLayout>

Drawing Views is in the same order as in XML, so ImageView will be drawn first and then TextView over it

Another approach would be to use FrameLayout with android:gravity="center" attr (nothing needed for childs)

Also remeber and read about android:elevation and android:translationZ XML attributes, which may reorder drawing, and also view.bringToFront() method

Text TOP Right on Image in Android

Give a marginTop to the imageview and then give the half of that as the marginTop of the textview.

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layoutforprofileimage"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/image"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="20dp"
android:padding="2dp"
android:src="@drawable/user" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/image"
android:layout_marginRight="0dp"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:background="@drawable/tv_circle"
android:gravity="center"
android:minHeight="20sp"
android:minWidth="20sp"
android:padding="2dp"
android:paddingBottom="1dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:scaleType="fitStart"
android:text="0"
android:textColor="#ffffffff"
android:textSize="12sp"
android:visibility="visible"
tools:ignore="RtlHardcoded" />

tv_circle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid android:color="#F08600" />

<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />

</shape>

For the above XML the output will be as following screenshot.

Sample Image

How do i place a TextView inside a ImageView on the same place across devices?

I know that I can make use of dimens.xml and use different margin values for different device dimensions there, but I don't believe this would be the best approach.

You are correct, you can use guidelines like this to make sure that your layout will look the same on different screens:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.8"
tools:srcCompat="@tools:sample/backgrounds/scenic" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"/>

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.45"/>

<TextView
android:id="@+id/textView7"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/colorAccent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintWidth_percent="0.2"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toStartOf="@+id/guideline3" />

</androidx.constraintlayout.widget.ConstraintLayout>

It will look like this (the black arrows are the guidelines):

Sample Image

And now you have only 1 layout but it is a responsive layout - you don't need to create more for different devices.


Please notice that I don't have any fixed sizes on my views, I have created a fully responsive layout using percentage, for more info about the subject you can check my answer.

Put a textview over Imageview in LinearLayout

just copy this :

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:padding="5dp"
android:text="Text 1" />

</FrameLayout>
<FrameLayout android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:padding="5dp"
android:text="Text 2" />

</FrameLayout>
<FrameLayout android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:padding="5dp"
android:text="Text 3" />

</FrameLayout>

How to properly place multiple TextView over imageView

You could use a ConstraintLayout. If you constrain the Name: TextView's top to the top of the ImageView and create a chain of constraints (ending with the Information EditText having its bottom constrained to the bottom of the ImageView), that will keep all your views in the bounds of the ImageView.

A layout like this:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="300dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:src="@color/grey"/>

<TextView
android:id="@+id/name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="@id/image"
app:layout_constraintTop_toTopOf="@id/image" />

<EditText
android:id="@+id/name_text_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="@id/name_text"
app:layout_constraintTop_toBottomOf="@id/name_text"
tools:text="Some random text in here"/>

<TextView
android:id="@+id/id_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID:"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="@id/name_text_entry"
app:layout_constraintTop_toBottomOf="@id/name_text_entry"/>

<EditText
android:id="@+id/id_text_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="@id/id_text"
app:layout_constraintTop_toBottomOf="@id/id_text"
tools:text="Some random text in here"/>

<TextView
android:id="@+id/information_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Information:"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="@id/id_text_entry"
app:layout_constraintTop_toBottomOf="@id/id_text_entry"/>

<EditText
android:id="@+id/information_text_entry"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:lines="5"
app:layout_constraintStart_toStartOf="@id/information_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/information_text"
tools:text="Some random text in here"/>

will produce something like this (the grey background being your ImageView)

Here's a link since I can't embed images

How can I place a TextView on top of ImageView

Try to set the imageview as the background of the textview.

Do you want to put some words upon a image or a sharp drawable?

I'll use a shape drawable to make an example.

Now I have a shape like this:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>

<size
android:height="100dp"
android:width="100dp"
/>

<solid
android:color="#66ccff"
/>

</shape>

Then set it as the background of a textview in a layout xml.

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/circle"
android:text="Hello World!"
/>

That looks like it. It's a 100dp*100dp one.
100*100

And try to modify the width and height of the textview.

<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/circle"
android:text="Hello World!"
/>

Now it looks like this. A Lot Larger.
200*200

So the conclusion is:

  • Change the size of drawable and use wrap_content in the layout xml.
  • Change "layout_width" and "layout_height" in the layout xml.

But if you want to use the smaller one or the lager one, maybe you should add some control codes in the control java file.

(Sorry I'm not in a English-spoken country so maybe my words is not clear enough)

I'm sorry that I didn't obverse the rules before answer (cuz it's my first answer).

Place a textView in the middle of imageView programatically android

Maybe you should just have an text view and use image as a background:

textView.setBackgroundResource(R.drawable.img);


Related Topics



Leave a reply



Submit