Technical Knowledge

technical knowledge is that type of blogger in which you can find all the soluctions of your problems which you cannot solve you can see the video given on this blog and solve your problem by yourself.

Like us on Facebook

Technical Knowledge
Showing posts with label activity. Show all posts
Showing posts with label activity. Show all posts

Sunday, March 29, 2015

Starting second activity based on login and password details: Android tutorial

Program description:

Starting second activity based on the login and password credentials given in first activity.

First activity will have 2 edit texts and 1 button. User will be prompted to enter user name in edit text1, and enter password in edit text2.

if user id is "techpalle" and password is"1234", then it will start second activity and pass the uid entered by user to second screen using explicit intent.

If user enters user id, and password wrong, then a toast message will be displayed, content of edit text 1 and edit text 2 will be cleared and automatically control will be moved to edit text1 to make user comfortable.


Once second activity is started, it takes the data passed from first activity and displays in a text view. This data passing between activities will be done by using putExtra() method.


Note: If user fails to enter user name or password correctly for 3 times, then main activity will be closed by using finish() method.


First Activity                                       First Activity (when pw is wrong)

 

First Activity (when pw is correct)          Second Activity

 


First Activity 
package com.techpalle.b15_assignment;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
EditText e,f;
Button b;
TextView tv;
Integer cnt = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e = (EditText) findViewById(R.id.editText1);
f = (EditText) findViewById(R.id.editText2);
b = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.textView1);
tv.setText(tv.getText().toString()+ cnt.toString());

b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String uid = e.getText().toString();
String pw = f.getText().toString();
boolean bu = uid.equals("techpalle");
boolean bp = pw.equals("1234");

if(bu && bp){
Intent in = new Intent(getApplicationContext(),
SecondScreen.class);
in.putExtra("uid", uid);
cnt = 0;
startActivity(in);
}else if(bu == false && bp == false){
Toast.makeText(getApplicationContext(),
"uid & pw wrong..try again", 0).show();
cnt++;
tv.setText("No of tries: "+cnt);
e.setText("");
f.setText("");
e.requestFocus();
}else if(bu == false){
Toast.makeText(getApplicationContext(),
"uid wrong..try again", 0).show();
cnt++;
e.setText("");
e.requestFocus();
tv.setText("No of tries: "+cnt);
}else{
Toast.makeText(getApplicationContext(),
"pw wrong..try again", 0).show();
cnt++;
f.setText("");
f.requestFocus();
tv.setText("No of tries: "+cnt);
}

if(cnt == 3){
Toast.makeText(getApplicationContext(),
"Tries over..", 0).show();
finish();
}
}
});
}

}


xml layout file for First Activity 
File name: activity_main.xml
  xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
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" >
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="22dp"
android:ems="10" >


android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="14dp"
android:layout_marginTop="16dp"
android:ems="10" />



Second screen
package com.techpalle.b15_assignment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondScreen extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
Bundle b = in.getExtras();
String uid = b.getString("uid");
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("Welcome.."+uid);
}
}
xml layout file for Second Activity 
File name: second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />



Download complete code : Click to download

Tags: Login page android, start activity on login, pass user id and password , android tutorial

How to start an activity and pass data to second screen: Android tutorial


This post will try to cover how to start a second activity from main activity on button click and pass data.
I will assume that you have 2 activities and 2 xml files (one for each activity).
First activity has one button, on clicking which we will start the second activity by using explicit intent. While starting the second activity we will use putExtra() method to pass the data to second activity.
Once second activity is started, the data passed from first activity will be displayed in second activity as toast messages.
Second activity has a button, on clicking which we will close second activity by using finish() method.

First screen                                                                  Second screen






First Activity

package com.techpalle.b15_startactivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(),
SecondaryScreen.class);
in.putExtra("uid", "satish");
startActivity(in);
}
});
}

@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;
}

}

xml layout file for First Activity 
File name: activity_main.xml
  xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


Second Activity
package com.techpalle.b15_startactivity;

import java.io.Closeable;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SecondaryScreen extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
String action = in.getAction();
Bundle bnd = in.getExtras();
String uid = bnd.getString("uid");
Toast.makeText(this, "uid is.."+uid+"..action.."+action, 0).show();
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
// TODO Auto-generated method stub
}

}

