First Activity
-------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".Firstact" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/txt"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/txt_val"
android:id="@+id/txt1"
/>
<Button
android:id="@+id/click_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sub" />
</LinearLayout>
Java Code
----------------
package com.example;
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;
public class Firstact extends Activity {
Button bn;
EditText et;
Bundle bundle;
int requestCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firstact);
bn=(Button) findViewById(R.id.click_button);
et=(EditText) findViewById(R.id.txt1);
requestCode=1;
bn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent("com.example.Second"); /* creating intent object */
bundle=new Bundle(); /* bundle for passing valu*/
bundle.putString("msg",et.getText().toString());
intent.putExtras(bundle);
startActivityForResult(intent, requestCode);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_firstact, menu);
return true;
}
public void onActivityResult(int rc,int resultCode,Intent data)/* validating data and request from second page*/
{
if(rc==requestCode)
{
if(resultCode==RESULT_OK)
{
et.setText(data.getData().toString());
}
}
}
}
This is basic application it only shows how to pass a value from 1st page(main activity) to second page(2nd activity) and in reverse.
Second Activity
--------------------
<?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/msg"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/txt_val"
android:id="@+id/txt2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sub"
android:id="@+id/b2"
/>
</LinearLayout>
Java Code
---------------
package com.example;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Second extends Activity{
Button bn1;
EditText txt;
Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_act);
bn1=(Button) findViewById(R.id.b2);
txt=(EditText) findViewById(R.id.txt2);
bundle=getIntent().getExtras();
txt.setHint(bundle.getString("msg"));
bn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent data=new Intent();
data.setData(Uri.parse(txt.getText().toString()));
setResult(RESULT_OK,data);
finish();
}
});
}
}


No comments:
Post a Comment