Skip to main content

<ToggleButton> onClickListener - changing drawable back to default



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);




Comments

  1. Try this:

    @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);


    }

    });
    }

    ReplyDelete
  2. dummy solution

    Keep 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;
    }
    });
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.