1. Create MainActivity.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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.kiran.lesson3_gettingdata.MainActivity">
<TextView
android:id="@+id/myaddress_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textStyle="normal|bold"
android:textSize="36sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
<Button
android:text="Call Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:id="@+id/next_btn"
android:onClick="callNext"
android:layout_below="@+id/myaddress_tv"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainActivity.java:
package com.example.kiran.lesson3_gettingdata;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mAddressTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAddressTv = (TextView) findViewById(R.id.myaddress_tv);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==200){ //from second Activity
if(resultCode==RESULT_OK) //Success Data
{
String address = data.getStringExtra("myaddress");
mAddressTv.setText(address);
}
}
}
public void callNext(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(intent,200);
}
}
3. SecondActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_second"
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.kiran.lesson3_gettingdata.SecondActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="89dp"
android:id="@+id/address_et" />
<Button
android:text="Send Data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendData"
android:layout_below="@+id/address_et"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:id="@+id/send_data_btn" />
</RelativeLayout>
4 SecondActivity.java:
package com.example.kiran.lesson3_gettingdata;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class SecondActivity extends AppCompatActivity {
private EditText mAddressEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mAddressEt = (EditText) findViewById(R.id.address_et);
}
public void sendData(View view) {
String address = mAddressEt.getText().toString();
Intent intent = new Intent();//Create an empty intent
intent.putExtra("myaddress", address);
setResult(RESULT_OK,intent); //Set request_code implicitly and result_code and intent explicitly
finish();
}
}
No comments:
Post a Comment