Save typed email and use it in a textview?

Asked

Viewed 52 times

0

I want to take the email that the person typed in the login to display it in a textview, how do I do that? This text is already being used to log in

  • Edittext, Sharedpreferences, is what after all?

  • changed the question

2 answers

2

I imagine you want to open another Activity and display in a Textview, the email typed on the login screen.

If it is only for display and use as proof of concept, you can do as follows:

Loginactivity.class

String txt = editText.getText().toString();
Intent intent = new Intent(LoginActivity.this, MainActivity.class); 
intent.putExtra("login", txt);     
startActivity(intent);           

Mainactivity.class

TextView txtEmail = (TextView) findViewById(R.id.tvEmail);
Bundle b = getIntent().getExtras();
if(b!=null){
    String email = b.getString("login");
    txtEmail.setText(email);
}

But I guide you to create a local base, to have no problem on display, if you continue with a navigation on other screens and have to return to Mainactivity, for example.

I hope I could have helped.

0

A simple form of data persistence is to use the SharedPreference as shown in the documentation on How to save key value sets. Below is an example of how to save, considering having a variable EditText by name myEditText:

SharedPreferences sharedpreferences = getSharedPreferences("pref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("str_email", myEditText.getText().toString());
editor.commit();

To rescue:

SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE);
String email = pref.getString("str_email", null);
myEditText.setText(email);

See more details on this question about Save value in Sharedpreference.

This way, at any time of its application, it is possible to return the value that was saved in a certain key. See here Data Persistence Levels in Android Applications other approaches.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.