click - In Android programming, how do you change the image of the button after being clicked? -


i want set button's image change when clicked. example, if want click button change. if click button b, image change , image of button revert back. how can this? badly need on one.

package com.example.mobile_nurse;  import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.view.window; import android.widget.imagebutton;  public class posture_1 extends activity {     @override     protected void oncreate(bundle savedinstancestate) {         requestwindowfeature(window.feature_no_title);         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_posture_1);         back();         _1minute();         _15minute();     }     private void _15minute() {         imagebutton button1 = (imagebutton) findviewbyid(r.id.imagebutton3);         imagebutton button4 = (imagebutton) findviewbyid(r.id.imagebutton4);         view.onclicklistener mylistener = new view.onclicklistener() {             @override             public void onclick(view v) {             button1.setbackgroundresource(r.drawable.posms1_button1_null);             button4.setbackgroundresource(r.drawable.posms1_button_a);             }         };          button1.setonclicklistener(mylistener);     }     private void _1minute() {         imagebutton button1 = (imagebutton) findviewbyid(r.id.imagebutton3);         imagebutton button4 = (imagebutton) findviewbyid(r.id.imagebutton4);         view.onclicklistener mylistener = new view.onclicklistener() {             @override             public void onclick(view v) {             button1.setbackgroundresource(r.drawable.posms1_button1);             button4.setbackgroundresource(r.drawable.posms1_button_a_null);             }         };          button1.setonclicklistener(mylistener);     }     private void back() {         imagebutton button = (imagebutton) findviewbyid(r.id.imagebutton1);         view.onclicklistener mylistener = new view.onclicklistener() {             @override             public void onclick(view v) {                 finish();             }         };          button.setonclicklistener(mylistener);     }     @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.posture_1, menu);         return true;     }  } 

setbackgroundresource updates background not image  

so use setimagedrawable(getresources().getdrawable(r.drawable.your_image))

inside _15minute() change to

button1.setimagedrawable(getresources().getdrawable(r.drawable.posms1_button1_null) button4.setimagedrawable(getresources().getdrawable(r.drawable.posms1_button_a) 

and inside _1minute() change to

button1.setimagedrawable(getresources().getdrawable(r.drawable.posms1_button1) button4.setimagedrawable(getresources().getdrawable(r.drawable.posms1_button_a_null) 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -