.
106 Android Studio Using Intent Object
This tutorial is based on http://developer.android.com/training/basics/firstapp/starting-activity.html .
The tutorial was done with Android Studio 14
In this lesson, you’ll add some code to your project so that it starts a new activity when the user clicks the Send button.
1) Start up
File Name: 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="horizontal">
<TextView android:text="Name:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="Type name here..." />
<Button
android:id="@+id/send_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
/>
</LinearLayout>
|
File Name: MainActivity.java
package com.notarazi.myemptyproject1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.send_message);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sendMessage(v);
}
});
}
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.edit_message);
Toast.makeText(getApplicationContext(), "You have entered, \n" + editText.getText().toString(), Toast.LENGTH_LONG).show();
}
}
|
2) Add a new activity.
Right-click the app project and select New/Activity/Empty Activity.
Type a name “DisplayMessage”.
Edit the layout as follows.
File name: activity_display_message.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.notarazi.myemptyproject1.DisplayMessage">
<TextView
android:id="@+id/text_message"
android:text="..."
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
|
Observe the Java file for the second activity.
No need to change anything unless it is different from the example below.
File name: DisplayMessage.java
package com.notarazi.myemptyproject1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class DisplayMessage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
}
}
|
Observe the Manifest file.
No need to change anything unless it is different from the example below.
File Name: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.notarazi.myemptyproject1" >
<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=".DisplayMessage" >
</activity>
</application>
|
We have successfully added a second activity to the project.
However, the activity will not come out on the screen until we write the code to do so.
|
3) Add the code to MainActivity
File Name: MainActivity.java
package com.notarazi.myemptyproject1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.send_message);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sendMessage(v);
}
});
}
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.edit_message);
Toast.makeText(getApplicationContext(), "You have entered, \n" + editText.getText().toString(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, DisplayMessage.class);
startActivity(intent);
}
}
|
OUTCOME.
Notice that when the user tap the button, the program displays Toast message while starting a new screen (activity).
We use an object called Intent to request Android System to start a new activity.
|
4) Pass the text value from first activity to second activity
Intent Object is known as a messaging object between applications and the Android System.
Besides requesting a start of an activity, Intent Object also provides a storage exchange between activities.
|
Edit MainActivity.java to comment Toast Message and pass input value to Intent Object.
File Name: MainActivity.java
package com.notarazi.myemptyproject1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.send_message);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sendMessage(v);
}
});
}
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.edit_message);
//Toast.makeText(getApplicationContext(), "You have entered, \n" + editText.getText().toString(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, DisplayMessage.class);
String message = "You have entered, \n"+editText.getText().toString();
intent.putExtra("EXTRA_MESSAGE", message);
startActivity(intent);
}
}
|
Edit DisplayMessage.java to pick up input value from Intent Object.
File Name: DisplayMessage.java
package com.notarazi.myemptyproject1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class DisplayMessage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra("EXTRA_MESSAGE");
// Set the content view
setContentView(R.layout.activity_display_message);
// Hook text_message as textView
// Set new value for textView
TextView textView = (TextView) findViewById(R.id.text_message);
textView.setText(message);
}
}
|
OUTCOME
We have successfully used Intent Object to request a start of new activity and to carry along data value.
In reality, a few applications may be running on Android System at the same time.
There could be an incidence of “similar parameter key” used by two or more applications.
To avoid this, we need to modify our codes so that the parameter key (ie EXTRA_MESSAGE) is unique to our application only.
In order to do that, we append our package name to the word “EXTRA_MESSAGE”
|
Edit MainActivity.java to modify Intent Key as a static string value.
File Name: MainActivity.java
package com.notarazi.myemptyproject1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.notarazi.myemptyproject1.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.send_message);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sendMessage(v);
}
});
}
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.edit_message);
//Toast.makeText(getApplicationContext(), "You have entered, \n" + editText.getText().toString(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, DisplayMessage.class);
String message = "You have entered, \n"+editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
|
Edit DisplayMesage.java to receive Intent Key as a variable of MainActivity
File Name: DisplayMessage.java
package com.notarazi.myemptyproject1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class DisplayMessage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Set the content view
setContentView(R.layout.activity_display_message);
// Hook text_message as textView
// Set new value for textView
TextView textView = (TextView) findViewById(R.id.text_message);
textView.setText(message);
}
}
|
OUTCOME.
We should be getting the same outcome.
Notice that we declared the variable as “public final static” ie a constant variable which will never change from its initial value.
In Java this type of variable is declared with capitalized letter names, ie “EXTRA_MESSAGE”
|
DOWNLOAD
.
No comments:
Post a Comment