0

I am having a slight issue with my android app where the text colour changes back to a default color once I rotate my screen.

Basically I have an if statement where if Player one makes a move in selecting a button (12 buttons altogether), their selection is displayed as a particular colour, if it's not player one's move then it must be Player two where their selection is marked as a different text colour.

@Override
public void onClick(View v) {
    if (!((Button) v).getText().toString().equals("")) {
        return;
    }

    if (playerOneMove) {
        ((Button) v).setText("A");
        ((Button) v).setTextColor(Color.parseColor("#e8e5e5"));
    } else {
        ((Button) v).setText("Z");
        ((Button) v).setTextColor(Color.parseColor("#737374"));
    } 

    ...

}

The above code is within OnCreate(). How can I keep my test colour within rotation? I know there is protected void onSaveInstanceState(Bundle outState) and protected void onRestoreInstanceState(Bundle savedInstanceState) but how do I call on my buttons within them?

UPDATE

    private Button btnObj1;
    private Button btnObj2;
    private Button btnObj3;
    private Button btnObj4;
    private Button btnObj5;
    private Button btnObj6;
    private Button btnObj7;
    private Button btnObj8;
    private Button btnObj9;

    private static final String TEXT_COLOR = "textColor";
    private String textColor;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

if (savedInstanceState != null) {
            textColor = savedInstanceState.getString(TEXT_COLOR);
            if(btnObj1 != null) {
                btnObj1.setTextColor(Color.parseColor(textColor));
            }
            if (btnObj2 != null) {
                btnObj2.setTextColor(Color.parseColor(textColor));
            }
            if (btnObj3 != null) {
                btnObj3.setTextColor(Color.parseColor(textColor));
            }
            if (btnObj4 != null) {
                btnObj4.setTextColor(Color.parseColor(textColor));
            }
            if (btnObj5 != null) {
                btnObj5.setTextColor(Color.parseColor(textColor));
            }
            if (btnObj6 != null) {
                btnObj6.setTextColor(Color.parseColor(textColor));
            }
            if (btnObj7 != null) {
                btnObj7.setTextColor(Color.parseColor(textColor));
            }
            if (btnObj8 != null) {
                btnObj8.setTextColor(Color.parseColor(textColor));
            }
            if (btnObj9 != null) {
                btnObj9.setTextColor(Color.parseColor(textColor));
            }
        }


        if (savedInstanceState != null) {
            textColor = savedInstanceState.getString(TEXT_COLOR);
            btnObj1.setTextColor(Color.parseColor(textColor));
            btnObj2.setTextColor(Color.parseColor(textColor));
            btnObj3.setTextColor(Color.parseColor(textColor));
            btnObj4.setTextColor(Color.parseColor(textColor));
            btnObj5.setTextColor(Color.parseColor(textColor));
            btnObj6.setTextColor(Color.parseColor(textColor));
            btnObj7.setTextColor(Color.parseColor(textColor));
            btnObj8.setTextColor(Color.parseColor(textColor));
            btnObj9.setTextColor(Color.parseColor(textColor));
        }


    setContentView(R.layout.activity_main_player2);

    for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    String buttonID = "button_" + i + j;
                    int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
                    buttons[i][j] = findViewById(resID);
                    buttons[i][j].setOnClickListener(this);
                }
            }

        @Override
        public void onClick(View v) {

            if (!((Button) v).getText().toString().equals("")) {
                return;
            }

            if (playerOneMove) {
                ((Button) v).setText("A");
                textColor = "#e8e5e5";
                ((Button) v).setTextColor(Color.parseColor(textColor));
            } else {
                ((Button) v).setText("Z");
                textColor = "#737374";
                ((Button) v).setTextColor(Color.parseColor(textColor));
            }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {


        outState.putBoolean("playerOneMove", playerOneMove);
        outState.putString(TEXT_COLOR, textColor);

super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {


        playerOneMove = savedInstanceState.getBoolean("playerOneMove");
        textColor = savedInstanceState.getString(TEXT_COLOR);

super.onRestoreInstanceState(savedInstanceState);
    }

Thanks

Merlin
  • 185
  • 2
  • 14
  • 1
    Please don't repeat questions. Simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working, will bump it to the top of the active queue. Also, if a given answer doesn't work for you, don't accept it. This indicates that your issue has been resolved, but it apparently hasn't been. – Mike M. Jun 04 '18 at 02:32

1 Answers1

1

Whenever screen is rotated and you are not handling the configuration changes, your activity will be re-created as a result all the state of your views won't be maintained. If you can use onSaveInstanceState to store the state as follows:

private static final String TEXT_COLOR = "TEXT_COLOR";
private String textColor;
private Button btnObj;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    btnObj = findViewById(<button id>);
    if (savedInstanceState != null) {
        textColor = savedInstanceState.getString(TEXT_COLOR);
        btnObj.setTextColor(Color.parseColor(textColor));
    } 
    ...
}

@Override
public void onClick(View v) {
    if (!((Button) v).getText().toString().equals("")) {
        return;
    }

    if (playerOneMove) {
        ((Button) v).setText("A");
         textColor = "#e8e5e5";
        ((Button) v).setTextColor(Color.parseColor(textColor));
    } else {
        ((Button) v).setText("Z");
        textColor = "#737374";
        ((Button) v).setTextColor(Color.parseColor(textColor));
    } 

    ...

}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putString(TEXT_COLOR, textColor);
    super.onSaveInstanceState(savedInstanceState);
}
Sagar
  • 23,903
  • 4
  • 62
  • 62