Advertisement
aliwidimaulana

AuthActivity.java

May 15th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. package com.example.android.mybbideal;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.widget.Button;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. public class AuthActivity extends AppCompatActivity {
  15.  
  16.     SharedPreferences sharedPreferences;
  17.  
  18.     String userName;
  19.     String userPwd;
  20.  
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.  
  26.         sharedPreferences = getSharedPreferences(Constant.MyPREFERENCES,Context.MODE_PRIVATE);
  27.  
  28.         userName = sharedPreferences.getString(Constant.Name,"0");
  29.  
  30.         setContentView(R.layout.activity_auth);
  31.         Button buttonAuth = (Button) this.findViewById(R.id.button_auth);
  32.  
  33.         if(userName.equals("0")) {
  34.             buttonAuth.setText("Daftar");
  35.         } else {
  36.             buttonAuth.setText("Login");
  37.         }
  38.  
  39.         TextView txtLupaPwd = (TextView) this.findViewById(R.id.text_lupa_pwd_login);
  40.  
  41.         txtLupaPwd.setOnClickListener(new View.OnClickListener() {
  42.             public void onClick(View v) {
  43.                 AuthActivity.this.finish();
  44.                 Intent intent = new Intent(AuthActivity.this, ForgotPwdActivity.class);
  45.                 AuthActivity.this.startActivity(intent);
  46.             }
  47.         });
  48.  
  49.         EditText editUsername = (EditText) findViewById(R.id.username);
  50.         EditText editPassword = (EditText) findViewById(R.id.password);
  51.  
  52.         if (savedInstanceState == null) {
  53.             Bundle extras = getIntent().getExtras();
  54.             if(extras == null) {
  55.                 // do nothing
  56.             } else {
  57.                 editUsername.setText( extras.getString("extraUser"));
  58.                 editPassword.setText( extras.getString("extraPwd") );
  59.             }
  60.         }
  61.     }
  62.  
  63.     public void prosesAuth(View view) {
  64.         EditText editUsername = (EditText) findViewById(R.id.username);
  65.         EditText editPassword = (EditText) findViewById(R.id.password);
  66.  
  67.         String u = editUsername.getText().toString();
  68.         String p = editPassword.getText().toString();
  69.  
  70.         Button btnBtn = (Button) this.findViewById(R.id.button_auth);
  71.         if( btnBtn.getText().toString().equals("Daftar") ) {
  72.             SharedPreferences.Editor editor = sharedPreferences.edit();
  73.  
  74.             editor.putString(Constant.Name, u);
  75.             editor.putString(Constant.Pwd, p);
  76.             editor.apply();
  77.  
  78.             goToActivity(MainActivity.class);
  79.         } else  {
  80.             userName = sharedPreferences.getString(Constant.Name,"0");
  81.             userPwd = sharedPreferences.getString(Constant.Pwd,"0");
  82.  
  83.             if( u.equals(userName) && p.equals(userPwd) ) {
  84.                 goToActivity(MainActivity.class);
  85.             } else {
  86.                 Toast.makeText(this, "Invalid username/password!", Toast.LENGTH_SHORT).show();
  87.             }
  88.         }
  89.     }
  90.     public void goToActivity(Class act) {
  91.         this.finish();
  92.         Intent intent = new Intent(this,act);
  93.         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  94.         this.startActivity(intent);
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement