Android button elevation shadow not working - android

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"

Related

How to set drawable with text in button android

I am trying to set drawable in my button but its not showing properly. Here is what I want to achieve
but I am unable to get the required results.
This is what I am doing
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/btnShareStore"
style="#style/yellowButton"
android:layout_marginTop="#dimen/margin_large"
android:layout_marginBottom="#dimen/margin_small"
android:layout_weight="1"
android:drawableStart="#drawable/ic_vector_share"
android:text="#string/lbl_share_store"
android:layout_marginStart="#dimen/margin_large"
android:layout_marginEnd="#dimen/margin_small"/>
and this is how it looks like
How can I move drawable to the adjacent left side of text?
Any help will be highly appreciated
You can try with this, keep in mind that it will need your app theme to be a descendant of MaterialTheme.
<com.google.android.material.button.MaterialButton
app:icon="#drawable/icon_search"
android:text="Hello"
app:iconGravity="textStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

ImageButton Border Transparancy - Removing the square border around round image is not working?

Hello Guys I need some help in creating Imagebutton. I created custom button on PS and then saved it in PNG format with transparent background. After that i patched my image with 9patch. Now i am using that image as a button in my layout but the image is showing colored background corners. I tried alot of things but its not removing.
Here is my XML
<ImageButton
android:id="#+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/registerbutton"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="123dp" />
Keep the background attribute as #null like this :
android:background="#null"
If still you are getting that corner, it might be from the png file that you are using.
This is an example of what i get from #null
Probably the ouput of "9patching" operation added corners. Open the file and check if there are corners or not.
If that won' help, try switching android:src attribute with android:background attribute.
And u can make .png button online using this site

Button with image: remove background color

I'd like to have an image in a button with text and I've already have it. But the problem is that my image is with some " grey background colour" (as you can see below) that I would like to remove.
In a normal situation I would use android:background="#null" and it would solve it, but I'm using as background a template to my button.
<Button
android:drawableLeft="#drawable/profile_32"
android:layout_marginRight="25dp"
android:layout_marginBottom="10dp"
android:textColor="#color/yellow3"
android:background="#drawable/buttons2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="#string/button_profile"
android:textSize="18dp"
android:textStyle="bold"
android:id="#+id/buttonProfile"
/>
Does anyone know any other way to remove that grey background color from the image? Thanks.
Is the grey actually part of the image? If not you could try background:"#android:color/transparent"
If the grey background is part of the profile_32 image, there is not an easy way to remove it in Android. You will need to have the image re-exported or use an image editing program to remove the background.
Yes, use an ImageButton and just add
android:background="#00000000"
or
android:background="#null"
to your Button.
The first 2 "0" is alpha, "00" = trasparent

Can't get rid of bottom padding on button

I have a button. It seems to have bottom padding I cannot get rid of:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Foo"/>
In the resource editor, I can click the button, and you see the padding below the bottom edge of the button there. This seems to block me from properly centering the button vertically in a parent RelativeLayout.
I tried setting padding=0dip and layout_margin=0dip, no effect. That bottom padding persists.
Thanks
The padding is in the 9-patch of the buttons themselves.
I'd advise against trying to compensate because these graphics resources can change without notice. You'd be better off building your own 9-patch or editing the existing one to remove the padding.
setting android:layout_marginBottom="-5dp" for the button worked nicely for me.
I am late to answer this but thought to share if someone come across similar use case( Removing all the inner and outer padding in Button )
<Button
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="-5dp"
android:layout_marginLeft="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="-5dp"
android:minHeight="-2dp"
android:minWidth="-2dp"
android:text="TextView"
android:textSize="12sp" />
Hmn really strange. Never noticed that. Probably because u usally want a little space around your buttons.
Also tried margin/padding=0dp and did in fact not work.
You could set android:layout_marginBottom="-10dp" however :).

How to have a transparent ImageButton: Android

<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.

Categories

Resources