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/editText1name" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" > <requestFocus /> </EditText> <Button android:id="@+id/button1send" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="30dp" android:text="Send" android:textSize="20sp" /> </LinearLayout>
MainActivity.java
package com.blogspot.kantapp.passingdatausingintentinandroid; 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; public class MainActivity extends Activity { EditText nameEdit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nameEdit = (EditText) findViewById(R.id.editText1name); Button sendBtn = (Button) findViewById(R.id.button1send); sendBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String name = nameEdit.getText().toString(); Intent in = new Intent(getApplicationContext(), Main2Activity.class); in.putExtra("myname", name); startActivity(in); } }); } }
activity_main2.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:text="name will apears here" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>
Main2Activity.java
package com.blogspot.kantapp.passingdatausingintentinandroid; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class Main2Activity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); TextView display = (TextView) findViewById(R.id.textView1name); Intent in = getIntent(); String name = in.getStringExtra("myname"); display.setText(name); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blogspot.kantapp.passingdatausingintentinandroid"> <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=".Main2Activity"></activity> </application> </manifest>
No comments:
Post a Comment