Sam
Sam

Reputation: 95

Android keep TextView at fixed location inside ImageView

I am currently trying to get some kind of dynamic credit card picture, but what I want is to be able to set the numbers and the creditcard, which is an imageview. Now I need my textview to be at the same location for all screensizes, but I have trouble achieving this.

Can anyone point me in the right direction? Or maybe share a better way with me? This is what I got now:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            android:padding="10dp">

<ImageView
    android:id="@+id/creditcardimage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    app:srcCompat="@drawable/creditcardblue"/>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/creditcardimage"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="81dp"
    android:layout_marginLeft="30dp"
    android:layout_marginStart="30dp"
    android:text="1234 5678 9876 5432"
    android:textSize="20sp"/>

Here is an image of the idea:

CreditCard preview

Thanks!

Upvotes: 2

Views: 140

Answers (1)

rupesh jain
rupesh jain

Reputation: 3430

You can follow the following steps

  1. Have a reference view of fixed size.
  2. Draw text on that view.Since the view is of fix size-the text will be drawn always at correct location.
  3. Now convert the reference view and overlapping textview into a bitmap.
  4. Now set this bitmap to actual image view.This image view along with text will scale as per current device configuration.

You can also set adjustViewBonds and other properties so that the aspect ratio of the imageview doesnt change.

Upvotes: 1

Related Questions