Created
December 1, 2020 16:44
-
-
Save edward1986/4321f572faa7d38183fc67349f18f848 to your computer and use it in GitHub Desktop.
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"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<TextView android:id="@+id/textView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<EditText android:id="@+id/editTextName" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:ems="10" | |
android:hint="Enter your name" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent"/> | |
<Button android:id="@+id/button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Save" | |
app:layout_constraintTop_toBottomOf="@+id/editTextName" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
android:onClick="saveName"/> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
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 com.example.preference; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.content.SharedPreferences; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
private final String NAME="NAME"; | |
private EditText mEditTextName; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
TextView textView = (TextView)findViewById(R.id.textView); | |
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); | |
String name = sharedPreferences.getString(NAME,null); | |
if (name==null) { | |
textView.setText("Hello"); | |
} else { | |
textView.setText("Welcome back " + name + "!"); | |
} | |
mEditTextName = findViewById(R.id.editTextName); | |
} | |
public void saveName(View view) { | |
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); | |
editor.putString(NAME, mEditTextName.getText().toString()); | |
editor.apply(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment