Android buttons and textview dynamically change colors

Source: Internet
Author: User

In order to achieve better user interaction, you often need to change the background color of the button and the font color of the textview to indicate the operational status of the user's current menu, android provides a method to define a selector so that programmers can configure the background and color values in advance. The specific method is as follows:

1. Button (imagebutton, imageview, including general view, viewgroup can all perform the same operation ):

1. First, add the background attribute for the button in your layout file, such as Android: Background = "@ drawable/btn_background"

 

2. Add the new XML file btn_background.xml in the drawable directory, that is, define some selector labels and specify the background image of the button in various states. The following code:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <selector xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <item Android: state_focused = "true" <br/> Android: state_pressed = "true" <br/> Android: drawable = "@ drawable/button_focused"/> <br/> <item Android: state_focused = "false" <br/> Android: state_pressed = "true" <br/> Android: drawable = "@ drawable/button_defocused"/> <br/> <item Android: state_focused = "true" <br/> Android: drawable = "@ drawable/button_focused"/> <br/> <item Android: state_focused = "false" <br/> Android: drawable = "@ drawable/button_defocused"/> <br/> </selector> 

3. Put the image used in the above XML file under drawable.

 

2. textview changes the font color. The difference between textview and button is that the textcolor attribute is changed, and the selector file is defined in the color directory:

 

1. Specify the textcolor attribute of textview in the layout file, for example, Android: textcolor = "@ color/textview_color ";

 

2. Add the new XML file textview_color.xml in the color directory and specify the color value of textview in various states. The following code:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <selector xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <item Android: state_selected = "true" Android: color = "# fff"/> <br/> <item Android: state_focused = "true" Android: color = "# fff"/> <br/> <item Android: state_pressed = "true" Android: color = "# fff"/> <br/> <item Android: color = "#000"/> <br/> </selector> <br/> 

3. OK.

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.