Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.saigopinarimeti.helloworld;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- EditText editText;
- Button login;
- SharedPreferences sharedPreferences;
- SharedPreferences.Editor editor;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- editText = findViewById(R.id.username);
- login = findViewById(R.id.login);
- sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
- editor = sharedPreferences.edit();
- if (sharedPreferences.getBoolean("isLoggedIn",false)){
- startActivity(new Intent(this,HomeActivity.class));
- Toast.makeText(this, "already logged in", Toast.LENGTH_SHORT).show();
- }else{
- Toast.makeText(this, "please loggin", Toast.LENGTH_SHORT).show();
- }
- login.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- editor.putString("username",editText.getText().toString()).apply();
- editor.putBoolean("isLoggedIn",true).apply();
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment