Question

Aftermath of this: Image for ImageButton doesn't change as intended

When ImageButton of "ibChamp" is clicked, it opens up champions.xml. In that layout, there is an Imagebutton called "ibAnnie". What I want that to do when clicked is, to change the image of "ibChamp", and to switch to that layout. What happened here is that after "ibAnnie" is clicked, it switches to "create_build_page.xml", and in that is a changed picture of "ibChamp". But that only happens for a split second before changing back to the original, unchanged picture, "create_build_page.xml" layout. When I click the back button, it goes to the "create_build_page.xml" layout with the changed picture, but the buttons do not work. I would gladly provide any additional information required.

Was it helpful?

Solution

I'm still not entirely clear on what you're trying to achieve... but if my understanding is correct, this should get you closer:

@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) {
            RelativeLayout test = (RelativeLayout) findViewById(R.id.layoutChampions);
            LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View createView = layoutInflater.inflate(R.layout.create_build_page, null);
            test.addView(createView);

            ImageButton img = (ImageButton) view.findViewById(R.id.ibChamp);
            img.setImageResource(R.drawable.ic_launcher);
            img.setTag(R.drawable.ic_launcher); // so you can retrieve it later
            img.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    // this will set the imageView of ibAnnie
                    ibAnnie.setImageView((Integer)view.getTag());
                    // this will remove the selection view from your layout
                    ((RelativeLayout) findViewById(R.id.layoutChampions)).removeChild(createView);
                }
            });

            // this opens another Activity... you don't want that, at least not for what you seem to be trying to do.
            /*try {
                Intent open = new Intent("android.intent.action.CREATE");
                startActivity(open);
            } catch (Exception e) {

            }*/

        }
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top