Skip to content

Instantly share code, notes, and snippets.

@fryctze
Last active June 30, 2018 00:10
Show Gist options
  • Save fryctze/d3c3c1a673dc1023a66e7dd3683084e1 to your computer and use it in GitHub Desktop.
Save fryctze/d3c3c1a673dc1023a66e7dd3683084e1 to your computer and use it in GitHub Desktop.
Contoh Login Activity untuk Tutorial Simple Login menggunakan Shared Preferences di https://medium.com/@vickyfarenza/tutorial-penggunaan-shared-preferences-pada-android-eddc300d7509
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_centerInParent="true"
android:layout_marginTop="250dp"
android:layout_marginEnd="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="18dp"
android:orientation="vertical">
<EditText
android:id="@+id/et_emailSignin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"/>
<EditText
android:id="@+id/et_passwordSignin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:imeActionId="6"
android:imeActionLabel="Sign In"
android:imeOptions="actionUnspecified"
android:layout_marginBottom="30dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_signupSignin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Sign Up"
android:layout_weight="1"/>
<Button
android:id="@+id/button_signinSignin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Sign In"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
package farenza.tutorial.simplelogin;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
private EditText mViewUser, mViewPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
/* Menginisialisasi variable dengan Form User dan Form Password dari Layout LoginActivity */
mViewUser=findViewById(R.id.et_emailSignin);
mViewPassword =findViewById(R.id.et_passwordSignin);
/* Menjalankan Method razia() Jika tombol SignIn di keyboard di sentuh */
mViewPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_NULL) {
razia();
return true;
}
return false;
}
});
/* Menjalankan Method razia() jika merasakan tombol SignIn disentuh */
findViewById(R.id.button_signinSignin).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
razia();
}
});
/* Ke RegisterActivity jika merasakan tombol SignUp disentuh */
findViewById(R.id.button_signupSignin).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getBaseContext(),RegisterActivity.class));
}
});
}
/** ke MainActivity jika data Status Login dari Data Preferences bernilai true */
@Override
protected void onStart() {
super.onStart();
if (Preferences.getLoggedInStatus(getBaseContext())){
startActivity(new Intent(getBaseContext(),MainActivity.class));
finish();
}
}
/** Men-check inputan User dan Password dan Memberikan akses ke MainActivity */
private void razia(){
/* Mereset semua Error dan fokus menjadi default */
mViewUser.setError(null);
mViewPassword.setError(null);
View fokus = null;
boolean cancel = false;
/* Mengambil text dari form User dan form Password dengan variable baru bertipe String*/
String user = mViewUser.getText().toString();
String password = mViewPassword.getText().toString();
/* Jika form user kosong atau TIDAK memenuhi kriteria di Method cekUser() maka, Set error
* di Form User dengan menset variable fokus dan error di Viewnya juga cancel menjadi true*/
if (TextUtils.isEmpty(user)){
mViewUser.setError("This field is required");
fokus = mViewUser;
cancel = true;
}else if(!cekUser(user)){
mViewUser.setError("This Username is not found");
fokus = mViewUser;
cancel = true;
}
/* Sama syarat percabangannya dengan User seperti di atas. Bedanya ini untuk Form Password*/
if (TextUtils.isEmpty(password)){
mViewPassword.setError("This field is required");
fokus = mViewPassword;
cancel = true;
}else if (!cekPassword(password)){
mViewPassword.setError("This password is incorrect");
fokus = mViewPassword;
cancel = true;
}
/* Jika cancel true, variable fokus mendapatkan fokus */
if (cancel) fokus.requestFocus();
else masuk();
}
/** Menuju ke MainActivity dan Set User dan Status sedang login, di Preferences */
private void masuk(){
Preferences.setLoggedInUser(getBaseContext(),Preferences.getRegisteredUser(getBaseContext()));
Preferences.setLoggedInStatus(getBaseContext(),true);
startActivity(new Intent(getBaseContext(),MainActivity.class));finish();
}
/** True jika parameter password sama dengan data password yang terdaftar dari Preferences */
private boolean cekPassword(String password){
return password.equals(Preferences.getRegisteredPass(getBaseContext()));
}
/** True jika parameter user sama dengan data user yang terdaftar dari Preferences */
private boolean cekUser(String user){
return user.equals(Preferences.getRegisteredUser(getBaseContext()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment