Code formatting error occurs when I tried to post a question on Stack Overflow.

Error message

Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.

My Post Script

**Expected result**

A button can act in a way like a toggle, such that,
(1) first time clicking on it, the button moves rightward;
(2) second time clicking on it, the button moves leftward;
(3) third time clicking on it, the button moves rightward;
(4) and so on.


the flag variable `flagToggleButton` cannot be used in button's OnClickListener event listener callback function.


<!-- language: lang-java -->

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.widget.Button;
    public class Main extends Activity {
        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
            final Button b = (Button) findViewById(R.id.button1);
            boolean flagToggleButton = false;
            b.setOnClickListener(new OnClickListener () {
                public void onClick(View arg0) {
                    if (flagToggleButton == false) {
                        Animation anim = AnimationUtils.loadAnimation(Main.this, 
                        flagToggleButton = true;
                    else {
                        Animation anim = AnimationUtils.loadAnimation(Main.this, 
                        flagToggleButton = false;
  • 6
    Try formatting your list to be an actual list, or possibly ditch the semicolons. My guess is that it's probably triggering based on multiple lines ending with a semicolon (which you would commonly see in code). – animuson Jul 2 '13 at 6:44
  • Hooray~ That's the answer! Thank you~ – George Jul 2 '13 at 6:53
  • 3
    @animuson nice catch, worthy of answer! :) – Shadow Wizard Wearing Mask Jul 2 '13 at 6:54

You must log in to answer this question.

Browse other questions tagged .