title: set and store password in android apps
body: <p>I have try to set and store the password for my apps, but it is not working at all. The password should be setted first time then return the home page, then when the user open it again the password should be stored but somehow it didn't store it. </p>
  
  <pre><code>PreferenceManager.getDefaultSharedPreferences(this);
      SharedPreferences passfile = getSharedPreferences("ans",0);
      String pass = passfile.getString("ans", null);
  
      check.setOnClickListener(new OnClickListener () {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
  
              String answer1 = answer.getText().toString();
  
  
              //Check that user typed in an answer
              if(answer1.length()&lt;8){
                  Toast.makeText(CheckPwActivity.this, "Answer must be 8 characters long", Toast.LENGTH_SHORT).show();
                  answer.setText("");
                  answer.requestFocus();
                  return;
              }
              answer.getEditableText().toString();
              //check if the answer is valid
              if (answer1.equals("ans")) {
                  Intent intent2 = new Intent(CheckPwActivity.this,MainActivity.class);
                  startActivity(intent2);
  
  
          }else{ 
              return;
  
          }
  
  
      }});
  
  
  
      }
  public void setPassword(String key, String value) {
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
      SharedPreferences.Editor editor = preferences.edit();
      Editor preferenceEditor = context.getSharedPreferences("password", 8).edit();
      preferenceEditor.putString(key, value);
      preferenceEditor.commit();
  }
  
  public static String getPassword(String filename) {
      return context.getSharedPreferences("password", 2).getString(filename,"");
  }
  </code></pre>