6

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!

Reinherd
  • 5,476
  • 7
  • 51
  • 88

3 Answers3

3

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);
suku
  • 10,507
  • 16
  • 75
  • 120
Dima
  • 158
  • 6
  • 1
    I'm not used in using canvas. How can I add this code as a View? – Reinherd Oct 28 '13 at 09:55
  • you can add an ImageView to your layout and then: ImageView image = (ImageView)getActivity().findViewById(R.id.imageView1);image.setImageBitmap(bmOverlay); – Dima Oct 28 '13 at 09:57
  • I've tried that and the text, if it's transparent it's invisible, so it's not working. – Reinherd Oct 28 '13 at 10:42
  • try this: http://android.okhelp.cz/create-bitmap-and-draw-text-into-bitmap-android-example/ – Dima Oct 28 '13 at 10:53
  • This is what I do need. However I tried to use it in my app (copy & paste) and the text is not transparent. Can you try it? – Reinherd Oct 28 '13 at 11:00
  • 1
    Okay, it worked with this: paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); – Reinherd Oct 28 '13 at 11:11
1

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

kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
0

You cannot achieve this in Xml Layout, you need to set same color for both layout background and the text like below:

<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="#0011ff" >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffcecece"
    android:padding="5dp"
    android:text="@string/hello_world"
    android:textColor="#0011ff" />
</RelativeLayout>
jyomin
  • 1,957
  • 2
  • 11
  • 27