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

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" />

Related

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

Android use vector drawable to replace text in button

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.

Android SDK: Button becomes colored when changing the background color on LinearLayout

I have a LinearLayout in an Android app I am creating now which contains a default button with gradient gray color. This LinearLayout is now white but when I try to change the background color to yellow the button also becomes yellow which I don't want to happen. I also tried to use a color image to set the background color on the LinearLayout but the same thing happens. How do I solve this problem? Here is the code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:background="#color/yellow">
<Button
android:id="#+id/buttonCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
SOLVED: Wait, it actually works now. while the buttons become colored and transparent in the layout view in Eclipse the buttons is unaffected when I run the app on the phone. I thought that it would display the same result when the app is runned. Strange how it become that way in the layout view...
In that case you may set a gray background color android:background="#A4A4A4" for button also..
Just set the background on your button separately. You can use a state list drawable, i.e. a different background for each state of the button (pressed, focused, etc) to make it compeletely custom.
Why the background color changes with the layout I'm not sure. It sounds like your button is default to transparant. Check your style and theme to make sure that's not affecting what's displayed.

Making a semi-transparent button such that button color should be visible and background should be visible

I have been trying to achieve this semi-transparent button, but I always failed on making that. I have referred many like this. But still no luck. I tried with android:color="#66FF0000" too, but it doesn't make it semi-transparent. Below is my code.
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="select"
android:onClick="selectClick"
android:color="#80000000"
/>
I want this type of rectangular button with semi transparent so that blue color of button should be visible and green color of the activity should also be visible. Can someone please suggest me? In the below picture I was unable to draw green color on the button to show what exactly I want. But I guess my above explanation is understandable.
Or if the blue color is not possible to be made visible, at least I want it to look like to give a user feel that there is a button and the green color should be visible.
Use
android:background="#80000000"
instead of
android:color="#80000000"

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

Categories

Resources