I've got this ToggleButton which is supposed to change the drawables (and functions) of some other buttons upon toggling on/off.
So far i've only managed to make it work one way, by using this code.
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggle = (ToggleButton) findViewById(R.id.bRedGreen);
toggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
minuskugle = (Button) findViewById(R.id.bBallhole);
minuskugle.setBackgroundResource(R.drawable.redballinhole);
}
});
}
How do you make it change the drawable back to default upon another click?
_ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _
This works for one entry.
However if i add multiple entries it comes with an error for the "else".
This works:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pluskugle = (Button) findViewById(R.id.bBallhole);
minuskugle = (Button) findViewById(R.id.bBallhole);
pluskegle = (Button) findViewById(R.id.bKegle);
minuskegle = (Button) findViewById(R.id.bKegle);
if(toggle.isChecked())
minuskugle.setBackgroundResource(R.drawable.redballinhole);
else
pluskugle.setBackgroundResource(R.drawable.whiteballhole);
This doesn't
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pluskugle = (Button) findViewById(R.id.bBallhole);
minuskugle = (Button) findViewById(R.id.bBallhole);
pluskegle = (Button) findViewById(R.id.bKegle);
minuskegle = (Button) findViewById(R.id.bKegle);
if(toggle.isChecked())
minuskugle.setBackgroundResource(R.drawable.redballinhole);
minuskegle.setBackgroundResource(R.drawable.redkegle);
else
pluskugle.setBackgroundResource(R.drawable.whiteballhole);
pluskegle.setBackgroundResource(R.drawable.redkegle);
Try this:
ReplyDelete@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggle = (ToggleButton) findViewById(R.id.bRedGreen);
toggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
minuskugle = (Button) findViewById(R.id.bBallhole);
if(toggle.isChecked())
minuskugle.setBackgroundResource(R.drawable.redballinhole);
else
minuskugle.setBackgroundResource(R.drawable.xy);
}
});
}
dummy solution
ReplyDeleteKeep a boolean somewhere to save the state
boolean isPressed = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggle = (ToggleButton) findViewById(R.id.bRedGreen);
toggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
minuskugle = (Button) findViewById(R.id.bBallhole);
if(!isPressed)
minuskugle.setBackgroundResource(R.drawable.redballinhole);
else
minuskugle.setBackgroundResource(R.drawable.yourotherdrawable);
isPressed = !isPressed;
}
});
}