Implementing a custom button in Android

Source: Internet
Author: User

There are times in the project that consider aesthetics and require a custom button.

1. Create a new folder in the Res folder of the project drawable and create a new shapes.xml (resource that implements the shape and color of the button)

2. Set android:background= "@drawable/shapes" in the button property of Mainactivity.xml.

Color changes for rounded buttons, no clicks, and clicks.

The effect is as follows:

Shapes.xml

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android">         <!--the initialization color of the view click Gradient gradient android:angle gradient angle, 0 from top to bottom, 90 for left to right, 45 for integer times default to 0; Android:startcolor start color an Droid:endcolor End color Android:type gradient style liner linear gradient radial annular gradient sweep view sizes size Corners fillet  -    <Itemandroid:state_pressed= "true">        <Shape>            <GradientAndroid:angle= "+"Android:endcolor= "#308014"Android:startcolor= "#308014" />            <sizeAndroid:height= "60DP"Android:width= "120DP" />            <CornersAndroid:radius= "8DP" />        </Shape>     </Item>    <!--Initialize color when view is not clicked -    <Item>        <Shape>            <GradientAndroid:angle= "+"Android:endcolor= "#32CD32"Android:startcolor= "#32CD32" />            <sizeAndroid:height= "60DP"Android:width= "120DP" />            <CornersAndroid:radius= "8DP" />        </Shape>   </Item></selector>

Mainactivity.xml

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity "> <TextView Android:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world"/> <Button Android:id= "@+id/stop"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignbaseline= "@+id/start"Android:layout_alignbottom= "@+id/start"Android:layout_marginleft= "30DP"Android:layout_torightof= "@+id/start"Android:text= "Stop"Android:background= "@drawable/shapes"/> <Button Android:id= "@+id/unbind"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignbaseline= "@+id/bind"Android:layout_alignbottom= "@+id/bind"Android:layout_alignleft= "@+id/stop"Android:text= "Unbind"Android:background= "@drawable/shapes"/> <Button Android:id= "@+id/start"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/textview1"Android:layout_below= "@+id/textview1"Android:layout_marginleft= "17DP"Android:layout_margintop= "108DP"Android:background= "@drawable/shapes"Android:text= "Start"Android:textcolor= "#FFFFFF"/> <Button Android:id= "@+id/bind"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/start"Android:layout_below= "@+id/start"Android:layout_margintop= "43DP"Android:background= "@drawable/shapes"Android:text= "Bind"/></relativelayout>

Implementing a custom button in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.