Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,
I am trying to change the background image of a view onclick, here is the code:

Java
public class ScreenChanger extends Activity{
	
	private ImageButton imagebutton;
	private ImageButton imagebuttonRED;
	private ImageButton imagebuttonBLUE;
	private ImageButton imagebuttonYELW;
	private ImageButton imagebuttonGRN;
	private View layout;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.screenview);
	    imagebutton = (ImageButton) findViewById(R.id.imageButton1);
	    
	    imagebuttonRED = (ImageButton) findViewById(R.id.ImageButton04);
	    imagebuttonBLUE = (ImageButton) findViewById(R.id.ImageButton02);
	    imagebuttonYELW = (ImageButton) findViewById(R.id.ImageButton03);
	    imagebuttonGRN = (ImageButton) findViewById(R.id.ImageButton01);
	    
	    layout = (View) findViewById(R.layout.screenview);
	
	    imagebutton.setOnClickListener(new OnClickListener() {

	        public void onClick(View v) {
	            setContentView(R.layout.main);
	            Intent myIntent = new Intent(ScreenChanger.this, FlashLightActivity.class);
	            ScreenChanger.this.startActivity(myIntent);
	        }
	    });
	
	    imagebuttonRED.setOnClickListener(new OnClickListener() {

	        public void onClick(View v) {
	        	setContentView(layout);
	            layout.setBackgroundColor(drawable.red);
	        }
	    });
	    imagebuttonBLUE.setOnClickListener(new OnClickListener() {

	        public void onClick(View v) {
	        	setContentView(layout);
	            layout.setBackgroundColor(drawable.blue);
	        }
	    });
	    imagebuttonYELW.setOnClickListener(new OnClickListener() {

	        public void onClick(View v) {
	        	setContentView(layout);
	            layout.setBackgroundColor(drawable.yellow);
	        }
	    });
	    imagebuttonGRN.setOnClickListener(new OnClickListener() {

	        public void onClick(View v) {
	        	setContentView(layout);
	            layout.setBackgroundColor(drawable.green);
	        }
	    });
	    
}
}


The problem i have is when i click the button it closes my app
Posted

1 solution

don't use setContentview

make your layout id
get that id and try to work on it..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900