How to Make a Background 20% Transparent on Android

How to Make a Background 20% Transparent on Android

6 June 2024 Stephan Petzl Leave a comment Tech-Help

Making a background partially transparent in Android can add a sleek, modern look to your UI. This article will guide you through the steps necessary to achieve a 20% transparent background for a TextView in Android.

Using Hexadecimal Color Codes

To make the background of a TextView 20% transparent, you can manipulate the alpha channel of a color in its hexadecimal representation. The format for a color with an alpha channel is #AARRGGBB, where AA represents the alpha value, and RR, GG, and BB represent the red, green, and blue channels, respectively.

Example for Red Color

For instance, to make a red background 20% transparent, you would use:

    
      <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="#33FF0000" />
    
  

In this example, 33 is the hexadecimal value for 20% transparency (since 255 * 0.2 = 51, which is 33 in hexadecimal).

Calculating Alpha Values

To calculate the alpha value for any percentage of transparency, follow these steps:

  1. Subtract the desired transparency percentage from 100 to get the opacity percentage. (e.g., 20% transparency means 80% opacity).
  2. Multiply the opacity percentage by 255 (the maximum value for an 8-bit channel).
  3. Convert the resulting value to hexadecimal.
  4. Prepend this hexadecimal value to the RGB color code.

General Hex Opacity Values

Here are some common opacity values:

  • 100% — FF
  • 95% — F2
  • 90% — E6
  • 85% — D9
  • 80% — CC
  • 75% — BF
  • 70% — B3
  • 65% — A6
  • 60% — 99
  • 55% — 8C
  • 50% — 80
  • 45% — 73
  • 40% — 66
  • 35% — 59
  • 30% — 4D
  • 25% — 40
  • 20% — 33
  • 15% — 26
  • 10% — 1A
  • 5% — 0D
  • 0% — 00

Practical Example

Let’s say you want to set a background color to 20% transparent black. You would use the following XML code:

    
      <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="#33000000" />
    
  

Here, 33 represents 20% transparency, and 000000 represents black.

Conclusion

Understanding how to manipulate the alpha channel in hexadecimal color codes allows you to create visually appealing designs with partial transparency. This technique can be applied to various UI elements to enhance the user experience.

Enhance Your Mobile Testing with Repeato

As a mobile developer, ensuring your app looks and functions as intended across different devices is crucial. Repeato, a no-code test automation tool for iOS and Android, can help you achieve this. With Repeato, you can create, run, and maintain automated tests quickly, allowing you to focus on developing a great product. Learn more about how Repeato can streamline your testing process on our documentation page.

Like this article? there’s more where that came from!