Sunday, August 23, 2015

How to set an ImageButton background transparent in Android

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F4F0EC"
    android:padding="16dp"
    tools:context=".MainActivity">

    <!-- No Transparency -->
    <ImageButton
        android:id="@+id/ib"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"
        android:src="@drawable/share_solid"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- Background Transparent by XML -->
    <!--
        You can also use
            android:background="@android:color/transparent"
            android:background="@null"
     -->
    <!--
        It keep the button click effect.
            android:background="?android:selectableItemBackground"
    -->
    <ImageButton
        android:id="@+id/ib2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="48dp"
        android:src="@drawable/share_solid"
        android:padding="8dp"
        android:background="?android:selectableItemBackground"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ib" />

    <!-- Background Transparent Programmatically + click effect -->
    <ImageButton
        android:id="@+id/ib3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="48dp"
        android:src="@drawable/share_solid"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ib2" />

    <!-- Background Transparent Programmatically -->
    <ImageButton
        android:id="@+id/ib4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="48dp"
        android:src="@drawable/share_solid"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ib3" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java

package com.cfsuman.androidtutorials;

import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.util.TypedValue;
import android.widget.ImageButton;


public class MainActivity extends Activity {

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

                // Get the widgets reference from XML layout
                ImageButton ib3 = findViewById(R.id.ib3);
                ImageButton ib4 = findViewById(R.id.ib4);

                // Set the third image button background transparent
                // This method allow us to show click effect
                TypedValue outValue = new TypedValue();
                getApplicationContext().getTheme().resolveAttribute(
                        android.R.attr.selectableItemBackground,
                        outValue,
                        true
                );
                ib3.setBackgroundResource(outValue.resourceId);

                // Another way (No button click effect)
                ib4.setBackgroundColor(Color.TRANSPARENT);
        }
}