xml layout file for Second Activity 
File name: second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome second.."
android:textAppearance="?android:attr/textAppearanceLarge" />

Tags : android, startactivity, putextras, pass data between activities, start second activity, android tutorial

Download complete code : Click to download

Android interview questions on this topic:
android activity interview questions and answers

Similar basic activity programs:
changing edittext text from button click listener 
Simple calculator program to add and subtract 2 numbers 
How to start an activity from main activity on clicking button, and pass data 
Starting second activity based on login and password details 
Getting results from child activity

Getting results from child activity: Android tutorial

Program description:

This program demonstrates how to pass data/results from sub activity (child activity) to Parent activity by using startActivityForResult method.

This program contains two activities first and second. First activity will contain a button to start second activity through explicit intent.

Second activity contains 2 buttons. 

On clicking first button second activity will send result success (RESULT_OK) to first activity. 
On clicking second button second activity will send result failure (RESULT_CANCELLED) and it will also send the reason for failure as "network failure" in intent.

Major functions used in this application:
startActivityForResult( intent, request_code); //used in first activity to start second activity.
onActivityResult(...);//used in first activity to catch the results from second activity.
setResult(..);//used in second activity to pass results back to first activity.
finish();//used in second activity to kill second activity to pass data back.



First Activity                                      Second Activity

 


When user clicks success in second   When user clicks failure in second
 


First Activity 
package com.techpalle.b15_childresult;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
//step 1: define req codes for child screens
public static final int CHLD_REQ1 = 1;
Button b; //for going to next screen

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
//step2 : write logic to start second activity for result
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(getApplicationContext(),
SecondScreen.class);
startActivityForResult(in, CHLD_REQ1);
}
});
}
//step 4: handling the results from the chld screens in parent activity
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if(requestCode == CHLD_REQ1){
if(resultCode == RESULT_OK){
Toast.makeText(getApplicationContext(), "child 1 success",
0).show();
}else{
Bundle b = data.getExtras();
String reason = b.getString("reason");
Toast.makeText(getApplicationContext(),
"child 1 fail bcoz.."+ reason,
0).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}

}



xml layout file for First Activity 
File name: activity_main.xml

  xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

Second screen
package com.techpalle.b15_childresult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SecondScreen extends Activity {
Button s,f;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
s = (Button) findViewById(R.id.button1);
f = (Button) findViewById(R.id.button2);

//step 3: set results and finish
s.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_OK);//for success
finish();
}
});
f.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//setResult(RESULT_CANCELED);//for failure
Intent in = new Intent();
in.putExtra("reason", "network failure");
setResult(RESULT_CANCELED, in);
finish();
}
});
}

}

xml layout file for Second Activity 
File name: second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent" >

Tags : startActivityForResult, pass data between activities, RESULT_OK, RESULT_CANCELLED, onActivityResult, setResult, finish, android tutorial, android free examples download

How to start an activity from main activity on clicking button, and pass data: Android tutorial

Program description:

Simple android program with 2 activities. First activity will have a button to start second activity. This is done by using explicit intent.
Along with intent we will pass some data "uid" (user id) to second activity.

Once second activity is started, it takes the data passed from first activity and displays in toast message. Second activity will have a button , on clicking it second activity will be destroyed.


               First Activity                                                Second Activity




First Activity
package com.techpalle.b15_startactivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
Intent in = getIntent();
String action = in.getAction();
Toast.makeText(this, "action.."+action, 0).show();
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(),
SecondaryScreen.class);
in.putExtra("uid", "satish");
startActivity(in);
}
});
}

@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;
}

}
xml layout file for First Activity 
File name: activity_main.xml
  xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


Second Activity
package com.techpalle.b15_startactivity;

import java.io.Closeable;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SecondaryScreen extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
String action = in.getAction();
Bundle bnd = in.getExtras();
String uid = bnd.getString("uid");
Toast.makeText(this, "uid is.."+uid+"..action.."+action, 0).show();
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
// TODO Auto-generated method stub
}

}

xml layout file for Second Activity 
File name: second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome second.."
android:textAppearance="?android:attr/textAppearanceLarge" />



For complete code, use below link to download it..
https://drive.google.com/file/d/0BzRRGL2xSFLGWnhtaVBkTjJMclk/edit?usp=sharing

