Activity1:
IntentEgActivity
IntentEgActivity.java
package intent.eg;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonOne = (Button) findViewById(R.id.button1);
buttonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText usernam = (EditText)findViewById(R.id.editText1);
String name = usernam.getText().toString();
EditText upas = (EditText)findViewById(R.id.editText2);
String pass = upas.getText().toString();
if(name.equals("xyz") && pass.equals("abc"))
{
Intent i = new Intent(IntentEgActivity.this,InvokedActivity.class);
i.putExtra("value1", name);
i.putExtra("value2", pass);
startActivity(i);
}
else
{ Toast.makeText(getApplicationContext(), "wrong user name or password", Toast.LENGTH_SHORT).show();
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter User Name" />
<EditText
android:id="@+id/editText1"
android:layout_width="232dp"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="152dp"
android:layout_height="wrap_content"
android:text="Enter Password" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Next Activity" />
</LinearLayout>
import android.os.Bundle;
import android.widget.TextView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.disp);
Bundle Name = getIntent().getExtras();
String userName = Name.getString("value1");
if(userName != null)
{
TextView txt = (TextView)findViewById(R.id.textView5);
txt.setText("Welcome"+" "+userName);
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView5"
android:layout_width="167dp"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
IntentEgActivity.java
package intent.eg;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class IntentEgActivity extends Activity {
/**
Called when the activity is first created. */@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonOne = (Button) findViewById(R.id.button1);
buttonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText usernam = (EditText)findViewById(R.id.editText1);
String name = usernam.getText().toString();
EditText upas = (EditText)findViewById(R.id.editText2);
String pass = upas.getText().toString();
if(name.equals("xyz") && pass.equals("abc"))
{
Intent i = new Intent(IntentEgActivity.this,InvokedActivity.class);
i.putExtra("value1", name);
i.putExtra("value2", pass);
startActivity(i);
}
else
{ Toast.makeText(getApplicationContext(), "wrong user name or password", Toast.LENGTH_SHORT).show();
}
} });
}
}
Main.xml
<?xml version="1.0"
encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter User Name" />
<EditText
android:id="@+id/editText1"
android:layout_width="232dp"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="152dp"
android:layout_height="wrap_content"
android:text="Enter Password" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Next Activity" />
</LinearLayout>
Activity2: InvokedActivity
InvokedActivity.java
package intent.eg;
import
android.app.Activity;import android.os.Bundle;
import android.widget.TextView;
public class InvokedActivity extends Activity
{ @Overridepublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.disp);
Bundle Name = getIntent().getExtras();
String userName = Name.getString("value1");
if(userName != null)
{
TextView txt = (TextView)findViewById(R.id.textView5);
txt.setText("Welcome"+" "+userName);
}
}
}
disp.xml
<?xml version="1.0"
encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView5"
android:layout_width="167dp"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>