Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Layout background alpha

Tags:

Hi I have a layout which I'm using to fill my page and I have to set a background image held in my drawable folder in that layout.

I want to then set the alpha value of the image to something quite low almost make the image like a water mark.

my xml looks like

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/background" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/main_background" > 

As you can see I have assigned an id to the layout

I thought in my oncreate I could do something like this?

View backgroundimage = (View) findViewById(R.id.background); backgroundimage.setAlpha(80); 

This is not working however I suspect its because I'm trying to cast the background as a View what should I cast it as?

like image 702
user1096447 Avatar asked Dec 21 '11 09:12

user1096447


People also ask

How do I make my background transparent on Android?

getBackground(). setAlpha(51); Here you can set the opacity between 0 (fully transparent) to 255 (completely opaque). The 51 is exactly the 20% you want.

How do I make Constraintlayout transparent?

If you add this line to the Constraint Layout xml it will become transparent: android:background="@android:color/transparent" . This will make the background see through and show what ever is bellow or overlapping.

How do you set up Alpha?

To get α subtract your confidence level from 1. For example, if you want to be 95 percent confident that your analysis is correct, the alpha level would be 1 – . 95 = 5 percent, assuming you had a one tailed test. For two-tailed tests, divide the alpha level by 2.

How do I change opacity of an image in Android?

Here is a simple function to change the opacity of a view in android. Opacity is called alpha in android. so setAlpha(int) will do the job. ImageView img = (ImageView)findViewById(R.


2 Answers

Try to use:

Drawable.setAlpha(); 

You should do something like this:

View backgroundImage = findViewById(R.id.background); Drawable background = backgroundImage.getBackground(); background.setAlpha(80); 
like image 62
Tang Ke Avatar answered Nov 27 '22 06:11

Tang Ke


If you want to set alpha in xml then u can try this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/background" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#CC000000" > 

first 2 digits are used to set alpha here alpha is 80% next 6 digits for color code

Hence, alpha is set in hexadecimal coding. FF = 100% and 00 = 0%, thus 80% is not 80 in hexadecimal, for more standard values see this post.

like image 29
Kaushik Avatar answered Nov 27 '22 06:11

Kaushik