1

I have main.xml and main.class that has two buttons named : Level 1 and Level 2

each button has a function to open another class

level1.setOnClickListener(new View.OnClickListener() {

                   @Override
                  public void onClick(View v){
                       // TODO Auto-generated method stub
                       Intent i =new Intent(getApplicationContext(),level1.class);
                       startActivity(i);              

    level2.setOnClickListener(new View.OnClickListener() {

               @Override
              public void onClick(View v){
                   // TODO Auto-generated method stub
                   Intent i =new Intent(getApplicationContext(),level2.class);
                   startActivity(i);             
                    }             
              });   

but the condition is level 2 button are locked but i dont know how to make a locked button, The first thing i want to ask is how to make a locked button

and to unlock level 2 button , level1.class event must completed. in level1.class has code like this

public class level1 extends Activity implements OnClickListener {

    int gamelifes=6, index=0, score=0;
    String [] answer ={"the answer"};

    lifes =(TextView)findViewById(R.id.lifestext);
    lifes.setTextColor(Color.RED);
    lifes.setText(String.valueOf(gamelifes));

    buttonanswer = (Button)findViewById(R.id.buttonanswer);
    buttonanswer.setOnClickListener(this);

    good =(ImageView)findViewById(R.id.youwin);
    good.setOnClickListener(this);

    buttonfinish = (Button)findViewById(R.id.finish);
    buttonfinish.setOnClickListener(this);

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub            

    edittext=(EditText)findViewById(R.id.editText); 
    if(v==buttonanswer) {

        String abc =edittext.getText().toString();              
        if(abc.equalsIgnoreCase(answer[index]))
        {
           good.setVisibility(View.VISIBLE);
           buttonfinish.setVisibility(View.VISIBLE);
           score++;
        }
        else
        {
           gamelifes--;                     
        }

       buttonfinish.setOnClickListener(new View.OnClickListener() {

          public void onClick(View v){
             finish();          
          }
       });  
}

when the answer is right there will be show up some image and a button. and score++; if the answer is correct and gamelifes--; if wrong.

my next question is how to make score and gamelifes values send to level2.class whereas the conditions when buttonfinish Click will finish(); and return to Main.class and unlock the level 2 button in main.class

then how to make a sharedpreference setting to save the unlock button?

1 Answers1

2

First, to make a button not clickable you can use:

level2.setEnabled(false);

Then, when it can be clicked you would just set it to true.

Secondly, SharedPreferences can be used to store your values, like score, which can then be accessed at any point in the application.

To set up SharedPreferences:

SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
preferences.edit().putInt("score", 100).apply();

Then to get the score later:

preferences.getInt("score");

So, if you wanted to use a saved boolean to know if the second level is unlocked you could do something like this:

SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
boolean levelTwoUnlocked = preferences.getBoolean("level2", false);

level2.setEnabled(levelTwoUnlocked);

Or if you want to set a buttons visibility:

if (levelTwoUnlocked) {
    level2.setVisibility(View.VISIBLE);
} else {
    level2.setVisibility(View.INVISIBLE);
}
mattfred
  • 2,689
  • 1
  • 22
  • 38
  • If i re-open the game did the button still unlock or become lock again? There is no use preference for the unlock button to save? –  Aug 09 '15 at 20:06
  • Use a preference Boolean. That way it stays looked or unlocked based on the saved Boolean value – mattfred Aug 09 '15 at 20:07
  • Can you explain with some example code for that? I really dont know about how to use preference activity –  Aug 10 '15 at 02:34
  • @RoboticX - I have updated my answer with some additional code sample. – mattfred Aug 10 '15 at 14:09
  • Oh okay bro i got it! but i have one question again, can i change ("level2", false); to gone or visible? –  Aug 10 '15 at 15:40