Android use vector drawable to replace text in button - android

Currently I have a button that looks like this:
What I want to do is to be able to replace the text with this vector drawable but keep the blue rectangle background:
I looked into doing this but I cannot seem to get it right.
I tried the following combination:
<Button
android:id="#+id/deletebutton"
android:layout_width="0dp"
android:layout_height="50dp"
android:src="#drawable/deletebtn24dp"
android:backgroundTint="#color/primaryButtonColor"
android:layout_weight="1"/>
However that just led to a blank blue button with no drawable. What can I do so the drawable replaces the text?
Also I would like to support API levels start at 16+.

You have to use app:srcCompat="#drawable/deletebtn24dp" instead of android:src="#drawable/deletebtn24dp". Using an ImageButton might also be better than using a straight Button.

Related

How to make a transparent button with borders and ripple effect?

There are a few answers on stackoverflow, but none of them seem to help. What I'm trying to achieve is not only the transparent background but also the glowing that happens when the button is clicked and the borders as well.
What I tried so far:
android:background="?android:attr/selectableItemBackground"
The results (when the button is not clicked):
The results (when the button is clicked):
This is basically what I want to achieve, except that the button have no borders.
I tried to set strokeColor and strokeWidth but that didn't do anything. I still get the same results as above.
I also tried to use the Material Outlined Button style which worked fine however, I couldn't get the glowing effect when the button is tapped to work. It was basically like clicking in a textView.
The closest I could find to my problem is this answer: Shaped drawable with selectableItemBackground as background
It works perfectly, except for the border color. For some reason, it's showing PURPLE (which is my primary color) instead of black as it should.
If you use a Material Components theme, and apply the Material outlined button style to a button, it will appear as I think you're describing.
<Button
android:id="#+id/button"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button" />

How to set a background Image to a Button BUT STILL WORKS LIKE A NORMAL BUTTON (With Effects)?

I have been trying to achieve the Normal android button effects when the button is already set with a background image
But the problem is, once the background is set, the button default nature is gone (shadows, clickable animation etc..)
Can Someone please give me a working solutions for this?
Here's the code to my button
<Button style="#style/Widget.AppCompat.Button.Colored"
android:id="#+id/student"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/student"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintVertical_bias="0.891" />
It looks amazing since I have set a background image but It still looks like you have set an image to a jLabel in Java)
How can I get the default button looks while keeping my background image intact?
Button works by setting the background to a background with the elevation, shadows, ripple, etc. To get the same behavior, you're going to need to set your background to a StateListDrawable that performs the ripple effect and puts the shadows and elevation effects with your image as the background inside of all that. You can't just set the background to be your image, that will lose all of those features. The easiest way to get that right is to look at the AOSP source code and find the drawable it uses, copy it to your project, and edit appropriately.
Basically in Android a Button is just a TextView with a special background preset.
Why don't you use ImageButton widget ? It has the same behaviour as a Button and ImageView combined
Check this tutorial to see how to work with it.
as mentionned in the official documentation
Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button, with the standard button background that changes color during different button states. The image on the surface of the button is defined either by the android:src attribute in the XML element or by the ImageView.setImageResource(int) method

Make a Button's background transparent

I am very new to Android Studio and creating apps in general.
I have created an Image Button and uploaded an Image, problem is that I want to make the background of the Button transparent.
This is what I mean:See Example
The background of the left button is black and I want to make it transparent, like the Right Button.
Just put this property inside ImageButton tag:
android:background="#android:color/transparent"
----------OR--------------
You can also user vector image for this:
Click on Vector Asset
Click on small icon image(just below ic_android_black_24dp)
Search any icon and add to your drawable. The icon you are using is appeared in search result. use this list icon.
Try This
<ImageButton
android:layout_width="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/image"
android:layout_height="wrap_content" />

Android imagebutton: how to remove tinted rectangle around image?

I'm developing and Android app. I have a main menu with lots of imagebuttons, each of which takes the user to a new view on click.
The problem I'm having is that surrounding each icon is a tinted rectangle that changes to a light blue colour when tapped. How do I remove this transparent square in the .xml layout file?
Many thanks in advance,
they probably have the default background from the android system. to remove that, define a transparent color
<color name="transparent">#00000000</color>
and then use it as background in your buttons
<ImageButton android:src="#drawable/button"
android:background="#color/transparent"/>
I think it will work the way you want
You could always use the framework's
android:background="#android:color/transparent"
But for what you are trying to archive this is the better solution:
android:background="#null"
I know theres now rectangle or colour, when you use your own buttons.. create, implement and use them, then you wont have the rectangle

Removing Android ToggleButton's green indicator light

I would like my app to have a day of week selector with multiple day selection capability. I'm using ToggleButtons for this right now, but because of the indicator lights a ToggleButton takes too much space on the screen. Without the lights, my ToggleButtons would look like normal (toggleable) Buttons and they could fit in one row. How can I hide the lights?
The answer is this part:
android:background="#android:drawable/btn_default"
For example, following will make the light disappear and toggle button would look like a default button, but with the toggle functionality:
<ToggleButton android:id="#+id/your_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOn="On" android:textOff="Off"
android:background="#android:drawable/btn_default" />
You can use a custom android:background drawable for them, that will override the graphics including the indicator light. If you look at Android: using framework drawables in custom button selector, there's instructions for copying resources from the SDK to your own project. You presumably could copy the platform normal button drawable and use that as your background.

Categories

Resources