I created a simple ImageButton in my app and im using and image in it.
I had like that when I click the button that it wont show that it was clicked (not sure how it is called but when the button changes color).
My button looks as follows:
and when I click it becomes:
<ImageButton
android:id="#+id/returnButton"
style="?android:borderlessButtonStyle"
android:layout_width="40dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:src="#drawable/ic_rarrow"
app:layout_constraintBottom_toBottomOf="#+id/tv_Username"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/tv_Username">
</ImageButton>
I want to remove this gray background even when clicked because it shows boundaries that I wish to hide.
Thank you
You can solve the problem by adding one line of code.
android:background="#null"
Hoping it will be helped
You can apply a transparent background to the ImageButton
android:background="#android:color/transparent"
Apply a background to the Button:
android:background="#android:color/white"
This question already has answers here:
Set FAB icon color
(18 answers)
Closed 4 years ago.
I am using one of the preset button designs (an add button) for my FAB. I was able to change the colour of the background colour of the button but the plus is green and I want to change that. How can I change that?
Using
android:tint
property you can set the color like this
<android.support.design.widget.FloatingActionButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:tint="#android:color/white"
android:src="#android:drawable/ic_input_add" />
Hope this helps. Good luck :)
I am trying to make a button that has a shadow using elevation with a background image being my sign in with gmail png. The button is contained within a relative layout. The elevation won't show no matter what I try to do. I tried solutions from other forum questions and none worked.
Here is my code for the button
<Button
android:id="#+id/google"
android:layout_width="270dp"
android:layout_height="38dp"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:background="#drawable/google"
android:elevation="10dp"
android:layout_below="#id/slogan"/>
The google drawable is a png image that I exported from Adobe XD.
Could someone please give me a pointer on what I am doing wrong? Thanks.
Additionally, I realize that if I set the background of the button to android:color/white the shadow appears. So I think the issue is with the png drawable? Does elevation not work with png images? Is there a workaround?
Use below the line of code to show an elevation in button
android:outlineProvider="bounds" – K. Sopheak
That works, thanks!
try this I hope it helps, because another view or layout just after your button is hiding shadow
android:layout_marginBottom="16dp"
For Material Button, I tried the following and it worked
android:translationZ="5dp"
Since you're using an image, replace the <Button> tag with that of an <ImageButton>. So, your workaround code would be:
<ImageButton
android:id="#+id/google"
android:layout_width="270dp"
android:layout_height="38dp"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:src="#drawable/google"
android:elevation="10dp"
android:layout_below="#id/slogan"/>
Take note that the android:src attribute is being used here rather than the android:background attribute.
This one worked for me!
try this , I hope this help you ||
android:layout_margin="5dp"
When im designing my android app im using XML and for this app im using the Relative layout but when i put the buttons on my screen with the png background i made its just looking bad.. the buttons are not put equaly like they should..
Look at this picture:
How would i solve so the pluss buttons and percent and comma and equal button will be placed right instead of looking all that weird?
If you are intrested to see my XML code here is an link for pastebin:
http://pastebin.com/gpxnPT4P
I think you have to check the android:layout_... of every item that is showing wrong. For example, the declaration for the number 5 button is:
<Button android:background="#drawable/number5" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:id="#+id/number5" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/number8" android:layout_alignLeft="#+id/divided"></Button>
If you look closely the android:layout_alignLeft declaration is pointing to de divide button (#+id/divided) that is far away. There is a similar situation with the button for number 6.
Try this declaration for number 5 button:
<Button android:background="#drawable/number5" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:id="#+id/number5" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/number8" android:layout_alignLeft="#+id/number4"></Button>
Hope this helps
<ImageButton android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/media_skip_backward"
android:background="#drawable/transparent"></ImageButton>
This is what I tried to get a transparent ImageButton so as to place those buttons on a SurfaceView. But Eclipse, gives me an error in the project as soon as I include the transparent line in xml.
Please help.
Try using null for the background ...
android:background="#null"
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"
You can also use a transparent color:
android:background="#android:color/transparent"
Setting the background to "#null" will make the button have no effect when clicked. This will be a better choice.
style="?android:attr/borderlessButtonStyle"
Later I found that using
android:background="?android:attr/selectableItemBackground"
is also a good solution. And you can inherit this attribute in your own style.
in run time, you can use following code
btn.setBackgroundDrawable(null);
I believe the accepted answer should be:
android:background="?attr/selectableItemBackground"
This is the same as #lory105's answer but it uses the support library for maximum compatibility (the android: equivalent is only available for API >= 11)
Don't use null or transparent if you need a click animation. Better:
//Rectangular click animation
android:background="?attr/selectableItemBackground"
//Rounded click animation
android:background="?attr/selectableItemBackgroundBorderless"
Remove this line :
android:background="#drawable/transparent">
And in your activity class set
ImageButton btn = (ImageButton)findViewById(R.id.previous);
btn.setAlpha(100);
You can set alpha level 0 to 255
o means transparent and 255 means opaque.
The best way is using the transparent color code
android:background="#00000000"
use the color code #00000000 for making any thing transparent
Use this:
<ImageButton
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:padding="10dp"
android:src="#drawable/backbtn" />
Use ImageView... it have transparent background by default...
I was already adding something to the background so , This thing worked for me:
android:backgroundTint="#android:color/transparent"
(Android Studio 3.4.1)
EDIT: only works on android api level 21 and above. for compatibility, use this instead
android:background="#android:color/transparent"
You can use the following code works just fine by setting the background to transparent:
<ImageButton
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/media_skip_backward"
android:background="transparent"></ImageButton>
Set the background of the ImageButton as #null in XML
<ImageButton android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/media_skip_backward"
android:background="#null"></ImageButton>
Use "#null" . It worked for me.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/bkash"
android:id="#+id/bid1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#null" />
It's android:background="#android:color/transparent"
<ImageButton
android:id="#+id/imageButton"
android:src="#android:drawable/ic_menu_delete"
android:background="#android:color/transparent"
/>
It works and also keeps visual feedback for button clicked,
android:backgroundTintMode="screen"
This is programatically set background color as transparent
ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);
btn.setBackgroundColor(Color.TRANSPARENT);
Programmatically it can be done by :
image_button.setAlpha(0f) // to make it full transparent
image_button.setAlpha(0.5f) // to make it half transparent
image_button.setAlpha(0.6f) // to make it (40%) transparent
image_button.setAlpha(1f) // to make it opaque
If you want to do it in a .xml use the below code:
android:background="#null"
And, here is an example of doing it programmatically
yourButton.setBackgroundResource(0);
Dont forget to add padding, it helps to show button effect. Add padding to adjust with this background.
android:background="?android:selectableItemBackground"
android:padding="5dp"
android:background="#00000000"
// OR
android:backgroundTint="#00000000"
<ImageButton
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/media_skip_backward">
</ImageButton>
I used a transparent png for the ImageButton, and the ImageButton worked.