Tags: startactivity, pass data between activities, getintent, android tutorial

android activity interview questions and answers

Monday, March 23, 2015

How to start an activity from main activity on clicking button, and pass data: Android tutorial

Program description:

Simple android program with 2 activities. First activity will have a button to start second activity. This is done by using explicit intent.
Along with intent we will pass some data "uid" (user id) to second activity.

Once second activity is started, it takes the data passed from first activity and displays in toast message. Second activity will have a button , on clicking it second activity will be destroyed.


               First Activity                                                Second Activity




First Activity
package com.techpalle.b15_startactivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
Intent in = getIntent();
String action = in.getAction();
Toast.makeText(this, "action.."+action, 0).show();
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(),
SecondaryScreen.class);
in.putExtra("uid", "satish");
startActivity(in);
}
});
}

@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;
}

}
xml layout file for First Activity 
File name: activity_main.xml
  xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


Second Activity
package com.techpalle.b15_startactivity;

import java.io.Closeable;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SecondaryScreen extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
String action = in.getAction();
Bundle bnd = in.getExtras();
String uid = bnd.getString("uid");
Toast.makeText(this, "uid is.."+uid+"..action.."+action, 0).show();
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
// TODO Auto-generated method stub
}

}

xml layout file for Second Activity 
File name: second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome second.."
android:textAppearance="?android:attr/textAppearanceLarge" />



For complete code, use below link to download it..
https://drive.google.com/file/d/0BzRRGL2xSFLGWnhtaVBkTjJMclk/edit?usp=sharing

Tags: startactivity, pass data between activities, getintent, android tutorial

android activity interview questions and answers

Starting second activity based on login and password details: Android tutorial

Program description:

Starting second activity based on the login and password credentials given in first activity.

First activity will have 2 edit texts and 1 button. User will be prompted to enter user name in edit text1, and enter password in edit text2.

if user id is "techpalle" and password is"1234", then it will start second activity and pass the uid entered by user to second screen using explicit intent.

If user enters user id, and password wrong, then a toast message will be displayed, content of edit text 1 and edit text 2 will be cleared and automatically control will be moved to edit text1 to make user comfortable.


Once second activity is started, it takes the data passed from first activity and displays in a text view. This data passing between activities will be done by using putExtra() method.


Note: If user fails to enter user name or password correctly for 3 times, then main activity will be closed by using finish() method.


First Activity                                       First Activity (when pw is wrong)

 

First Activity (when pw is correct)          Second Activity

 


First Activity 
package com.techpalle.b15_assignment;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
EditText e,f;
Button b;
TextView tv;
Integer cnt = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e = (EditText) findViewById(R.id.editText1);
f = (EditText) findViewById(R.id.editText2);
b = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.textView1);
tv.setText(tv.getText().toString()+ cnt.toString());

b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String uid = e.getText().toString();
String pw = f.getText().toString();
boolean bu = uid.equals("techpalle");
boolean bp = pw.equals("1234");

if(bu && bp){
Intent in = new Intent(getApplicationContext(),
SecondScreen.class);
in.putExtra("uid", uid);
cnt = 0;
startActivity(in);
}else if(bu == false && bp == false){
Toast.makeText(getApplicationContext(),
"uid & pw wrong..try again", 0).show();
cnt++;
tv.setText("No of tries: "+cnt);
e.setText("");
f.setText("");
e.requestFocus();
}else if(bu == false){
Toast.makeText(getApplicationContext(),
"uid wrong..try again", 0).show();
cnt++;
e.setText("");
e.requestFocus();
tv.setText("No of tries: "+cnt);
}else{
Toast.makeText(getApplicationContext(),
"pw wrong..try again", 0).show();
cnt++;
f.setText("");
f.requestFocus();
tv.setText("No of tries: "+cnt);
}

if(cnt == 3){
Toast.makeText(getApplicationContext(),
"Tries over..", 0).show();
finish();
}
}
});
}

}


xml layout file for First Activity 
File name: activity_main.xml
  xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
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" >
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="22dp"
android:ems="10" >


android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="14dp"
android:layout_marginTop="16dp"
android:ems="10" />



