Set button radius as a theme - java

<Button android:id="#+id/gp_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:onClick="gp"
android:text="#string/go_gplay"
android:textAllCaps="false"
android:theme="#style/ButonTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<style name="ButonTheme" parent="ButonTheme">
<item name="android:backgroundTint">#ffffff</item>
<item name="android:textColor">#121212</item>
<item name="android:topLeftRadius">20dp</item>
<item name="android:bottomLeftRadius">20dp</item>
<item name="android:topRightRadius">20dp</item>
<item name="android:bottomRightRadius">20dp</item>
</style>
How can I edit button border radius as a theme, as you see i set the radius to 20dp and in preview it shows ok, but when compile and run it in phone it shows the radius by default.(i know the other "trick" to make a shape and then set it as button background, but in this way i will loose the "material" default animation)

Make a drawable.. and then apply the drawables filename as a background to the <Button> within your layout.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<stroke android:width="3dp"
android:color="#ff000000" />
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" />
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>

try setting radius for whole corners , <item name android:radius="20dp"/>
or you maybe can put your button in a card view and curve it's corners.
but the typical way is using shape.

Related

Android Studio, color in selector (in item) doesnt work

<Button
android:id="#+id/sort_time"
android:layout_width="0dp"
android:layout_height="52dp"
android:layout_marginTop="54dp"
android:background="#drawable/roundless_button"
android:backgroundTint="#2A56C6"
android:text="#string/order_by_time"
android:textSize="14sp"
app:layout_constraintEnd_toStartOf="#+id/sort_status"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/IndBlue"
>
<shape android:shape="rectangle">
</shape>
<solid android:color="#color/IndBlue" />
</item>
</selector>
I tried to edit the buttons colors, but for some reason it won't change. Solid android:color / android:drawable doesn't work even though I placed it everywhere.
when you use selector as button background it inherits the color from theme. you can try changing the theme primary color and that will affect the button color.

How to set dropdown arrow from drawable in spinner?

I am trying to set drawable as an arrow in Spinner but bitmap is not supported.
I used svg file (ic_arrow.xml) and set it in spinner_arrow.xml file:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp"
/>
<solid android:color="#color/white" />
<stroke android:width="2px" android:color="#color/grey" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<bitmap android:gravity="center|right" android:src="#drawable/ic_arrow"/>
</item>
</layer-list>
So I used it for background in layout file
<Spinner
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/spinner_arrow"/>
But I have an error in displaying Spinner: "Binary XML file line #41 in myapp:layout/activity_registration_driver: Error inflating class Spinner".
I think the problem is in adding drawable for arrow in spinner but i don't know how to solve it
Please use below code for add arrow with spinner:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/btn_height_common"
android:layout_marginLeft="#dimen/_15sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_marginRight="#dimen/_15sdp"
android:background="#drawable/bg_editext">
<Spinner
android:id="#+id/sp_gender"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:entries="#array/gender"
android:fontFamily="#font/gotham_narrow_book"
android:overlapAnchor="false"
android:paddingLeft="#dimen/_10sdp"
android:paddingRight="#dimen/_10sdp"
android:textColor="#color/gray_text_color"
android:textSize="#dimen/normal_textsize" />
<ImageView
android:layout_width="#dimen/_15sdp"
android:layout_height="#dimen/_15sdp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/_15sdp"
android:padding="#dimen/_2sdp"
android:src="#mipmap/ic_down_gray" />
</RelativeLayout>
Output:
Basically one needs to create a custom background for a spinner. It should be something like this:spinner_background.xml
<item>
<layer-list>
<item>
<color
android:color="#android:color/white"/>
</item>
<item>
<bitmap
android:gravity="center_vertical|right"
android:src="#drawable/ic_arrow_drop_down_black_24dp"/>
</item>
</layer-list>
</item>
Then create a custom style for your spinner, where you specify the above selector as background:
<style name="Widget.App.Spinner" parent="#style/Widget.AppCompat.Spinner">
<item name="overlapAnchor">true</item>
<item name="android:background">#drawable/spinner_background</item>
</style>
And finally in your app theme you should override two attributes if you want it to be applied all across your app:
<item name="spinnerStyle">#style/Widget.App.Spinner</item>
<item name="android:spinnerStyle">#style/Widget.App.Spinner</item>

Change Colors States of Button using a Generic Drawable

I have Button that is using a custom drawable to make rounded corners. But I would like to change the background colors for the different states, focused, pressed etc..
I have used the below in onCreate to change the overall background color. But I don't know how to target the state_pressed to change its color too.
Any help?
in onCreate
DrawableCompat.setTint(btnLogin.getBackground(), ContextCompat.getColor(getApplicationContext(), R.color.colorGreen));
Button
<Button
android:id="#+id/btnLogin"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#drawable/button_rounded"
android:text="Sign In"
android:textColor="#android:color/white"
android:textStyle="bold" />
button_rounded.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="10dip" />
<solid android:color="#5c89c1" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle">
<corners android:radius="10dip" />
<solid android:color="#204778" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="10dip" />
<solid android:color="#204778" />
</shape>
</item>
</selector>
Try this and it work for me:
Drawable drawable = getResources().getDrawable(R.drawable.button_rounded);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable.mutate(), getResources().getColor(R.color.colorGreen));

Android studio- Adding style to buttons

I want to add a shadow and have rounded edges on my button, like box-shadow and border radius in CSS how can I do this in XML?
XML code:
<Button android:id="#+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="198dp"
android:layout_height="69dp"
android:layout_weight="1"
android:text="#string/dummy_button"
android:fontFamily="trebuchet ms"
android:clickable="true"
android:onClick="printStarter"
android:background="#8c96ff"
android:layout_gravity="center_horizontal|bottom" />
The Android Developer's Guide has a detailed guide on this: Shape Drawbables.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#1a1a1a" />
button with shadow
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shadow"/>
<item
android:drawable="#drawable/button"
android:bottom="4px" />
</layer-list>
</shape>
Refer this tutorial
You can use XML, here are some links:
How to provide shadow to Button ;
http://zrgiu.com/blog/2011/01/making-your-android-app-look-better/ . OR
You can use a 9patch image: http://developer.android.com/tools/help/draw9patch.html

Android radius button

I have a button like this
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:text="ButtonText"
style="#style/ButtonText"/>
and this style
<style name="ButtonText">
<item name="android:layout_margin">3dp</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">2</item>
</style>
Now I want to add radius to button, I have tried <item name="android:radius">6dp</item> but it is not working. What is a solution for this?
In your drawable folder, create a round_button.xml with
<?xml version="1.0" encoding="utf-8"?>
<!-- http://www.dibbus.com/2011/02/gradient-buttons-for-android/ -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners
android:radius="2dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
and then in the xml containing the button, set the background as:
android:background="#drawable/round_button"
I believe to add radius to a button you have to create a custom drawable (see this link, specifically the shape drawable section)
then, with the drawable defined, set the background xml tag of the button equal to your drawable file:
android:background="#drawable/your_file"

Categories

Resources