Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Activity;
- import android.app.AlertDialog.Builder;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.app.AlertDialog;
- import android.widget.EditText;
- public class userPreferences extends Activity {
- EditText editText;
- Button save;
- public static final String NAME="UserName";
- SharedPreferences sharedPreferences;
- SharedPreferences.Editor editor;
- String n;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.userpreferences);
- editText=(EditText)findViewById(R.id.editText);
- save=(Button)findViewById(R.id.button);
- sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
- save.setOnClickListener(new View.OnClickListener() {
- final AlertDialog.Builder alertDialog = new AlertDialog.Builder(userPreferences.this);
- @Override
- public void onClick(View arg0) {
- alertDialog.setTitle("Alert Dialog");
- alertDialog.setMessage("Are you sure you want to save?");
- alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- n=editText.getText().toString();
- sharedPreferences=getPreferences(MODE_PRIVATE);
- editor=sharedPreferences.edit();
- editor.putString(NAME,n);
- editor.apply();
- startActivity(new Intent(userPreferences.this,Welcome.class));
- }
- });
- alertDialog.setNegativeButton("No", null);
- alertDialog.show();
- }
- });
- }
- }
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
- public class Welcome extends Activity {
- TextView textView;
- SharedPreferences sharedPreferences;
- public static final String NAME="UserName";
- SharedPreferences.Editor editor;
- String n;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.welcome);
- textView=(TextView)findViewById(R.id.textView);
- textView.setText("Welcome," + editor.putString(NAME, n));
- sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
- editor.apply();
- }
- @Override
- public void onBackPressed() {
- super.onBackPressed();
- startActivity(new Intent(Welcome.this, MainActivity.class));
- return;
- }
- Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference
- sharedPreferences=getPreferences(MODE_PRIVATE);
- sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
- if(!sharedPreferences.getString(NAME,"Default value").equals("Default value")){
- startActivity(new Intent(userPreferences.this, Welcome.class));
- finish();
- }
- sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
- textView=(TextView)findViewById(R.id.textView);
- textView.setText("Welcome," + sharedPreferences.getString(NAME,"Default value"));
- editor = = sharedPreferences.edit();
- editor.putString(NAME, sharedPreferences.getString(NAME,"Default value"));
- editor.apply();
- // open your preferences using the name you originally used to create it
- sharedPreferences = getSharedPreferences(NAME, MODE_PRIVATE);
- // open for editing
- editor = sharedPreferences.edit();
- // you probably don't want to use the same name here as preferences name
- editor.putString(NAME,n);
- editor.apply();
- sharedPreferences = getSharedPreferences(NAME, MODE_PRIVATE);
- String name = sharedPreferences.getString(NAME);
- textView.setText("Welcome," + name);
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.welcome);
- textView=(TextView)findViewById(R.id.textView);
- sharedPreferences = getSharedPreferences(NAME, MODE_PRIVATE);
- String name = sharedPreferences.getString(NAME);
- textView.setText("Welcome," + name);
- editor = sharedPreferences.edit();
- editor.putString(NAME, name);
- editor.apply();
- }
- TextView textView;
- SharedPreferences sharedPreferences;
- public static final String NAME = "UserName";
- public static final String defaultName = "";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.welcome);
- textView = (TextView) findViewById(R.id.textView);
- sharedPreferences = getSharedPreferences(NAME, Context.MODE_PRIVATE);
- textView.setText("Welcome," + sharedPreferences.getString(NAME, defaultName));
- }
- EditText editText;
- Button save;
- public static final String NAME="UserName";
- public static final String default = "";
- SharedPreferences sharedPreferences;
- SharedPreferences.Editor editor;
- String n;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.userpreferences);
- editText = (EditText) findViewById(R.id.editText);
- save = (Button) findViewById(R.id.button);
- sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
- if (sharedPreferences.getString(NAME, default).length() > 0) {
- // The user has already entered a username, so start the Welcome activity
- startActivity(new Intent(userPreferences.this, Welcome.class));
- finish();
- }
- // put the rest of your onCreate code here
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement