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"
I have Button in my layout,in that I want to display a down arrow symbol. I defined the Button in a layout file How can I set the down arrow symbol to that button,
can anyone help, thanks in advance......
Taken from the Android API guides on Buttons, you can use an ImageButton.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/down_arrow_icon"
... />
You can download the down_arrow_icon from a number of resources (like the Google Material icons page) or even create your own image.
Simply use android:drawableRight property. Like:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/your_drawable"/>
When I place a button in a layout, it has a standard gray background. However I want to make it more like the kind of button you see in the Dialogs where it's a background-less button (e.g. the white buttons with text).
Like this
Not like this... not like this...
Old question but I just stumbled on it and have the answer (or at least I think), I really don't know if that's the best approach, but it should do the job.
<Button
android:id="#+id/sign_in_button"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The trick here is the #style/Widget.AppCompat.Button.Borderless.Colored. Also see this answer, it explains a lot https://stackoverflow.com/a/36666660/1920068
try this
android:background="#android:color/transparent"
Just use a TextView instead, and put a click effect with the background:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#23ac29"
android:text="Signup"
android:textSize="18sp"
android:padding="8dp"
android:background="?android:attr/selectableItemBackground"/>
There is some closeable view in my app and it has a close button in header. I want make this button borderless and small.The code:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"
android:layout_margin="0dp"
android:padding="0dp"
android:src="#android:drawable/ic_menu_close_clear_cancel"/>
In result the button is borderless but has much empty place around cross image (button is hitted on screenshot to make empty space visible)
How could I fix it?
You should use:
android:background="?selectableItemBackgroundBorderless"
Added
android:minHeight="0dp"
android:minWidth="0dp"
android:padding="2dp"
The result
Also I'll use negative margins to place the button closer to corner.
You can use the following on your ImageButton to remove the 'border':
android:background="#android:color/transparent"
or
android:background="#null"
If you would like change the background when the user clicks the button you can create a selector. Example ImageButton doesn't highlight on click with Transparent background.
With the introduction of ConstraintLayout, you can use constraints to reduce the width of the borderless ImageButton, that's way too wide by default, by constraining it to be as wide as the height:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_closeable_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:contentDescription="#string/action_close"
android:src="#android:drawable/ic_menu_close_clear_cancel"
app:layout_constraintDimensionRatio="w,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I don't recommend you to make it any smaller than this because the touch target needs to be at least 48dp x 48dp, as for Google's Material Design guidelines, otherwise a user would have a hard time trying to touch it.
If you really need it to look like it's closer to the edges of the view, you can always use an image that's not centered (has transparent padding on one or two sides), but I'd try my best to avoid doing this workaround and try rethinking my app's design in order to accommodate the button as it is.
use textView for that..
and use textview.OnTouchListener
finally onTouch change the color of text.. thats it
or else in your code just use
android:background="#null"
<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.