This blog will help you to learn about "how send data from second activity to fist activity....."
1. Click on file ->new->Android Application Project
2. Give name to your android application and chhose Minimum Required SDK and Targed SDK
3. Then click on
next->next->next->select balnk activity ->next and then click on finish
After that click on src ->com.example.ApplicationName
open MainActivity.java file..
and copy paste the following code..
--------------------------------------------------------------------------------------------------------------
package com.example.applicationname;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
final static int REQ_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, REQ_CODE);
}});
}
@Override
protected void onActivityResult(int reqCode, int resCode, Intent i) {
// TODO Auto-generated method stub
if(reqCode == REQ_CODE){
if (resCode == RESULT_OK){
String ss=i.getStringExtra("message");
TextView tv=(TextView)findViewById(R.id.TextView);
tv.setText(ss);
Toast.makeText(getApplicationContext(), ss, Toast.LENGTH_LONG).show();
}
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
------------------------------------------------------------------------------------------------------------
4. Click on res->layout->activity_main.xml and copy paste the following code
------------------------------------------------------------------------------------------------------------
<RelativeLayout 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: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="com.example.applicationame.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:text="Start second activity" />
<TextView
android:id="@+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="48dp"
/>
</RelativeLayout>
-------------------------------------------------------------------------------------------------------------
5. Then Right click on com.example.applicationname
select new ->other ->Android Activity->next
6. After clicking next.
Select blank activity then give activity name SecondActivity then click on finish..
In SecondActivity.class copy paste the following code..
-------------------------------------------------------------------------------------------------------------
package com.example.applicationname;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button btnReturnOK = (Button)findViewById(R.id.button1);
btnReturnOK.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
EditText et=(EditText)findViewById(R.id.editText1);
String ss=et.getText().toString();
i.putExtra("message", ss);
setResult(RESULT_OK, i);
finish();
}});
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
-----------------------------------------------------------------------------------------------------------
7. Click on res->layout->activity_second.xml and copy paste the following code
----------------------------------------------------------------------------------------------------------
<RelativeLayout 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: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="com.example.applicationname.SecondActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Enter text here...">
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="Send" />
</RelativeLayout>
------------------------------------------------------------------------------------------------------------
8. save you project and run ....
after running application click on start second activity button
then enter the text that you want to send to first activity..
and click on send button..
you will see that the text displayed in first activity....
1. Click on file ->new->Android Application Project
2. Give name to your android application and chhose Minimum Required SDK and Targed SDK
3. Then click on
next->next->next->select balnk activity ->next and then click on finish
After that click on src ->com.example.ApplicationName
open MainActivity.java file..
and copy paste the following code..
--------------------------------------------------------------------------------------------------------------
package com.example.applicationname;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
final static int REQ_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, REQ_CODE);
}});
}
@Override
protected void onActivityResult(int reqCode, int resCode, Intent i) {
// TODO Auto-generated method stub
if(reqCode == REQ_CODE){
if (resCode == RESULT_OK){
String ss=i.getStringExtra("message");
TextView tv=(TextView)findViewById(R.id.TextView);
tv.setText(ss);
Toast.makeText(getApplicationContext(), ss, Toast.LENGTH_LONG).show();
}
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
------------------------------------------------------------------------------------------------------------
4. Click on res->layout->activity_main.xml and copy paste the following code
------------------------------------------------------------------------------------------------------------
<RelativeLayout 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: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="com.example.applicationame.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:text="Start second activity" />
<TextView
android:id="@+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="48dp"
/>
</RelativeLayout>
-------------------------------------------------------------------------------------------------------------
5. Then Right click on com.example.applicationname
select new ->other ->Android Activity->next
6. After clicking next.
Select blank activity then give activity name SecondActivity then click on finish..
In SecondActivity.class copy paste the following code..
-------------------------------------------------------------------------------------------------------------
package com.example.applicationname;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button btnReturnOK = (Button)findViewById(R.id.button1);
btnReturnOK.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
EditText et=(EditText)findViewById(R.id.editText1);
String ss=et.getText().toString();
i.putExtra("message", ss);
setResult(RESULT_OK, i);
finish();
}});
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
-----------------------------------------------------------------------------------------------------------
7. Click on res->layout->activity_second.xml and copy paste the following code
----------------------------------------------------------------------------------------------------------
<RelativeLayout 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: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="com.example.applicationname.SecondActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Enter text here...">
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="Send" />
</RelativeLayout>
------------------------------------------------------------------------------------------------------------
8. save you project and run ....
after running application click on start second activity button
then enter the text that you want to send to first activity..
and click on send button..
you will see that the text displayed in first activity....
No comments:
Post a Comment