How to make button non transparent?

11,442

Solution 1

The standard Holo themed button is partially transparent. You can either create a new button 9.png image to use in the button or create a new button by using drawable.

Here's a link on how to do the latter:

http://droidapp.co.uk/?p=309

Solution 2

Set the background to @android:drawable/btn_default to get rid of the transparency of holo theme

Solution 3

you can put the button in seperate Frame Layout so that your view wont affect the button and thats my idea

Solution 4

I was with the same problem but with ToggleButton. A ugly but fast solution was to position a ImageView right behind the button and change its margins, background and alpha to fit the button. For example, inside a RelativeLayout:

<ImageView
    android:layout_alignLeft="@+id/toggleButton"
    android:layout_alignBottom="@+id/toggleButton"
    android:layout_alignRight="@+id/toggleButton"
    android:layout_alignTop="@+id/toggleButton"
    style="@style/HackBackgroundToggleButton" />

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/toggleButton"
    ... />

And, in an effort to reduce the visible hack, the extracted style:

<style name="HackBackgroundToggleButton">
    <item name="android:layout_width">84dp</item>
    <item name="android:layout_height">42dp</item>
    <item name="android:background">#FFFFFF</item>
    <item name="android:layout_marginTop">4dp</item>
    <item name="android:layout_marginLeft">4dp</item>
    <item name="android:layout_marginRight">4dp</item>
    <item name="android:layout_marginBottom">4dp</item>
    <item name="android:alpha">0.75</item>
</style>

Solution 5

save below code as xml in drawable folder and give this xml as button background

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="0dp" android:color="#ffffff" />
    <solid android:color="#000000"/>
    <corners android:radius="1px"/>
    <padding android:left="5dp" android:top="3dp" android:right="5dp" android:bottom="3dp" /> 
</shape>

Ex:

android:background="@drawable/<< your file name>>"
Share:
11,442

Related videos on Youtube

Suzan Cioc
Author by

Suzan Cioc

Not to be offended

Updated on June 04, 2022

Comments

  • Suzan Cioc
    Suzan Cioc almost 2 years

    I wrote custom View which draws a circle. Then I put it on relative layout. Also I put standard button there, so that they overlap. And I see that button is transparent. How to make it non transparent?

    enter image description here

  • Suzan Cioc
    Suzan Cioc almost 12 years
    What is the simplest way? Can't I just style button as not transparent?
  • Tony
    Tony almost 12 years
    The second option is quite straight forward. You can just change the background of the button to a solid color (ie #999999) but that would not look good and wouldn't give the button a pressed state causing bad user experience.
  • Timmmm
    Timmmm over 11 years
    This also changes the graphic to the device default (i.e. not holo).
  • IgorGanapolsky
    IgorGanapolsky over 10 years
    Perfect answer. Thanks.
  • Bronek
    Bronek over 10 years
    It's the perfect answer! Do you know how to apply it to another controls? (especially to EditText)
  • Bronek
    Bronek over 10 years
    All right, I've found the answer to my question by myself, it is: @android:drawable/edit_text
  • Floris
    Floris about 10 years
    This is excellent, but when you say "Then I ran this...", where do you run that code from?