Introduction
In the previous article we learned about the View and ViewGroup objects. We learned about Layout and resources as well. In a previous article we created a hierarchy of View and ViewGroup objects. Now I will develop an activity that shows how to migrate from one activity to another. However choosing an activity from a new project in Android Studio has already been explained in my previous article.
Creating an activity named MainActivity
What do we need to do? We need to write the code for making an activity just as in a Layout file named activity_main.xml and use the following procedure:
activity_main.xml
This file contains all the attributes of the view elements necessary for making an 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
String.xml contains the string resources such as "
app name" , "
edit_message" ,"action settings" and so on. So we must remove the default string "hello world".
- <?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>
It is just an activity with a
Text Field with a
Button. Now just try to attach a
listener of the button so that the message can be sent and another activity will receive that message.
XML Design
The previous code produces the following output:
Building an Intent
After creation of the first activity with a
TextField and a
Button as explained above it is now time to initiate this activity using a thing called an Intent.
An
Intent is an object that provides runtime binding between two separate components (such as two activities). The Intent represents an application's "intent to do something". Intents could be used for a wide variety of tasks, but most often they are used to start another activity.
In MainActivity.java declare a method named sendMessage(View view) with these parameters and inside it create an Intent to start an activity called DisplayMessageActivity with this code given below:
- 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);
-
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)
- {
-
- 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) {
-
- getMenuInflater().inflate(R.menu.menu_main_, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
-
-
- int id = item.getItemId();
-
-
- 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 the second 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
Click Finish and open the ChildActivity.java file and use the following procedure:
-
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();
-
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);
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 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);
-
- Intent intent = getIntent();
- String message = intent.getStringExtra(Main_Activity.EXTRA_MESSAGE);
-
-
-
- TextView textview = new TextView(this);
- textview.setTextSize(50);
- textview.setText(message);
-
-
- setContentView(textview);
-
- }
-
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- 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.
Summary
In this article we learned how to migrate from one Activity to another Activity using Intents. In Android everything on the screen that is user intractable is called an activity. This article explains the entire procedure very clearly. However it includes nearly three major steps to do as shown, for example creating an activity, building an intent and creation of another activity for receiving the pushed text.