-1

I want set a password.
Firstly i save a data in sharedprefarances, Then i want access the saved data from mainActivity, where i set login system.

    package com.example.akibmahmud.pasword;

    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;


    public class SetPassword extends ActionBarActivity {
        final static String SHARED_NAME="akib";
        final static String PASSWORD_NAME="password";
        EditText cPass,newPass;
        Button setButton;
        SharedPreferences sharedPreferences;

        @Override
        protected void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_set_password);
            cPass= (EditText) findViewById(R.id.password1);
            newPass= (EditText) findViewById(R.id.password2);
            setButton= (Button) findViewById(R.id.setbutton);



          sharedPreferences=getSharedPreferences(SHARED_NAME,MODE_PRIVATE);
            final String `passwod_name=sharedPreferences.getString(PASSWORD_NAME,"1234");`

            setButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String oPass=cPass.getText().toString();
                    String nPass=newPass.getText().toString();
                    if (passwod_name.equals(oPass)){
                        SharedPreferences.Editor editor=sharedPreferences.edit();
                        editor.putString(PASSWORD_NAME,nPass);
                        editor.commit();

                    }
                    else{
                        Toast.makeText(getApplicationContext(),"Please Enter Your Right Current Password",Toast.LENGTH_LONG).show();
                    }
                }
            });
        }



    }

I need that, i want access (passwod_name),from Main_Activity class

    package com.example.akibmahmud.pasword;

    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    import java.util.Set;

    import static android.content.SharedPreferences.*;


    public class MainActivity extends ActionBarActivity {
        EditText pass,pass1;
        Button btn,btn1;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            pass= (EditText) findViewById(R.id.pasword);
            btn= (Button) findViewById(R.id.nextSet);
            btn1= (Button) findViewById(R.id.logIn);
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String input = pass1.getText().toString();
                    if (?????.equals(input)) {
                        Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                        startActivity(intent);

                    } else {
                        Toast.makeText(getApplicationContext(), "Password Wrong,Please Enter Right Password", Toast.LENGTH_LONG).show();
                    }
                }
            });
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(MainActivity.this,SetPassword.class);
                    startActivity(intent);
                }
            });

        }        
    }
Harsh Dattani
  • 2,109
  • 1
  • 17
  • 27
  • I can't tell what your question is. – nasch Apr 28 '15 at 19:12
  • possible duplicate of [How to use SharedPreferences in Android to store, fetch and edit values](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – Rod Apr 28 '15 at 19:16

3 Answers3

0

A simple google query (sharedpreferences all activities) return this:

Storing data in SharedPreferences accessible to all activities

There i found your solution i think. But i don't want to copy so read it. if still no answer there tell us again.

Community
  • 1
  • 1
0

If you use the same name as the first argument when accessing the SharedPreferences this should work inside your MainActivity:

final static String SHARED_NAME="akib";
final static String PASSWORD_NAME="password";    
SharedPreferences sharedPreferences;
sharedPreferences=getSharedPreferences(SHARED_NAME,MODE_PRIVATE);
String password  = sharedPreferences.getString(PASSWORD_NAME, "");
Orr
  • 4,740
  • 3
  • 28
  • 31
0

In your SetPassword class use SharedPreference like this way:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(PASSWORD_NAME,nPass);
        editor.commit();

And in you MainActivity to access data use SharedPreference like this:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String password_data = sharedPreferences.getString("password", "");
Harsh Dattani
  • 2,109
  • 1
  • 17
  • 27