Introduction
-
change Relative Layout with Linear Layout
-
Add a Text Field
-
Add String resource
-
Add a button and its listener as well
activity_main.xml
This file contains all the attributes of the view elements necessary for making activity. Here we are creating a simple activity that contains only a Text Field (please enter a text) and a button named Send.
Here is the code,
- // activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <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="horizontal">
- <EditText android:id="@+id/edit_message"
- android:layout_weight="1"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:hint="@string/edit_message" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/button_send"
- android:onClick="sendMessage"/>
- </LinearLayout>
It is just an activity with a Text Field and a Button. Now we attach a navigation-among-activities-using-android-studio:onClick attribute so that the message can be sent and another activity will receive that message.
String.xml- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">Megatron</string>
- <string name="edit_message">please enter a text</string>
- <string name="button_send">Send</string>
- <string name="action_settings">Settings</string>
- <string name="title_activity_main">Main_Activity</string>
- <string name="title_activity_child">ChildActivity</string>
- </resources>

- public void sendMessage(View view) {
- Intent intent = new Intent(this, ChildActivity.class);
- }
-
We need to create a class for ChildActivity because the reference to this class might raise an error in Android Studio.
-
Note that in the constructor used here, "this" is used because the Activity is a subclass of the Context class and imports the Intent class. ( import navigation-among-activities-using-android-studio.content.Intent; ).However, Android Studio automatically suggests that we press ALT+ENTER and the relevant class will be imported and the packages as well.
-
Inside the sendMessage() method use findViewById() to get an EditText element.
- EditText editText = (EditText) findViewById(R.id.edit_message);
- EditText editText = (EditText) findViewById(R.id.edit_message);
-
At the top of the file import the EditText class again. Press ALT+ENTER and the relevant class will be imported and the packages as well.
-
Assign the text to a local message variable and use the putExtra() method as in the following:
- String message = editText.getText().toString();
- intent.putExtra(EXTRA_MESSAGE, message);
- }
-
We must add an EXTRA_MESSAGE definition as shown below:
- public class MyActivity extends ActionBarActivity {
- public final static String EXTRA_MESSAGE = "com.example..megatron.MESSAGE";
- }
-
Finally just finish the sendMessage() method with startActivity(intent) and now invoke the send button.
MainActivity.java will look such as:
- package com.example.gkumar.megatron;
- import android.content.Intent;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.support.v7.internal.widget.AdapterViewCompat;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.EditText;
- public class Main_Activity extends ActionBarActivity {
- Public final static String EXTRA_MESSAGE="com.example.gkumar.megatron.MESSAGE"
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main_);
- }
- public void sendMessage(View view)
- {
- // building an Intent
- Intent intent= new Intent(this,ChildActivity.class);
- EditText editText = (EditText) findViewById(R.id.edit_message);
- String message = editText.getText().toString();
- intent.putExtra(EXTRA_MESSAGE , message);
- startActivity(intent);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main_, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Now the sending and action procedure is completed. Now after clicking the send button a something must be there to catch the text being entered by the user. Then for this we need to create a new Activity.
Create a second activity
-
Go to the Java directory on the package name com.example.gkumar.megatron. Right-click it and select new > Activity > Blank Activity.

-
In the naming window of Activity choose Activity name as ChildActivity
-
layout Name : activity_child.xml
-
Title: ChildActivity
-
Hierarchical parent: MainActivity
-
package name: com.example.gkumar.megatron

-
The newly created Activity file must have an implementation of onCreate() and onOptionsItemSelected(), that handles the action bar's up behavior and must remain untouched.
-
Remove the onCreateOptionsMenu() method, we don't need it.
Receive the Intent
As we all know and learned above that every activity needs an Intent irrespective of the user's handling of Activity.
-
Look at the default method generated by setContentView(R.layout.activity_display_message). Remove that line.
-
Get the intent and assign it to a local variable.
- Intent intent = getIntent();
- Intent intent = getIntent();
-
At the top import the Intent class as we did in MainActivity.java.
-
Now receive the message delivered by the previous activity using the getStringExtra() method.
- String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
- String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
Display the Message
-
in the onCreate() method create a TextView object
-
set the text size and message with setText();
-
set the text size up to 50 and the text as a message.
-
add the TextView as a root view of the activity's layout by ing it to setContentView(textview);
ChildActivity.java
- package com.example.gkumar.megatron;
- import android.content.Intent;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
- public class ChildActivity extends ActionBarActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // receiving the intent
- Intent intent = getIntent();
- String message = intent.getStringExtra(Main_Activity.EXTRA_MESSAGE);
- // display the message
- TextView textview = new TextView(this);
- textview.setTextSize(50);
- textview.setText(message);
- // set the text view as the activity layout
- setContentView(textview);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_child, menu);
- return true;
- }
- }
Output
After executing the preceding code I am getting the following errors:
Although I am working on it I hope the output will be posted very soon.


Gowtham RajamanickamPosted Apr 7, 2016, 4:28 AM
Good Show.
Neeraj KumarPosted Jul 30, 2015, 6:13 PM
Good
Abhishek AroraPosted Jan 14, 2015, 11:09 PM
Nice article..!!