Android Transparent Textview

Android Transparent TextView?

Please try this piece of code..

<TextView 
android:id="@+id/txtview1"
android:layout_width="wrap_content"
android:background="@drawable/bg_task"
android:layout_height="wrap_content"
android:textSize="14sp"
android:singleLine="true"
android:textColor="#FFFFFF" />

Used background image as transparent so may be solved that.

OR

android:background="#07000000"

OR

Please try below ...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="center_horizontal" android:orientation="vertical"
android:background="@drawable/main_bg">
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/header"
android:src="@drawable/btn_complete" />
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"
android:paddingRight="5dp" android:scrollbarStyle="outsideOverlay"
android:cacheColorHint="#00000000" />
<TextView android:id="@+id/footer" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="25sp"
android:singleLine="true" android:background="#07000000"
android:textColor="#FFFFFF" android:text="rrrrr"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</LinearLayout>

How to make your TextView background transparent?

Try creating a color value resource

<color name="color_transparent">#00FFFFFF</color>
in colors.xml under values directory

Then use it as background in your textview as:

android:background="@color/color_transparent"

Normally, textviews come transparent out of the box, so please check attributes of the parent layout first.

Android transparent text

Update, Jan 30, 2016

I made a small library and written a blog post out of this answer, so you don't need to copy and paste code and I do the maintenance for you. :)

Use the view in xml as:

<it.gilvegliach.android.transparenttexttextview.TransparentTextTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/view_bg"
android:text="Hello World" />

Gradle dependency:

 compile 'it.gilvegliach.android:transparent-text-textview:1.0.3'

Original Answer

This is how you can achieve that effect:

  1. you render the text over a transparent background on a bitmap
  2. you use that bitmap to clip the text shape out of the solid white background

Here is a simple subclass of TextView that does that.

final public class SeeThroughTextView extends TextView
{
Bitmap mMaskBitmap;
Canvas mMaskCanvas;
Paint mPaint;

Drawable mBackground;
Bitmap mBackgroundBitmap;
Canvas mBackgroundCanvas;
boolean mSetBoundsOnSizeAvailable = false;

public SeeThroughTextView(Context context)
{
super(context);

mPaint = new Paint();
mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_OUT));
super.setTextColor(Color.BLACK);
super.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}

@Override
@Deprecated
public void setBackgroundDrawable(Drawable bg)
{
mBackground = bg;
int w = bg.getIntrinsicWidth();
int h = bg.getIntrinsicHeight();

// Drawable has no dimensions, retrieve View's dimensions
if (w == -1 || h == -1)
{
w = getWidth();
h = getHeight();
}

// Layout has not run
if (w == 0 || h == 0)
{
mSetBoundsOnSizeAvailable = true;
return;
}

mBackground.setBounds(0, 0, w, h);
invalidate();
}

@Override
public void setBackgroundColor(int color)
{
setBackgroundDrawable(new ColorDrawable(color));
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
mBackgroundBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mBackgroundCanvas = new Canvas(mBackgroundBitmap);
mMaskBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mMaskCanvas = new Canvas(mMaskBitmap);

if (mSetBoundsOnSizeAvailable)
{
mBackground.setBounds(0, 0, w, h);
mSetBoundsOnSizeAvailable = false;
}
}

@Override
protected void onDraw(Canvas canvas)
{
// Draw background
mBackground.draw(mBackgroundCanvas);

// Draw mask
mMaskCanvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR);
super.onDraw(mMaskCanvas);

mBackgroundCanvas.drawBitmap(mMaskBitmap, 0.f, 0.f, mPaint);
canvas.drawBitmap(mBackgroundBitmap, 0.f, 0.f, null);
}
}

Example screenshot: indigo pattern for activity background, pink solid fill for TextView background.

This works both for solid color backgrounds and general drawables. Anyway, this is only a BASIC implementation, some feature such as tiling are not supported.

How to make the end of the text in TextView transparent in Android

I'd use a color gradient like this

color_gradient.xml

<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#ffffff"
android:endColor="#00ffffff"
android:angle="180"/>
</shape>

For example for the first row:

Sample Image

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_centerVertical="true"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World HelloWorld HelloWorld"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Title"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World HelloWorld HelloWorld"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Subhead"
/>
</LinearLayout>

<View
android:id="@+id/gradient"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_toStartOf="@+id/button_box"
android:background="@drawable/color_gradient"/>

<FrameLayout
android:id="@+id/button_box"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:padding="16dp"
android:background="#ffffff">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="8dp"
android:text="Tout mettre a jour"
android:textColor="#ffffff"
android:background="#22bb66"/>
</FrameLayout>
</RelativeLayout>

Please note that there are several options for the layout. The central idea in this approach is to make the gradient drawable plus the white box containing the Button overlap the TextViews

How to set background opacity of a textview placed on to of ImageView

In your TextView definition, add:

android:background="#8888"

888 being a mid grey (888888), with 8 being a mid alpha (88)

OR

android:background="#8000"

000 being a black (000000), with 8 being a mid alpha (88)

How to make textview's background transparent when it is the child of the layout

I checked with the code added above, I could not see any issue at all. Could you please share the snapshot of screen to point out exact issue. Also you can try with setting the background="@null" on TextView.

Changing TextView Opacity in Android

you can set the alpha like this

int alpha = 0;
((TextView)findViewById(R.id.t1)).setTextColor(Color.argb(alpha, 255, 0, 0));

as the alpha you getting from the seekbar that will be set into the text color

TextView with a background and Transparent Text

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);

Android textview over imageview with background transparent

I think you should use merge tag in xml of your layout.. For example Merge example

How to make image background of textview semi transparent but not the text?

just realign your layout

//this is your root
<RelativeLayout>
//this is your background
//add your semi transparent for below imageView
<ImageView android:height="match_parent" android:witdh="match_parent"/>

//this is your text
<TextView/ >

</RelativeLayout>

then programmatically

imageView.setImageAlpha(255); //it will not affect the textview

or just add android:alpha in xml at imageView



Related Topics



Leave a reply



Submit