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
Related
<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.
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>
I am trying to set a selectable background on a linear layout with a background color. I know the usual way is android:background="?android:attr/selectableItemBackground" but i already have another code in background. Here is the snippet of the code.
<LinearLayout
android:id="#+id/number"
android:layout_width="150dp"
android:layout_height="130dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginTop="0dp"
android:background="#color/category_colors"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
The question is how do i set selectable background, there?
Try this code snipped on for size:
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
the android foreground attribute is relatively newly added XML attribute, but it does not work for API 22 and below! If this is the case then looks like we are going to have to stack attributes in a custom XML file,but don't worry, it is easier than it sounds!
1)In your project view go to res/drawable folder
2)right click the Drawable folder itself and select new>>>drawable resource file
3)Enter a file name my_custom_button.xml(the root doesn't really matter because you will replace it with the below code)
4)Click on the XML text tab (as opposed to the design view) if you aren't already there
5)Select all text and basically replace with the following:
(creating a custom color border is basically the same steps).
Feel free to change and play with the colors or to replace the gradient (mixture of colors) to a custom color of your own!
<?xml version="1.0" encoding="utf-8"?>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/category_colors">
<item android:id="#android:id/ripple">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimaryDark" />
<corners android:radius="#dimen/button_radius_large" />
</shape>
</item>
<item android:id="#android:id/background">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#color/colorPrimaryLight"
android:startColor="#color/colorPrimary"
android:type="linear" />
<corners android:radius="#dimen/button_radius_large" />
</shape>
</item>
</ripple>
HOPE THIS HELPS!!!
You should background selector XML file like below code and apply your initial colour along with press and pressed colour.
Then you would have to create the res/drawable/bg_selector.xml file like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/color_056DAE" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#color/color_056DAE" /> <!-- focused -->
<item android:color="#color/color_333333" /> <!-- default -->
</selector>
then you have to apply this file to the background of your layout in XML.
android:background="#drawable/bg_selector"
<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.
I'm looking to create a custom button. This button ideally would have an image on the left and a textview on the right. How would I accomplish this?
The fastest way
Create clickable View with Button drawable as background that contains ImageView and TextView.
<RelativeLayout
android:clickable="true"
android:background="#android:drawable/btn_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#android:drawable/ic_input_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="#+id/text"
android:text="Sample text"
android:textAppearance="?android:attr/textAppearanceButton"
android:layout_toRightOf="#id/image"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</RelativeLayout>
Other way
Create your own class, extend RelativeLayout, set content view as above, add custom methods to set text, image etc. and that's all.
Use your custom layout.
you may to use such code for that task:
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/btnLogin"
android:text="#string/text"
android:gravity="center"
android:drawableLeft="#drawable/your_image"
android:background="#drawable/login"/> // this is for nice background - *.9.png
Create a xml with the Layout you want, xreate a drawable which look like you want the Button to look, add the drawable as background to Linear Layout and inflate the xml to a custom view.
Example drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid android:color="#f3ae1b" />
<stroke android:width="1dp" android:color="#bb6008" />
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#f3ae1b"
android:endColor="#bb6008"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#bb6008" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Edit: Code to inflate
private LayoutInflater mInflater;
mInflater = LayoutInflater.from(this);
public LinearLayout yourButton;
yourButton = (LinearLayout) mInflater.inflate(R.xml.yourButton, null);
In Xml layout use this drawable layout options and get simple layouts.The following line gives the image at the left side of the text.
android:drawableLeft="#drawable/image"
If you want to write code to do this, use setCompoundDrawables() of the Button class to decide where the drawable should appear relative to text.
Look for the API for more details.
You can do this by using NoboButton
Simple and fast way to create android button with icon, radius, background
Library url https://github.com/alex31n/NoboButton
Screenshot https://raw.githubusercontent.com/alex31n/NoboButton/master/Assets/button_screenshot.jpg