---
title: "Trying to set opacity (alpha) for a view in android"  
description: "Trying to set opacity (alpha) for a view in android"  
author: "Anurag Sharma"  
published: 2016-01-13  
updated: 2023-06-27  
canonical: https://www.mindstick.com/forum/33872/trying-to-set-opacity-alpha-for-a-view-in-android  
category: "android"  
tags: ["android", "android styles"]  
reading_time: 2 minutes  

---

# Trying to set opacity (alpha) for a view in android

I am using a button :

```
<Button      android:text="Submit"      android:id="@+id/Button01"      android:layout_width="fill_parent"      android:layout_height="wrap_content"></Button>
```

In my onCreate() [event](https://www.mindstick.com/articles/167719/marquee-hire-benefits-of-event-lighting-hire-london), I am calling Button01 like this:\

```
setContentView(R.layout.main);View Button01 = this.findViewById(R.id.Button01);Button01.setOnClickListener(this);
```

There is a [background](https://www.mindstick.com/articles/65/how-to-change-background-color-of-tab-control-in-c-sharp) in the [application](https://www.mindstick.com/articles/12824/calculator-application-in-android), and I want to set an opacity on this [submit button](https://www.mindstick.com/forum/12834/make-text-appear-when-submit-button-clicked-and-checkbox-is-unchecked). How can I set an opacity for this [view](https://yourviews.mindstick.com/view/84701/layoffs-in-google-india-2023-view)? Is it something that I can set on the [java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) side, or can I set in the main.xml [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp)?\
On the java side I tried Button01.mutate().SetAlpha(100), but it gave me an [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing)

## Replies

### Reply by Aryan Kumar

Sure, here is how to set opacity for a view in Android:

Code snippet

```plaintext
import android.content.Context;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the view from the layout.
        View view = findViewById(R.id.view);

        // Set the opacity of the view.
        float opacity = 0.5f;
        view.setAlpha(opacity);
    }
}
```

In this code, we first get the view from the layout using the `findViewById()` method. Then, we set the opacity of the view using the `setAlpha()` method. The `setAlpha()` method takes a float value as input, which represents the opacity of the view. A value of 1.0f represents fully opaque, while a value of 0.0f represents fully transparent.

In this example, we are setting the opacity of the view to 0.5f, which means that the view will be half transparent.

### Reply by Anonymous User

Hi Anurag,\
You just need to extend TextView and override onSetAlpha:\

```
import android.content.Context;import android.util.AttributeSet;import android.widget.TextView;public class AlphaTextView extends TextView {  public AlphaTextView(Context context) {    super(context);  }  public AlphaTextView(Context context, AttributeSet attrs) {    super(context, attrs);  }  public AlphaTextView(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);  }  @Override  public boolean onSetAlpha(int alpha) {    setTextColor(getTextColors().withAlpha(alpha));    setHintTextColor(getHintTextColors().withAlpha(alpha));    setLinkTextColor(getLinkTextColors().withAlpha(alpha));    return true;  }}
```


---

Original Source: https://www.mindstick.com/forum/33872/trying-to-set-opacity-alpha-for-a-view-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
