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>
MainActivity.java
package com.blogspot.kantapp.bundleinandroid; 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.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>
Second.java
package com.blogspot.kantapp.bundleinandroid; 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.blogspot.kantapp.bundleinandroid"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <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>
No comments:
Post a Comment