java - Image for ImageButton doesn't change as intended -


my goal here change image imagebutton(ibchamp).

package com.example.custombuilds;  import android.app.activity; import android.content.context; import android.content.intent; import android.os.bundle;z import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.widget.imagebutton;  public class champions extends activity {  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.champions);      imagebutton ibannie = (imagebutton) findviewbyid(r.id.ibannie);      ibannie.setonclicklistener(new onclicklistener() {          @override         public void onclick(view arg0) {             // todo check errors             relativelayout test = (relativelayout) findviewbyid(r.id.layoutchampions);             layoutinflater layoutinflater = (layoutinflater) getsystemservice(context.layout_inflater_service);             view view = layoutinflater.inflate(r.layout.create_build_page, null);             test.addview(view);             imagebutton img = (imagebutton) view.findviewbyid(r.id.ibchamp);             img.setimageresource(r.drawable.ic_launcher);              try {                 intent open = new intent("android.intent.action.create");                 startactivity(open);             } catch (exception e) {              }          }     }); } } 

note imagebutton ibchamp xml layout create_build_page, , not xml champions. code runs without crashing, image imagebutton ibchamp not change, trying do. happy provide other additional information.

you inflating "ibchamp" in onclick. creates new imagebutton. not see until use "addview" parent.

you can add xml, need reference existing button in activity in order change it. or else add new button , remove old one...

in other words, change this:

        layoutinflater layoutinflater = (layoutinflater) getsystemservice(context.layout_inflater_service);         view view = layoutinflater.inflate(r.layout.create_build_page, null);         imagebutton img = (imagebutton) view.findviewbyid(r.id.ibchamp); 

to this:

        imagebutton img = (imagebutton)findviewbyid(r.id.ibchamp); 

and want.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -