Question

I have designed 3 pages for user registration. I want to keep the values to be stored until the user press the submit button in 3rd page. I have next and previous button in each page.When I click the previous button in 2nd page all the values entered by user in 1st page will be loaded and when he clicks the next button the details given in second page will be loaded. How can I achieve this? Can anyone help?

This is the code i tried,

    private EditText username;
    private EditText email;
    private EditText mobile;
    private EditText firstname;
    public static ArrayList<String> userInfo = new ArrayList<String>();
    String userinfo[];
    private Button next1;
    final Context context=this;
    public static final String MyPREFERENCES = "MyPrefs" ;
    public static final String Name = "nameKey"; 
    public static final String Phone = "phoneKey"; 
    public static final String Email = "emailKey"; 
    public static final String user = "userKey"; 
    SharedPreferences sharedpreferences;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    ActionBar actbar=getActionBar();
    actbar.show();

    getActionBar().setDisplayHomeAsUpEnabled(true);
    Button clr=(Button)findViewById(R.id.cler);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

     if (sharedpreferences.contains(Name))
     {
     firstname.setText(sharedpreferences.getString(Name, ""));

     }
     if (sharedpreferences.contains(Phone))
     {
     mobile.setText(sharedpreferences.getString(Phone, ""));

     }
      if (sharedpreferences.contains(Email))
     {
     email.setText(sharedpreferences.getString(Email, ""));

      }
      if (sharedpreferences.contains(user))
     {
     username.setText(sharedpreferences.getString(user, ""));

     }
     clr.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
     firstname = (EditText) findViewById(R.id.firstnam);
     username  = (EditText) findViewById(R.id.frstname);
     email = (EditText) findViewById(R.id.mail);
     mobile = (EditText) findViewById(R.id.mobile);
    username.setText("");
   email.setText("");
   mobile.setText("");
   firstname.setText("");

   }

     });
    registerValid();
  }
   public void run(View view){
    String n  = firstname.getText().toString();
    String ph  = mobile.getText().toString();
    String e  = email.getText().toString();
    String u  = username.getText().toString();

    Editor editor = sharedpreferences.edit();
    editor.putString(Name, n);
    editor.putString(Phone, ph);
    editor.putString(Email, e);
    editor.putString(user, u);


    editor.commit(); 

 }

Layout file,

    <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/backgrou"
    android:orientation="vertical" >

<Button
    android:id="@+id/next"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="70sp"
    android:layout_height="40sp"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/firstnam"
    android:layout_marginBottom="64dp"
    android:background="@drawable/buttonback"
    android:text="@string/next2"
    android:textColor="#ffffff" />

<EditText
    android:id="@+id/frstname"
    android:layout_width="180sp"
    android:layout_height="40sp"
    android:layout_above="@+id/mobile"
    android:layout_alignLeft="@+id/mobile"
    android:layout_marginBottom="26dp"
    android:background="@drawable/edittext"
    android:ems="10"
    android:digits="abcdefghijklmnopqrstuvwxyz0123456789.-@_"
    android:inputType="textPersonName" />

    <TextView
    android:id="@+id/firstname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/frstname"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="40dp"
    android:layout_marginLeft="20dp"
    android:text="@string/firname"
    android:textColor="#ffffff" />

<TextView
    android:id="@+id/mob"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/next"
    android:layout_alignRight="@+id/fname"
    android:layout_marginBottom="144dp"
    android:text="@string/mobile"
    android:textColor="#ffffff" />

   <EditText
    android:id="@+id/mobile"
    android:layout_width="180sp"
    android:layout_height="40sp"
    android:layout_alignLeft="@+id/mail"
    android:layout_alignTop="@+id/mob"
    android:background="@drawable/edittext"
    android:ems="10"
    android:maxLength="10"
    android:inputType="phone" />

    <Button
    android:id="@+id/cler"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="70sp"
    android:layout_height="40sp"
    android:layout_alignBaseline="@+id/next"
    android:layout_alignBottom="@+id/next"
    android:layout_alignLeft="@+id/mob"
    android:background="@drawable/buttonback"
    android:text="@string/reset"
    android:textColor="#ffffff" />

<EditText
    android:id="@+id/firstnam"
    android:layout_width="180sp"
    android:layout_height="40sp"
    android:layout_alignLeft="@+id/frstname"
    android:layout_alignTop="@+id/firstname"
    android:background="@drawable/edittext"
    android:ems="10"
    android:inputType="textPersonName" >  <requestFocus /></EditText>

<TextView
    android:id="@+id/fname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/firstname"
    android:layout_below="@+id/firstname"
    android:text="@string/fname"
    android:textColor="#ffffff" />

<TextView
    android:id="@+id/email"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/mob"
    android:layout_below="@+id/mobile"
    android:layout_marginTop="20dp"
    android:text="@string/mail"
    android:textColor="#ffffff" />

   <EditText
    android:id="@+id/mail"
    android:layout_width="180sp"
    android:layout_height="40sp"
    android:layout_alignTop="@+id/email"
    android:layout_toRightOf="@+id/cler"
    android:background="@drawable/edittext"
    android:ems="10"
    android:inputType="textEmailAddress" />

   </RelativeLayout>
Was it helpful?

Solution

Okay, use below code.

XML

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/scrollView1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      tools:context=".DisplayContact" >

<RelativeLayout
   android:layout_width="match_parent"
   android:layout_height="370dp"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   >

   <EditText
      android:id="@+id/editTextName"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="82dp"
      android:ems="10"
      android:inputType="text" >

   </EditText>

   <EditText
      android:id="@+id/editTextEmail"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editTextStreet"
      android:layout_below="@+id/editTextStreet"
      android:layout_marginTop="22dp"
      android:ems="10"
      android:inputType="textEmailAddress" />

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/editTextName"
      android:layout_alignParentLeft="true"
      android:text="@string/name"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editTextCity"
      android:layout_alignParentBottom="true"
      android:layout_marginBottom="28dp"
      android:onClick="run"
      android:text="@string/save" />

   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/editTextEmail"
      android:layout_alignLeft="@+id/textView1"
      android:text="@string/email"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <TextView
      android:id="@+id/textView5" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/editTextPhone"
      android:layout_alignLeft="@+id/textView1"
      android:text="@string/phone"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <TextView
      android:id="@+id/textView4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@+id/editTextEmail"
      android:layout_alignLeft="@+id/textView5"
      android:text="@string/street"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <EditText
      android:id="@+id/editTextCity"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@+id/editTextName"
      android:layout_below="@+id/editTextEmail"
      android:layout_marginTop="30dp"
      android:ems="10"
      android:inputType="text" />

   <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBaseline="@+id/editTextCity"
      android:layout_alignBottom="@+id/editTextCity"
      android:layout_alignParentLeft="true"
      android:layout_toLeftOf="@+id/editTextEmail"
      android:text="@string/country"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <EditText
      android:id="@+id/editTextStreet"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editTextName"
      android:layout_below="@+id/editTextPhone"
      android:ems="10"
      android:inputType="text" >

      <requestFocus />
   </EditText>

   <EditText
      android:id="@+id/editTextPhone"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editTextStreet"
      android:layout_below="@+id/editTextName"
      android:ems="10"
      android:inputType="phone|text" />

   </RelativeLayout>

   </ScrollView>
public class MainActivity extends Activity {

   TextView name ;
   TextView phone;
   TextView email;
   TextView street;
   TextView place;
   public static final String MyPREFERENCES = "MyPrefs" ;
   public static final String Name = "nameKey"; 
   public static final String Phone = "phoneKey"; 
   public static final String Email = "emailKey"; 
   public static final String Street = "streetKey"; 
   public static final String Place = "placeKey"; 

   SharedPreferences sharedpreferences;

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

      name = (TextView) findViewById(R.id.editTextName);
      phone = (TextView) findViewById(R.id.editTextPhone);
      email = (TextView) findViewById(R.id.editTextStreet);
      street = (TextView) findViewById(R.id.editTextEmail);
      place = (TextView) findViewById(R.id.editTextCity);

next1 = (Button) findViewById(R.id.next); 
next1.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View view) { 
String n = firstname.getText().toString(); 
String ph = mobile.getText().toString(); 
String e = email.getText().toString(); 
String u = username.getText().toString(); 

Editor editor = sharedpreferences.edit(); 
editor.putString(Name, n); 
editor.putString(Phone, ph); 
editor.putString(Email, e); 
editor.putString(user, u); 


editor.commit(); 

startActivity(new Intent(SignupActivity.this,NextActivity.class)); 

} 
});

      sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

      }



 @Override 
protected void onResume() { 
super.onResume(); 
if (sharedpreferences.contains(Name)) 
{ 
firstname.setText(sharedpreferences.getString(Name, "")); 

} 
if (sharedpreferences.contains(Phone)) 
{ 
mobile.setText(sharedpreferences.getString(Phone, "")); 

} 
if (sharedpreferences.contains(Email)) 
{ 
email.setText(sharedpreferences.getString(Email, "")); 

} 
if (sharedpreferences.contains(user)) 
{ 
username.setText(sharedpreferences.getString(user, "")); 

} 
} 

   @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);
      return true;
   }

}

Now on next button's click event go-to next activity and on previous button's of next activity when you come to this activity this data will be as it is in edit-text.

OTHER TIPS

Pass values through bundle and don't finish activities. In this way when u come back using backpressed values are in place. and if u want soft back button in your activity then use super.onBackPressed in onClick of it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top