Usually, there's a bit effect when we press a button. But when I set gradient background for a button by creating a shape border.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1px" android:color="#cccccc" />
<corners android:radius="6px" />
<gradient
android:startColor="#d8d8d8"
android:endColor="#f9f2f9"
android:angle="90" />
</shape>
and button:
<Button
android:id="#+id/visibility"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:drawableLeft="#drawable/idle"
android:singleLine="true"
android:padding="5px"
android:textSize="14px"
android:textColor="#444444"
android:textStyle="bold"
android:typeface="sans"
android:layout_marginLeft="5px"
android:layout_marginRight="5px"
android:background="#drawable/border"
android:clickable="true">
</Button>
the effect when press the button is lost, the button now is just like a textview, nothing is changed when I click on it.
But if I delete this line in button, everything's ok:
android:background="#drawable/border"
So any solutions to don't be lost the effect when press the button, or any solutions to make a button has border and gradient background without use shape?
Thanks!
You have to use a StateListDrawable for the button background, supplying different background images for each state (pressed, focused, etc.).
It will be much simpler to simply not change the button background at all.
Related
Ok so my buttons are not showing gradient as they should be (the gradient is not showing, its just a normal color background), idk why this is happening, everything looks like how it should be to me, can someone please take a look :
activity_main.xml
...
<Button
android:id="#+id/button1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#drawable/button"
app:icon="#drawable/ic_baseline_room_service_24"
android:fontFamily="monospace"
android:padding="15dp"
android:paddingEnd="40dp"
android:text="BROWSE BY INGREDIENTS"
android:textColor="#FFFFFF"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.084"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/viewPager"
app:layout_constraintVertical_bias="0.396" />
...
Here's a photo of the button as seen in the layout :
button (gradient not working)
button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#1746D3"
android:endColor="#6D34CF"/>
<corners
android:bottomRightRadius="900dp"
android:radius="90dp"/>
</shape>
</item>
</selector>
Here's a photo of the gradient background : gradient
However, the android:background="..." does show the mini preview at the side : button_preview
Any help is appriciated!
Replace in Button with androidx.appcompat.widget.AppCompatButton in activity_main.xml
It seems that using Button it is not possible to have the complete personalization of the widget.
Button widget handles its own background and you can only use tinting to change its color. This is an open issue and hasn't been fixed yet.
I am Working with the Buttons Which have a background that I have Created in Drawable xml File. The Buttons look Fine in Designing But on Runtime as I Press Click, its size get Shrink and When I Click on Other Button in Design the Previous button Size goes to initial Android...
here is My Button Xml File
<Button
android:id="#+id/btn_facbookid"
android:layout_width="#dimen/_95sdp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_15sdp"
android:layout_marginRight="#dimen/_10sdp"
android:background="#drawable/button_bg_rounded_corners_main"
android:fontFamily="#font/poppins_light"
android:text="#string/url"
android:textColor="#color/textcolorblack"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/tv_facebook"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/btn_facbookurl"
/>
<Button
android:id="#+id/btn_facbookurl"
android:layout_width="#dimen/_180sdp"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_15sdp"
android:layout_marginTop="#dimen/_15sdp"
android:fontFamily="#font/poppins_light"
android:background="#drawable/button_bg_rounded_corners"
android:text="#string/facebookid"
app:layout_constraintStart_toStartOf="parent"
android:textColor="#color/whitecolor"
app:layout_constraintTop_toBottomOf="#id/tv_facebook"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#+id/btn_facbookid"
/>
The selector drawable Layout layut i have Created for buttons is
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#8420CA"
android:endColor="#46CEFC"
android:angle="45"/>
<stroke android:color="#color/transparent" android:width="4dp" />
<!--corners allow us to make the rounded corners button-->
<corners android:radius="#dimen/_50sdp" />
</shape>
</item>
The Scenario is as i Click on Button It Changes its color, It changes the color but Button get Shrinked
btn_facbookurl.setOnClickListener(View.OnClickListener {
btn_facbookurl.background=
ContextCompat.getDrawable(this,R.drawable.button_bg_rounded_corners)
btn_facbookid.background=
ContextCompat.getDrawable(this,R.drawable.button_bg_rounded_corners_main)
btn_facbookurl.setTextColor(getColor(R.color.whitecolor))
btn_facbookid.setTextColor(getColor(R.color.textcolorblack))
url_facebookedit.setHint("Enter Facebook ID Here")
})
btn_facbookid.setOnClickListener(View.OnClickListener {
btn_facbookid.background=
ContextCompat.getDrawable(this,R.drawable.button_bg_rounded_corners)
btn_facbookurl.background=
ContextCompat.getDrawable(this,R.drawable.button_bg_rounded_corners_main)
btn_facbookid.setTextColor(getColor(R.color.whitecolor))
btn_facbookurl.setTextColor(getColor(R.color.textcolorblack))
url_facebookedit.setHint("Enter Facebook URL Here")
})
here is the UI
As I click on Button Facebook it Shrink and Come to origional Size as I Click on URL and Vice Versa...
You're adding a transparent stroke around the button (which makes the filled area smaller)
<stroke android:color="#color/transparent" android:width="4dp" />
I assume that file is R.drawable.button_bg_rounded_corners, and the stroke isn't there in R.drawable.button_bg_rounded_corners_main.
Also check your corner radius, I don't know what sdp is but if the radius doesn't match in both files, and one is slightly larger, it might affect the size since it's defining the entire shape and not just the corners. Maybe!
For your Button Shrink case, You should add android:layout_height value.
Specifies the basic height of the view. This is a required attribute
for any view inside of a containing layout manager
Don't
<Button
android:layout_height="wrap_content"
Try with
<Button
android:layout_height="#dimen/_50sdp"
I just updated my android Studio and every time that I try to change the background color of AppCompatButton the layout gets flat and looses the ripple effect, it wasn't like that.
And normal :
In my old project, works fine too!
<androidx.appcompat.widget.AppCompatButton
android:clickable="true"
android:onClick="Login"
android:layout_marginEnd="50dp"
android:layout_marginTop="17dp"
android:textColor="#color/primaryDarkColor"
android:layout_marginStart="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="Logar"
android:focusable="true" />
A good way to change your buttons background and keep the ripple effect is to create a separate drawable for them.
So you would create a default_button_background.xml with some pre-defined colors
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/button_ripple_color">
<item>
<shape android:shape="rectangle">
<corners android:radius="#dimen/button_corner_radius" />
<solid android:color="#color/buttonBackground" />
</shape>
</item>
</ripple>
Then you can assign this drawable to your button background like:
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/default_button_background"/>
How do I create buttons like the one below in Android? For the most part I have the main section of the button down. I can't seem to figure out the shadow and the clicking animation.
Button
<Button
android:text="Connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/connectionStatus"
android:layout_centerHorizontal="true"
android:id="#+id/button"
android:background="#drawable/button_background"
android:minWidth="300dp"
android:layout_marginTop="32dp"
android:onClick="onClick"
android:textColor="#fff"
/>
Here is the drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#21D4FD"
android:endColor="#B721FF"
android:angle="45"
/>
<corners android:bottomLeftRadius="20dip"
android:topRightRadius="20dip"
android:topLeftRadius="20dip"
android:bottomRightRadius="20dip"
/>
</shape>
This is what I currently have:
This is what I want to have:
You should try This to make gradient on your button.
to know more please click Here
I found a solution for that 4 Buttons in your image. I combined a library with my own drawable and a trick and reached a Button like you were searching for. Take a look at this and here is my Spoiler:
I am changing some button background colors before the activity is shown and when the buttons are displayed, they are missing the white space around them.
How do I get the buttons to be red, where the grey is, and keep the white spacing?
You probably have missed setting margin for layout. Actually there is a white space around your button so all the buttons are touched, so when you set background that white space also turns red. This should be okay I guess.!
Layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="About"
android:layout_margin="5dp" />
<Button
android:id="#+id/two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Gallery"
android:layout_margin="5dp"/>
<Button
android:id="#+id/third"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Media"
android:layout_margin="5dp"/>
</LinearLayout>
In strings.xml
<color name="red">#FF0000</color>
In Java
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
three = (Button) findViewById(R.id.third);
one.setBackgroundResource(R.color.red);
two.setBackgroundResource(R.color.red);
three.setBackgroundResource(R.color.red);
Output
Create a red_button.xml in drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="2dp"
android:color="#80FFFFFF" />
<corners android:radius="25dp" />
<gradient android:angle="270"
android:centerColor="#ff0000"
android:endColor="#ff0000"
android:startColor="#ff0000" />
</shape>
To get same shape as the default Button
You can play around with radius and stroke width and stroke color to get your Button as you want.
EDIT: You can add the color to original default Button like this:
Drawable d = yourButton.getBackground();
PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP); // or whatever color you like
d.setColorFilter(filter);