Last active
June 30, 2018 00:13
-
-
Save fryctze/c26380abdfddda0cad9d67f16abefecf to your computer and use it in GitHub Desktop.
Contoh Register Activity untuk Tutorial Simple Login menggunakan Shared Preferences di https://medium.com/@vickyfarenza/tutorial-penggunaan-shared-preferences-pada-android-eddc300d7509
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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=".RegisterActivity"> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<LinearLayout | |
android:layout_centerInParent="true" | |
android:layout_marginTop="230dp" | |
android:layout_marginEnd="50dp" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:padding="18dp" | |
android:orientation="vertical"> | |
<EditText | |
android:id="@+id/et_emailSignup" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="@string/label_user" | |
android:inputType="textEmailAddress" | |
android:maxLines="1" | |
android:singleLine="true"/> | |
<EditText | |
android:id="@+id/et_passwordSignup" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Password" | |
android:inputType="textPassword" | |
android:maxLines="1" | |
android:singleLine="true" /> | |
<EditText | |
android:id="@+id/et_passwordSignup2" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="30dp" | |
android:hint="@string/label_confirm_password" | |
android:inputType="textPassword" | |
android:maxLines="1" | |
android:singleLine="true" | |
android:imeActionId="6" | |
android:imeActionLabel="@string/label_signup" | |
android:imeOptions="actionUnspecified" /> | |
<Button | |
android:id="@+id/button_signupSignup" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Sign Up" /> | |
</LinearLayout> | |
</RelativeLayout> | |
</ScrollView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package farenza.tutorial.simplelogin; | |
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 RegisterActivity extends AppCompatActivity { | |
private EditText mViewUser, mViewPassword, mViewRepassword; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_register); | |
/* Menginisialisasi variable dengan Form User, Form Password, dan Form Repassword | |
dari Layout RegisterActivity */ | |
mViewUser =findViewById(R.id.et_emailSignup); | |
mViewPassword =findViewById(R.id.et_passwordSignup); | |
mViewRepassword =findViewById(R.id.et_passwordSignup2); | |
/* Menjalankan Method razia() jika merasakan tombol SignUp di keyboard disentuh */ | |
mViewRepassword.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 SignUp disentuh */ | |
findViewById(R.id.button_signupSignup).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
razia(); | |
} | |
}); | |
} | |
/** 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); | |
mViewRepassword.setError(null); | |
View fokus = null; | |
boolean cancel = false; | |
/* Mengambil text dari Form User, Password, Repassword dengan variable baru bertipe String*/ | |
String repassword = mViewRepassword.getText().toString(); | |
String user = mViewUser.getText().toString(); | |
String password = mViewPassword.getText().toString(); | |
/* Jika form user kosong atau 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 already exist"); | |
fokus = mViewUser; | |
cancel = true; | |
} | |
/* Jika form password kosong dan MEMENUHI kriteria di Method cekPassword maka, | |
* Reaksinya sama dengan percabangan User di atas. Bedanya untuk Password dan Repassword*/ | |
if (TextUtils.isEmpty(password)){ | |
mViewPassword.setError("This field is required"); | |
fokus = mViewPassword; | |
cancel = true; | |
}else if (!cekPassword(password,repassword)){ | |
mViewRepassword.setError("This password is incorrect"); | |
fokus = mViewRepassword; | |
cancel = true; | |
} | |
/** Jika cancel true, variable fokus mendapatkan fokus. Jika false, maka | |
* Kembali ke LoginActivity dan Set User dan Password untuk data yang terdaftar */ | |
if (cancel){ | |
fokus.requestFocus(); | |
}else{ | |
Preferences.setRegisteredUser(getBaseContext(),user); | |
Preferences.setRegisteredPass(getBaseContext(),password); | |
finish(); | |
} | |
} | |
/** True jika parameter password sama dengan parameter repassword */ | |
private boolean cekPassword(String password, String repassword){ | |
return password.equals(repassword); | |
} | |
/** 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