package com.example.viraj.sharedpreferences;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button savebutton;
private EditText entertext;
private TextView resultdisplay;
private SharedPreferences myPref;
//information isme store kart he iss liy create kiya
private static final String PREF_NAME = "MY_PREF_FILE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
savebutton = (Button) findViewById(R.id.id_savedata);
entertext = (EditText) findViewById(R.id.id_entersomething);
resultdisplay = (TextView) findViewById(R.id.id_resulttextview);
savebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myPref = getSharedPreferences(PREF_NAME, 0);
SharedPreferences.Editor editor = myPref.edit();
//editor liya our save kiya
editor.putString("key_messgae_ki_he", entertext.getText().toString());
//save commit
editor.commit();
}
});
//get data back
SharedPreferences pref = getSharedPreferences(PREF_NAME, 0);//level o he
if (pref.contains("key_messgae_ki_he")) {
String message = pref.getString("key_messgae_ki_he", "not found");
resultdisplay.setText("MESSAGE" + message);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.viraj.sharedpreferences.MainActivity">
<EditText
android:id="@+id/id_entersomething"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="ENTER SOMETHING"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/id_savedata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="52dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id_entersomething" />
<TextView
android:id="@+id/id_resulttextview"
android:layout_width="252dp"
android:layout_height="28dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id_savedata" />
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button savebutton;
private EditText entertext;
private TextView resultdisplay;
private SharedPreferences myPref;
//information isme store kart he iss liy create kiya
private static final String PREF_NAME = "MY_PREF_FILE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
savebutton = (Button) findViewById(R.id.id_savedata);
entertext = (EditText) findViewById(R.id.id_entersomething);
resultdisplay = (TextView) findViewById(R.id.id_resulttextview);
savebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myPref = getSharedPreferences(PREF_NAME, 0);
SharedPreferences.Editor editor = myPref.edit();
//editor liya our save kiya
editor.putString("key_messgae_ki_he", entertext.getText().toString());
//save commit
editor.commit();
}
});
//get data back
SharedPreferences pref = getSharedPreferences(PREF_NAME, 0);//level o he
if (pref.contains("key_messgae_ki_he")) {
String message = pref.getString("key_messgae_ki_he", "not found");
resultdisplay.setText("MESSAGE" + message);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.viraj.sharedpreferences.MainActivity">
<EditText
android:id="@+id/id_entersomething"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="ENTER SOMETHING"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/id_savedata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="52dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id_entersomething" />
<TextView
android:id="@+id/id_resulttextview"
android:layout_width="252dp"
android:layout_height="28dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id_savedata" />
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
0 comments:
Post a Comment