You can use intents to transfer the data from one activity to another using. Below is an example to implement a signup form in Android
MainActivity.java
package com.AndroidDevelopmentGuru.registeration_form;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText text1,text2,text3,text4,text5;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.Register);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text1 = (EditText)findViewById(R.id.username);
text2 = (EditText)findViewById(R.id.email);
text3= (EditText)findViewById(R.id.contact);
text4=(EditText)findViewById(R.id.password);
text5=(EditText)findViewById(R.id.RePass);
String user = text1.getText().toString();
String email = text2.getText().toString();
String contact = text3.getText().toString();
String password = text4.getText().toString();
String repass = text5.getText().toString();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
//empty email
if (TextUtils.isEmpty(email)) {
// boolean invalid = true;
Toast.makeText(MainActivity.this,getResources().getString(R.string.title_empty_email),
Toast.LENGTH_SHORT).show();
}else if(!email.matches(emailPattern))
{
Toast.makeText(MainActivity.this,getResources().getString(R.string.title_email_not_valid),
Toast.LENGTH_SHORT).show();
}
else if(!password.equals(repass)){
Toast.makeText(MainActivity.this,getResources().getString(R.string.title_notmatch_conform_password) ,
Toast.LENGTH_SHORT).show();
}else{
//sending data to next activity thru Intent
Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
intent.putExtra("User", user);
startActivity(intent);
Intent intent1 = new Intent(MainActivity.this,RegisterActivity.class);
intent.putExtra("email", email);
startActivity(intent);
Intent intent2 = new Intent(MainActivity.this,RegisterActivity.class);
intent.putExtra("contact", contact);
startActivity(intent);
}
}
});
}
}
RegisterActivity.java
package com.AndroidDevelopmentGuru.registeration_form;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class RegisterActivity extends Activity {
TextView tv1,tv2,tv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
tv1 =(TextView)findViewById(R.id.user);
tv2=(TextView)findViewById(R.id.remail);
tv3 =(TextView)findViewById(R.id.rcontact);
//Retrieving data from previous activity
String user = getIntent().getStringExtra("User");
tv1.setText(user);
String email = getIntent().getStringExtra("email");
tv2.setText(email);
String contact = getIntent().getStringExtra("contact");
tv3.setText(contact);
Toast.makeText(getApplicationContext(), user+" Registered Successfully ", Toast.LENGTH_LONG).show();
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="UserName"
android:inputType="textPersonName" >
<requestFocus
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</EditText>
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Contact"
android:inputType="phone" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/RePass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPassword" />
<Button
android:id="@+id/Register"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Register" />
</LinearLayout>
activity_register.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".RegisterActivity" >
<TextView
android:id="@+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="TextView" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/line" />
<TextView
android:id="@+id/remail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="TextView" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/line" />
<TextView
android:id="@+id/rcontact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="TextView" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/line" />
</LinearLayout>
Download the source code
Output:
MainActivity.java
package com.AndroidDevelopmentGuru.registeration_form;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText text1,text2,text3,text4,text5;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.Register);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text1 = (EditText)findViewById(R.id.username);
text2 = (EditText)findViewById(R.id.email);
text3= (EditText)findViewById(R.id.contact);
text4=(EditText)findViewById(R.id.password);
text5=(EditText)findViewById(R.id.RePass);
String user = text1.getText().toString();
String email = text2.getText().toString();
String contact = text3.getText().toString();
String password = text4.getText().toString();
String repass = text5.getText().toString();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
//empty email
if (TextUtils.isEmpty(email)) {
// boolean invalid = true;
Toast.makeText(MainActivity.this,getResources().getString(R.string.title_empty_email),
Toast.LENGTH_SHORT).show();
}else if(!email.matches(emailPattern))
{
Toast.makeText(MainActivity.this,getResources().getString(R.string.title_email_not_valid),
Toast.LENGTH_SHORT).show();
}
else if(!password.equals(repass)){
Toast.makeText(MainActivity.this,getResources().getString(R.string.title_notmatch_conform_password) ,
Toast.LENGTH_SHORT).show();
}else{
//sending data to next activity thru Intent
Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
intent.putExtra("User", user);
startActivity(intent);
Intent intent1 = new Intent(MainActivity.this,RegisterActivity.class);
intent.putExtra("email", email);
startActivity(intent);
Intent intent2 = new Intent(MainActivity.this,RegisterActivity.class);
intent.putExtra("contact", contact);
startActivity(intent);
}
}
});
}
}
RegisterActivity.java
package com.AndroidDevelopmentGuru.registeration_form;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class RegisterActivity extends Activity {
TextView tv1,tv2,tv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
tv1 =(TextView)findViewById(R.id.user);
tv2=(TextView)findViewById(R.id.remail);
tv3 =(TextView)findViewById(R.id.rcontact);
//Retrieving data from previous activity
String user = getIntent().getStringExtra("User");
tv1.setText(user);
String email = getIntent().getStringExtra("email");
tv2.setText(email);
String contact = getIntent().getStringExtra("contact");
tv3.setText(contact);
Toast.makeText(getApplicationContext(), user+" Registered Successfully ", Toast.LENGTH_LONG).show();
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="UserName"
android:inputType="textPersonName" >
<requestFocus
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</EditText>
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Contact"
android:inputType="phone" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/RePass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPassword" />
<Button
android:id="@+id/Register"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Register" />
</LinearLayout>
activity_register.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".RegisterActivity" >
<TextView
android:id="@+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="TextView" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/line" />
<TextView
android:id="@+id/remail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="TextView" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/line" />
<TextView
android:id="@+id/rcontact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="TextView" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/line" />
</LinearLayout>
Download the source code
Output:
Comments
Post a Comment