Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView with a background and Transparent Text

I'm trying to do the following:

enter image description here

For now, I am trying to use NativeAndroid.

However, I can do this with HTML and css.

As you can see, it's something like:

  • Holder with blue (might be any other color or even an IMAGE) background
  • TextView with (transparent??) color
  • TextView with gray background

However, if I set background to grey and the text to transparent, the text "disappears" and it's all shown as gray.

This is my try (Native Android):

<RelativeLayout 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:background="#ff000000" >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffcecece"
    android:padding="5dp"
    android:text="@string/hello_world"
    android:textColor="#00ffffff" />
</RelativeLayout>

However, it's showing the following:

enter image description here

There's text, bu as it is transparent, it's not shown.

So my question is:

  • How can I achieve this?
  • Canvas? Spans? I'm not used to those approach, so some directions would be appreciated.

Thanks!!

Edit: That blue background must be dynamic and it may be an IMAGE!

like image 414
Reinherd Avatar asked Oct 28 '13 09:10

Reinherd


People also ask

How do I make my background transparent?

Click Picture Tools > Recolor > Set Transparent Color.

How do I add a newline to a TextView in Android?

Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.


2 Answers

you can use Canvas for that:

Bitmap bmOverlay = Bitmap.createBitmap(1200, 1200, Bitmap.Config.ARGB_8888);//bMap.getConfig());

Canvas canvas = new Canvas(bmOverlay);
canvas.drawColor(Color.LTGRAY);
Paint paint = new Paint();

Paint mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintText.setStrokeWidth(3);
mPaintText.setStyle(Paint.Style.FILL_AND_STROKE);
mPaintText.setColor(Color.TRANSPARENT);
mPaintText.setTextSize(50f);
mPaintText.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));    
canvas.drawText("Your TExt", x, y, mPaintText);
like image 121
Dima Avatar answered Nov 15 '22 08:11

Dima


Set the textcolor as your layout backgound color like light sky blue..

like image 42
kalyan pvs Avatar answered Nov 15 '22 07:11

kalyan pvs