B
B
bychok3002017-02-21 15:32:08
Android
bychok300, 2017-02-21 15:32:08

How to save username and password in android application?

Hi
I have a simple activity that requires a username and password. The question is, how can I save this username and password and the next time I open the application, ask it again and check? And with the correct input, translate to a new page?
I read about SharedPreferences, but somehow I didn’t really understand, please explain.
Here is the activation itself

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
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 MainActivity extends Activity {

    EditText validateEmail,pass;
    Button login;
    String email,email_pattern,password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        validateEmail = (EditText) findViewById(R.id.email);
        pass = (EditText) findViewById(R.id.pass);
        login = (Button) findViewById(R.id.button);

        email_pattern = "[a-zA-Z0-9._-][email protected][a-z]+\\.+[a-z]+";

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                email = validateEmail.getText().toString().trim();
                password = pass.getText().toString().trim();

                if (TextUtils.isEmpty(email)) {
                    validateEmail.setError("emailcannot be empty");
                    validateEmail.focusSearch(View.FOCUS_DOWN);

                } else if (email.matches(email_pattern)) {

                    if (TextUtils.isEmpty(password)) {

                        pass.setError("password mustnot be empty");
                        pass.focusSearch(View.FOCUS_DOWN);

                    } else if (password.length() >= 5 && password.length() <= 10) {

                        Toast.makeText(getApplicationContext(), "validation sucess", Toast.LENGTH_LONG).show();

                    } else {
                        pass.setError("password length must be match:5=<password>=10");
                        pass.focusSearch(View.FOCUS_DOWN);
                    }
                } else {

                    validateEmail.setError("email must be in format:[email protected]");
                    validateEmail.focusSearch(View.FOCUS_DOWN);

                }


            }
        });


    }


    protected void ClickMe(View v){
        Intent intent = new Intent(MainActivity.this, new_activity.class);
        startActivity(intent);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aol-nnov, 2017-02-21
@aol-nnov

I have a simple activity that requires a username and password.

you don't need activation with username and password :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question