How to Make a Background 20% Transparent on Android

How to make a background 20% transparent on Android

Make the color have 80% in the alpha channel. For example, for red use #CCFF0000:

<TextView
...
android:background="#CCFF0000" />

In the example, CC is the hexadecimal number for 255 * 0.8 = 204. Note that the first two hexadecimal digits are for the alpha channel. The format is #AARRGGBB, where AA is the alpha channel, RR is the red channel, GG is the green channel and BB is the blue channel.

I'm assuming that 20% transparent means 80% opaque. If you meant the other way, instead of CC use 33 which is the hexadecimal for 255 * 0.2 = 51.

In order to calculate the proper value for an alpha transparency value you can follow this procedure:

  1. Given a transparency percentage, for example 20%, you know the opaque percentage value is 80% (this is 100-20=80)
  2. The range for the alpha channel is 8 bits (2^8=256), meaning the range goes from 0 to 255.
  3. Project the opaque percentage into the alpha range, that is, multiply the range (255) by the percentage. In this example 255 * 0.8 = 204. Round to the nearest integer if needed.
  4. Convert the value obtained in 3., which is in base 10, to hexadecimal (base 16). You can use Google for this or any calculator. Using Google, type "204 to hexa" and it will give you the hexadecimal value. In this case it is 0xCC.
  5. Prepend the value obtained in 4. to the desired color. For example, for red, which is FF0000, you will have CCFF0000.

You can take a look at the Android documentation for colors.

Set transparent background of an imageview on Android

In your XML set the Background attribute to any colour, White(#FFFFFF) shade or Black(#000000) shade. If you want transparency, just put 80 before the actual hash code:

#80000000

This will change any colour you want to a transparent one.. :)

How to make android layout background transparent?

Try to give this on your item CardView

 app:cardBackgroundColor="@android:color/transparent"

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.

How can I make the background of my dialog in android transparent?

Add this code where you are initializing your dialog

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

or you can also add this code instead

 dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

How to make Custom View transparent in android

In your Class ConnectDotsView change:

private static final int BACKGROUND = 0xFFDDDDDD;

to

private static final int BACKGROUND = Color.TRANSPARENT;

or

private static final int BACKGROUND = Color.parseColor("#00000000");

#00AABBCC = ARGB (00 is Alpha, AA is red, BB is green and CC is blue), 00 alpha is 0% and FF is 100%. This mean #00AABBCC will be transparent, #80AABBCC will be at 50% transparent and #FFAABBCC not transparent

How to make chip background transparent in android?

You can set it programmatically through the setChipBackgroundColorResource API:

copyLinkChip.setChipBackgroundColorResource(android.R.color.transparent)

Or through XML

app:chipBackgroundColor="@android:color/transparent"


Related Topics



Leave a reply



Submit