Android imagebutton selector example

Source: Internet
Author: User

Generally, you can use imagebutton to display a button. However, sometimes we want to change the button status, for example, before the button is pressed, after the button is pressed, it is another way. Android allows us to change the button image depending on different states, if the button is set or the button is pressed. The following describes how to implement it:

P.s this project is developed in eclipse 3.7, and tested with Android 2.3.3.

1. Add an image to resources to prepare three images with different button states, and then put the image " Resource/drawable"
.
  1. Button_normal_green.png-Default image button.
  2. Button_focused_orange.png-
    When a button is followed, for example, it is displayed when the phone keyboard moves (Focus.
  3. Button_pressed_yellow.png-
    Displayed when the button is pressed

2. Add selector for different button States and create a new XML layout file in "Res/drawable/". Here we name it" new_button.xml". This XML file defines which button image the button status belongs.

File: Res/drawable/new_button.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/button_pressed_yellow"          android:state_pressed="true" />    <item android:drawable="@drawable/button_focused_orange"          android:state_focused="true" />    <item android:drawable="@drawable/button_normal_green" /></selector>
3. Add button

Open the "Res/layout/Main. xml" layout file, add a normal button, and then add a background image for this button.

File
: Res/layout/Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <button Android: id = "@ + ID/imagebuttonselector" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: background = "@ drawable/new_button"/> // This method is used to implement </linearlayout>
4. the activity code is like a normal button and a simple click listener package com. Demo. app;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. Toast;

Public class apptestactivity extends activity {

/* (Non-javadoc)
* @ See Android. App. Activity # oncreate (Android. OS. Bundle)
*/
Button imagebutton;
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Imagebutton = (button) findviewbyid (R. Id. imagebuttonselector );
Imagebutton. setonclicklistener (New onclicklistener (){

Public void onclick (view v ){
Toast. maketext (apptestactivity. This, "eee", Toast. length_short );

}
});
}

}
5. Run

Run the application
1. Result: The default button. (button_normal_green.png)


2.
Button focus. (button_focused_orange.png)


2.
Button is pressed. (button_focused_orange.png)

This application is still very extensive, such as player control and game status.

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.