Skip to content

Instantly share code, notes, and snippets.

@fryctze
Last active June 30, 2018 00:11
Show Gist options
  • Save fryctze/f68de953d29d57db225ca15f4afbd9e8 to your computer and use it in GitHub Desktop.
Save fryctze/f68de953d29d57db225ca15f4afbd9e8 to your computer and use it in GitHub Desktop.
Contoh MainActivity 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"?>
<RelativeLayout 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"
android:padding="30dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Masuk sebagai"/>
<TextView
android:id="@+id/tv_namaMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
tools:text="Namaku adalah"/>
</LinearLayout>
<Button
android:id="@+id/button_logoutMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dp"
android:text="Log Out" />
</RelativeLayout>
package farenza.tutorial.simplelogin;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Deklarasi dan Menginisialisasi variable nama dengan Label Nama dari Layout MainActivity */
TextView nama = findViewById(R.id.tv_namaMain);
/* Men-set Label Nama dengan data User sedang login dari Preferences */
nama.setText(Preferences.getLoggedInUser(getBaseContext()));
/* Men-set Status dan User yang sedang login menjadi default atau kosong di
* Data Preferences. Kemudian menuju ke LoginActivity*/
findViewById(R.id.button_logoutMain).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Menghapus Status login dan kembali ke Login Activity
Preferences.clearLoggedInUser(getBaseContext());
startActivity(new Intent(getBaseContext(),LoginActivity.class));
finish();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment