1) Create a new Android project go to
file-> new Android application->
Type the Application name as:- FirsttoSecond
package name as:-com.example.firsttosecond
then click next
2) Then follow these steps:-
click next
3)
4)
5) click next and change the Activity name to FirstActivity
and click finish
6) The layout file will open
click to activity_first.xml next to graphical layout tab
and write this code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/entertext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/hinttext" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/buttonText" />
</LinearLayout>
7) Go to values folder -> Strings.xml in that put these fields
<string name="hinttext">Enter some data</string>
<string name="buttonText">Submit</string>
8) Go to source folder open FirstActivity.java
and write this code
package com.example.firsttosecond;
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 FirstActivity extends Activity implements OnClickListener{
private EditText editData;
private Button btnClick;
private String getdata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editData=(EditText)findViewById(R.id.entertext);
btnClick=(Button)findViewById(R.id.submit);
btnClick.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getdata=editData.getText().toString(); // here we are getting the data from edittext and saving it to a string data
//An Intent is exactly what it describes. It's an "intention" to do an action.
//here we are starting the other activity from the current activity while clicking on a button
// it takes two parameter firt is the current contextPackage and
// second parameter is the class to which it going to start
Intent inSecond=new Intent(FirstActivity.this,SecondActivity.class);
inSecond.putExtra("data",getdata);
// inSecond.putExtra is used to pass the data from first activity to second
// it takes two parameter first the key and another one the value
// so here data is a key and
//getdata is a value which we will get through editText
startActivity(inSecond); //this function is used to start your second activity
finish(); //to kill your current activity
}
}
9) Right click on layout folder go to New->click Android Xml File type the file name secondactivity_xml under Root element: select the linear layout and click finish.
Now open the secondactivity_xml and write this code.
<?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/texttoShow"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
10) Go to source folder -> right click on package name i.e com.example.firsttosecond
select New-> class-> Type the name SecondActivity->In Superclass tab type android.app.Activity
and click finish
11) Open the SecondActivity.java file and type this code
package com.example.firsttosecond;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
private TextView txtShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity_xml);
txtShow=(TextView)findViewById(R.id.texttoShow);
Intent intent = this.getIntent(); // this is used get the intent from the first activity
/* Obtain String from Intent */
String displayData = intent.getExtras().getString("data");// as we have passed the string data
// from first activity so here we used getString method with the same key as we used in first activity
txtShow.setText(displayData); //this setText method will display the data on the screen
}
///This method will override your back key to go back to the first activity
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent inFirst=new Intent(SecondActivity.this,FirstActivity.class);
startActivity(inFirst);
finish(); //here again we are killing this activity so that its instance will not run in the background.
}
}
11) Go to Android Manifest file open it and write this code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.firsttosecond"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.firsttosecond.FirstActivity"
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="com.example.firsttosecond.SecondActivity"></activity>
</application>
</manifest>
12) Your done with this now compile your project and run it.
No comments:
Post a Comment