Change the color implementation method when the Android button is pressed _android

Source: Internet
Author: User

The demand is when I press the button, the Change button color, so that users feel that they pressed the button, when the release, change back to the original color.

Normal Time:

When pressed:

Some people say, the direct monitor button presses the event to be extremely good, actually this can achieve the same effect, but has a shortcoming, for example many buttons all need this kind of effect, that your same code must repeat many times. So, it's still going to be universal.

First, create a new folder drawable under the Res folder, which is not a resolution:

Create an XML file below: Login_button_selector.xml

Copy Code code as follows:

<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >

<item android:drawable= "@drawable/clr_normal" android:state_pressed= "false"/>
<item android:drawable= "@drawable/clr_pressed" android:state_pressed= "true"/>

</selector>

Then add the following in the String.xml file in the value folder:

Copy Code code as follows:

<drawable name= "Clr_normal" > #ff6501 </drawable>
<drawable name= "clr_pressed" > #a44100 </drawable>

Finally, add the button:

Copy Code code as follows:

<button
android:layout_margintop= "15DP"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "@string/loginsubmit"
Android:id= "@+id/login"
Android:textcolor= "@android: Color/white"
android:background= "@drawable/login_button_selector"
/>

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.