12

I want to overlay textview on imageview on android which I have done programatically. I also want that image should be able to zoom in and zoom out which also I am able to done. But I am stuck in a problem of placing text on image in such a way that while I zoom image then text should not zoom and it should stay on the same position on the image not on the screen.

ismail
  • 46,010
  • 9
  • 86
  • 95
user1205088
  • 229
  • 2
  • 11
  • Do you have tried to add the TextView as View to the ImageView and overriding the gesture-listeners? – Tobias S Jul 31 '15 at 06:21
  • 1
    Check this out: http://stackoverflow.com/questions/14004424/android-zoomable-scrollable-imageview-with-overlays?rq=1 – zed Aug 03 '15 at 15:37
  • @TobiasK. what do you mean by adding "add the TextView as View to the ImageView" ? Can ImageView have child elements ? – techtinkerer Mar 18 '16 at 07:09
  • I wanted to place an textview over an imageview. Afterwards i wanted to resize the text on pinch to zoom gestures, but now i do not remember how i solved this whether is used different elements. – Tobias S Mar 18 '16 at 07:31

1 Answers1

6

OK, I created an XML layout that fits your needs. All you have to do is zoom in or out of the image.

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

    xmlns:android="http://schemas.android.com/apk/res/android"

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

    <ImageView
        android:id="@+id/image"
        android:layout_width="287dp"
        android:layout_height="287dp"

        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="Hello Constant Text Overlay"

        android:layout_gravity="center_horizontal|center_vertical"/>
        <!-- Don't want it centered? Then change the layout gravity to fit your needs -->

</FrameLayout>

With this, you can easily modify the width and height of the ImageView and the TextView will remain in the center without getting larger or smaller.

I noticed that you took a programmatic solution, and you can easily look at how it is done via XML and convert that to code.

What is important here is that you have a container around your ImageView that wraps the content. Inside of that you have the ImageView of specified width/height, and a TextView that is centered horizontally and vertically in the container.

You can do whatever you want to do with the Image now, the text will remain untouched; just it will remain in the center

Christopher Rucinski
  • 4,737
  • 2
  • 27
  • 58
  • This is a way to go if he wants centered text, but if he wants text in some other position then zed's comment is excellent approach. – Marko Lazić Aug 06 '15 at 22:54
  • I added a comment to explain how to change if from being centered. There are a plethora of positions that my solution works for correctly. This is also one of the simplest solutions – Christopher Rucinski Aug 07 '15 at 08:23
  • Also, if the user does not want it in one of the locations determined by `layout_gravity`, then they can add additional attributes like margins and such – Christopher Rucinski Aug 07 '15 at 08:26