Pages

Search This Blog

Sunday, 12 August 2012

IN ANDROID MAKING A BUTTON TRANSPARENT

In few occasion button should be made transparent i.e. the background should be visible and no color should be made on button. When button is made transparent the background image will be visible even after placing the button. The following code also applies border for button in android, it also makes the button rounded rectangle.
Creating layout and button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@drawable/and4">
'and4 is an image in drawable folder

<Button
    android:id="@+id/buttonStart"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@drawable/button_shape" 
'button_shape is an xml file which applies style for button
    android:textColor="#FFFFFF"
    android:textStyle="bold|normal"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:text="Start" />
Button Style
button_shape.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#0000"/>
'use only 4 zeros to make button transparent
<gradient
            android:startColor="#3300FF"
            android:endColor="#3300FF"
            android:angle="270" />
    <corners
     android:bottomRightRadius="20dp"
     android:bottomLeftRadius="20dp"
     android:topLeftRadius="20dp"
     android:topRightRadius="20dp"/>
  <stroke android:width="6px" android:color="#6600FF"/>
'stroke applies border for button in android
</shape>


No comments:

Post a Comment