Second screen
package com.techpalle.b15_assignment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondScreen extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
Bundle b = in.getExtras();
String uid = b.getString("uid");
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("Welcome.."+uid);
}
}
xml layout file for Second Activity 
File name: second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />



Download complete code : Click to download

Tags: Login page android, start activity on login, pass user id and password , android tutorial

How to start an activity and pass data to second screen: Android tutorial


This post will try to cover how to start a second activity from main activity on button click and pass data.
I will assume that you have 2 activities and 2 xml files (one for each activity).
First activity has one button, on clicking which we will start the second activity by using explicit intent. While starting the second activity we will use putExtra() method to pass the data to second activity.
Once second activity is started, the data passed from first activity will be displayed in second activity as toast messages.
Second activity has a button, on clicking which we will close second activity by using finish() method.

First screen                                                                  Second screen






First Activity

package com.techpalle.b15_startactivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(),
SecondaryScreen.class);
in.putExtra("uid", "satish");
startActivity(in);
}
});
}

@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;
}

}

xml layout file for First Activity 
File name: activity_main.xml
  xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


Second Activity
package com.techpalle.b15_startactivity;

import java.io.Closeable;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SecondaryScreen extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
String action = in.getAction();
Bundle bnd = in.getExtras();
String uid = bnd.getString("uid");
Toast.makeText(this, "uid is.."+uid+"..action.."+action, 0).show();
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
// TODO Auto-generated method stub
}

}

xml layout file for Second Activity 
File name: second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome second.."
android:textAppearance="?android:attr/textAppearanceLarge" />

Tags : android, startactivity, putextras, pass data between activities, start second activity, android tutorial

Download complete code : Click to download

Android interview questions on this topic:
android activity interview questions and answers

Similar basic activity programs:
changing edittext text from button click listener 
Simple calculator program to add and subtract 2 numbers 
How to start an activity from main activity on clicking button, and pass data 
Starting second activity based on login and password details 
Getting results from child activity

Getting results from child activity: Android tutorial

Program description:

This program demonstrates how to pass data/results from sub activity (child activity) to Parent activity by using startActivityForResult method.

This program contains two activities first and second. First activity will contain a button to start second activity through explicit intent.

Second activity contains 2 buttons. 

On clicking first button second activity will send result success (RESULT_OK) to first activity. 
On clicking second button second activity will send result failure (RESULT_CANCELLED) and it will also send the reason for failure as "network failure" in intent.

Major functions used in this application:
startActivityForResult( intent, request_code); //used in first activity to start second activity.
onActivityResult(...);//used in first activity to catch the results from second activity.
setResult(..);//used in second activity to pass results back to first activity.
finish();//used in second activity to kill second activity to pass data back.



First Activity                                      Second Activity

 


When user clicks success in second   When user clicks failure in second
 


First Activity 
package com.techpalle.b15_childresult;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
//step 1: define req codes for child screens
public static final int CHLD_REQ1 = 1;
Button b; //for going to next screen

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
//step2 : write logic to start second activity for result
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(getApplicationContext(),
SecondScreen.class);
startActivityForResult(in, CHLD_REQ1);
}
});
}
//step 4: handling the results from the chld screens in parent activity
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if(requestCode == CHLD_REQ1){
if(resultCode == RESULT_OK){
Toast.makeText(getApplicationContext(), "child 1 success",
0).show();
}else{
Bundle b = data.getExtras();
String reason = b.getString("reason");
Toast.makeText(getApplicationContext(),
"child 1 fail bcoz.."+ reason,
0).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}

}



xml layout file for First Activity 
File name: activity_main.xml

  xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

Second screen
package com.techpalle.b15_childresult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SecondScreen extends Activity {
Button s,f;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
s = (Button) findViewById(R.id.button1);
f = (Button) findViewById(R.id.button2);

//step 3: set results and finish
s.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_OK);//for success
finish();
}
});
f.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//setResult(RESULT_CANCELED);//for failure
Intent in = new Intent();
in.putExtra("reason", "network failure");
setResult(RESULT_CANCELED, in);
finish();
}
});
}

}

xml layout file for Second Activity 
File name: second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent" >

Tags : startActivityForResult, pass data between activities, RESULT_OK, RESULT_CANCELLED, onActivityResult, setResult, finish, android tutorial, android free examples download

Older Posts Home