[Solved] I need to create a Android activity that’s protected with a pin code [closed]


When Ok is pressed after the pin is entered you need to verify if the entered pin is the same as the saved pin, if yes you can open the activity you want to.
You need to have an edittext to collect the pin.

<EditText
 android:id="@+id/passwordedittext"
 android:layout_width="200dp"
 android:layout_height="wrap_content"
 android:inputType="textPassword">
 <requestFocus />

An ok button is required so that when clicked verify if the pin is correct then open the activity else show an error message.

<Button
 android:id="@+id/okbutton"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_marginTop="50dp"
 android:clickable="true" 
 android:layout_gravity="center_horizontal" 
 android:layout_marginRight="20dp"/>

Code for opening your activity:

Button okButton = (Button) findViewById(R.id.okbutton);
    okButton.setOnClickListener(new View.OnClickListener() {        
        public void onClick(View v){
        EditText passwordEditText = (EditText) findViewById(R.id.pinedittext);
                    if(passwordEditText.getText().toString().equals("theuserpin")){
                        startActivity(new Intent("com.my.activtyToBeOpened"));
                    }
                    else{
                       //add some code to display error message
                    }
}});

Now if incase if the user needs to set the pin the first time and later the same pin is user to validate the login then you need to get the pin and save it for later use when user enters it. You need to have an edittext and a button, when button is clicked you need to save it using preferences.

SharedPreferences.Editor editor = getSharedPreferences("my_pin_pref", MODE_PRIVATE).edit();
 editor.putString("pin", edittextping.getText().toString());
 editor.commit();

You can retrieve this saved value using:

SharedPreferences prefs = getSharedPreferences("my_pin_pref", MODE_PRIVATE); 
String mysavedpin = prefs.getString("pin", "");

Combining this with the code we had to verify the entered pin with the saved pin the new code would be:

SharedPreferences prefs = getSharedPreferences("my_pin_pref", MODE_PRIVATE); 
String mysavedpin = prefs.getString("pin", "");

Button okButton = (Button) findViewById(R.id.okbutton);
    okButton.setOnClickListener(new View.OnClickListener() {        
        public void onClick(View v){
        EditText passwordEditText = (EditText) findViewById(R.id.pinedittext);
                    if(passwordEditText.getText().toString().equals(mysavedpin)){
                        startActivity(new Intent("com.my.activtyToBeOpened"));
                    }
                    else{
                       //add some code to display error message
                    }
}});

1

solved I need to create a Android activity that’s protected with a pin code [closed]