I have a button as in the following:
<Button
android:text="Submit"
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
In my onCreate()
event, I am calling Button01 like this:
setContentView(R.layout.main);
View Button01 = this.findViewById(R.id.Button01);
Button01.setOnClickListener(this);
There is a background in the application, and I want to set an opacity on this submit button. How can I set an opacity for this view? Is it something that I can set on the java side, or can I set in the main.xml file?
On the java side I tried Button01.mutate().SetAlpha(100)
, but it gave me an error.
I've run into this problem with ICS/JB because the default buttons for the Holo theme consist of images that are slightly transparent. For a background this is especially noticeable.
Gingerbread vs. ICS+:
Copying over all of the drawable states and images for each resolution and making the transparent images solid is a pain, so I've opted for a dirtier solution: wrap the button in a holder that has a white background. Here's a crude XML drawable (ButtonHolder) which does exactly that:
Your XML file
ButtonHolder.xml
styles.xml
However, this results in a white border because the Holo button images include margins to account for the pressed space:
So the solution is to give the white background a margin (4dp worked for me) and rounded corners (2dp) to completely hide the white yet make the button solid:
ButtonHolder.xml
The final result looks like this:
You should target this style for v14+, and tweak or exclude it for Gingerbread/Honeycomb because their native button image sizes are different from ICS and JB's (e.g. this exact style behind a Gingerbread button results in a small bit of white below the button).
For API < 11 for textView color I did the following:
A little cumbersome, but hey, it works :-)
I'm amazed by everyone else's MUCH more complicated answers.
XML
You can very simply define the alpha in the color definition of the button (or any other view) in your xml:
In the above example, the color would be a partially transparent red.
When defining the color of a view, the format can be either
#RRGGBB
or#AARRGGBB
, whereAA
is the hex alpha value.FF
would be fully opaque and00
would be full transparent.Dynamically
If you need to dynamically alter the opacity in your code, use
Where the INT ranges from
0
(fully transparent) to255
(fully opaque).According to the android docs view alpha is a value between 0 and 1. So to set it use something like this:
What I would suggest you do is create a custom ARGB color in your colors.xml file such as :
then set your button background to that color :
Another thing you can do if you want to play around with the shape of the button is to create a Shape drawable resource where you set up the properties what the button should look like :
file: res/drawable/rounded_corner_box.xml
Then use that as the button background :
Although
btnMybutton.getBackground().setAlpha(45);
is nice idea, it just apply alpha to background and not the whole view.If you want apply alpha to view use
btnMybutton.setAlpha(0.30f);
instead. This apply opacity to View. It accepts a value between 0 and 1.Doc says: