0

I want to know how I can write text on image and auto-adjust text size.

Let's say I have the image below enter image description here

How can I put text into this image and auto-adjust text size depending on the length of the text ? I don't want to deform the image I just want to change the text size. And of course I don't want the text go out of the square.

Edit 1:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background">


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

    <ImageView
        android:id="@+id/dice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dice" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/dice"
        android:layout_alignTop="@+id/dice"
        android:layout_alignRight="@+id/dice"
        android:layout_alignBottom="@+id/dice"
        android:text="Lorem ipsum dolor sit amet bla bla bla bla Lorem ipsum dolor sit amet bla bla bla bla Lorem ipsum dolor sit amet bla bla bla bla Lorem ipsum dolor sit amet bla bla bla bla"
        android:gravity="center"/>

</RelativeLayout>

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

    <ImageView
        android:id="@+id/dice2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dice" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/dice2"
        android:layout_alignTop="@+id/dice2"
        android:layout_alignRight="@+id/dice2"
        android:layout_alignBottom="@+id/dice2"
        android:text="Coucou"
        android:gravity="center"
        />

</RelativeLayout>

enter image description here

Edit 2 : enter image description here

guillaume
  • 1,566
  • 4
  • 22
  • 43

1 Answers1

1

You could place the TextView over this ImageView and use:

android:id = "@+id/myImageView"
android:layout_alignLeft="@+id/myTextView"
android:layout_alignRight="@+id/myTextView"
android:layout_width="wrap_content"

This will make sure that it does not jump out of the square width wise. If you want to ensure height wise as well, use layout_alignBottom and layout_alignTop as well.

Abu
  • 553
  • 3
  • 8
  • @guillaume you have given toLeftOf instead of layout_alignLeft – Abu Feb 06 '15 at 17:43
  • Even with a layout_alignLeft it's the same thing – guillaume Feb 06 '15 at 17:45
  • Are you sure your image is actually only the square? Because if so, how are the two images separated so far since you have not given any margin/padding and the weight for both layouts are the same! – Abu Feb 06 '15 at 17:52