How to set transparent background for Image Button in code?

AndroidBackgroundTransparentImagebutton

Android Problem Overview


I can set ImageButton background transparent in layout.xml using:

android:background="@android:color/transparent"

How I can acomplish same thing using java code? Something like ib.setBackgroundColor(???);

Android Solutions


Solution 1 - Android

This is the simple only you have to set background color as transparent

    ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);
	btn.setBackgroundColor(Color.TRANSPARENT);

Solution 2 - Android

Do it in your xml

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButtonSettings"
        android:layout_gravity="right|bottom"
        android:src="@drawable/tabbar_settings_icon"
        android:background="@android:color/transparent"/>

Solution 3 - Android

This should work - imageButton.setBackgroundColor(android.R.color.transparent);

Solution 4 - Android

DON'T USE A TRANSAPENT OR NULL LAYOUT because then the button (or the generic view) will no more highlight at click!!!

>I had the same problem and finally I found the correct attribute from Android API to solve the problem. It can apply to any view

Use this in the button specifications

android:background="?android:selectableItemBackground"

This requires API 11

Solution 5 - Android

Try like this

ImageButton imagetrans=(ImageButton)findViewById(R.id.ImagevieID);

imagetrans.setBackgroundColor(Color.TRANSPARENT);

OR

include this in your .xml file in res/layout

android:background="@android:color/transparent 

Solution 6 - Android

simply use this in your imagebutton layout

android:background="@null"

using

 android:background="@android:color/transparent 

or

 btn.setBackgroundColor(Color.TRANSPARENT);

doesn't give perfect transparency

Solution 7 - Android

If you want to use android R class

textView.setBackgroundColor(ContextCompat.getColor(getActivity(), android.R.color.transparent));

and don't forget to add support library to Gradle file

compile 'com.android.support:support-v4:23.3.0'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeterView Question on Stackoverflow
Solution 1 - AndroidParag ChauhanView Answer on Stackoverflow
Solution 2 - AndroidbsautnerView Answer on Stackoverflow
Solution 3 - AndroidAbhinav ManchandaView Answer on Stackoverflow
Solution 4 - AndroidZar E AhmerView Answer on Stackoverflow
Solution 5 - AndroidBunnyView Answer on Stackoverflow
Solution 6 - Androidbourax webmasterView Answer on Stackoverflow
Solution 7 - AndroidMarsPeopleView Answer on Stackoverflow