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:
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 want to make a perfect square shaped button and the shape looks like this:
Here, xml is :
<Button
android:id="#+id/button3"
android:layout_width="#dimen/box_size"
android:layout_height="#dimen/box_size"
android:layout_weight="50"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.598"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.146" />
I want to delete the blank between the line and blue square and don't know how to.
I tried to make an Image button and it also gave me unwanted result.
One way to solve this (Which I use), is to create a "Layout Resource File" and set it as the background for your button.
You could do this for example: create a "custom_background.xml" file (or any name you like), and write in it:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/colorPrimary" />
</shape>
Then add in the button code:
android:background="#drawable/button_background"
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"
Is it possible to specify border in Android button in the main.xml?
[p.s.
without the 'separate xml file containing stroke tag' but in the original file where I define the button and also
without the 'dynamically by programming' solution
and 'images' solution]
<Button
android:background="#FFFFFF"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="Player 3"
android:layout_x="0px"
android:layout_y="0px"
android:id="#+id/p3"
android:layout_weight="1"
/>
here I am changing the background dynamically but the problem is, for 2 buttons there is no border.
Try to use shape
my_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:radius="0.1dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#E8E6E7" />
</shape>
And Button
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/my_shape"
android:text="Button" />
Screenshot
I hope this will help you.
This is not the recommended way to do it because it causes overdraw and adds unnecessary views, Gunaseelan has the proper method.
There's no concept of borders as an attribute. The accepted way is to use a separate drawable as the background to the View (using stroke as you've mentioned and as #gunaseelan writes).
The other (not recommended) way is to enclose your Button in another View like a LinearLayout, set the background color to the desired border colour on the encapsulating View and padding on the outer View too.
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#342334"
android:padding="5dp"
>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="whatwhaaat"
/>
</LinearLayout>
The value of the padding will indicate the thickness of the border. This method is not recommended as you end up with an extra View in your layout, and another layer of overdraw (it draws the LinearLayout then the Button on top).
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.