In MainActivity enter details and send them to Second Activity
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/namedit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter phone number"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/phedit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/sendbutton1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Send"
android:textSize="20sp" />
</LinearLayout>
second.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/textView1name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2phnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MainActivity.java:
package com.ram.bundledemo;
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.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText nameEdit,phnumberEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Setup UI
nameEdit = (EditText)findViewById(R.id.namedit);
phnumberEdit = (EditText)findViewById(R.id.phedit);
Button sendBtn = (Button)findViewById(R.id.sendbutton1);
sendBtn.setOnClickListener(new OnClickListener() {
//Listner for button
@Override
public void onClick(View arg0) {
//Getting data from EditText
String name = nameEdit.getText().toString();
String phnumber = phnumberEdit.getText().toString();
if(name.length()==0&&phnumber.length()==0){
Toast.makeText(getApplicationContext(), "plz enter all details", Toast.LENGTH_SHORT).show();
}else{
//Converting phnumber to long type
long phno = Long.parseLong(phnumber);
//Creating Bundle object
Bundle b = new Bundle();
//Storing data into bundle
b.putString("myname", name);
b.putLong("phnum", phno);
//Creating Intent object
Intent in = new Intent(getApplicationContext(), Second.class);
//Storing bundle object into intent
in.putExtras(b);
startActivity(in);
}
}
});
}
}
Second.java:
package com.ram.bundledemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView nameText = (TextView)findViewById(R.id.textView1name);
TextView phText = (TextView)findViewById(R.id.textView2phnumber);
// getIntent():Return the intent that started this activity.
Intent in = getIntent();
//Getting bundle
Bundle b = in.getExtras();
//Getting data from bundle
String name = b.getString("myname");
long phnumber = b.getLong("phnum");
//Converting long type to String
String phno = Long.toString(phnumber);
//Binding values to TextViews
nameText.setText("name :"+name);
phText.setText("phnumber :"+phno);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ram.bundledemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ram.bundledemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Second"></activity>
</application>
</manifest>
sir whether we should add activity tag for Second or it would be added automatically
ReplyDeleteThe information which you provides is very much useful for the Android Learners. Thank you for your valuable information. I found 123trainings is the best Android Online Traininginstitute in Hyderabad, India .
ReplyDeleteThis comment has been removed by the author.
ReplyDelete