Nikhil Agrawal
Nikhil Agrawal

Reputation: 26528

Centerally aligned text over ImageView in android Linear Layout

How to display text on android ImageView with Linear Layout. I found many examples on stack overflow for this purpose but all are for Relative Layout, Frame Layout But I am not getting it in Linear Layout.

I have tried for android:text="My Text" But its not working.

Please suggest me if its possible to do so. If so how can it be done.

 <ImageView
    android:id="@+id/share_button"
    android:layout_width="fill_parent"
    android:layout_height="25dp"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    android:layout_weight="2"
    android:text="text"
    android:background="@drawable/iosbutton" />

This is the Image attached to this ImageView on which I want to display text in the center.

enter image description here

Upvotes: 0

Views: 1224

Answers (2)

Pragnani
Pragnani

Reputation: 20155

Try this

Use this method

public BitmapDrawable writeOnDrawable(int drawableId, String text){

    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);

    Paint paint = new Paint(); 
    paint.setStyle(Style.FILL);  
    paint.setColor(Color.BLACK); 
    paint.setTextSize(20); 

    Canvas canvas = new Canvas(bm);
    canvas.drawText(text, 0, bm.getHeight()/2, paint);

    return new BitmapDrawable(bm);
}

call like this

ImageView image=(ImageView)findViewById(R.id.imageView1);  
image.setImageDrawable(writeOnDrawable(R.drawable.ic_launcher,"Hello"));

Upvotes: 1

alex
alex

Reputation: 6409

Does it have to be an ImageView?

<TextView
    android:id="@+id/share_button"
    android:layout_width="fill_parent"
    android:layout_height="25dp"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    android:layout_weight="2"
    android:text="text"
    android:gravity="center"
    android:background="@drawable/iosbutton" />

Upvotes: 1

Related Questions