Create an other activity lesson 3
-
Upload
kalluri-vinay-reddy -
Category
Engineering
-
view
32 -
download
1
Transcript of Create an other activity lesson 3
ByKalluri Vinay Reddy
Android Clubwwwfacebookcomvitccandroidclub
Starting Another ActivityLesson 3
you have an app that shows an activity
(a single screen) with a text field and a button
In this lesson yoursquoll add some code to
MyActivity that starts a new activity when the user clicks
the Send button
Respond to the Send Button1In Android Studio from the reslayout directory edit theactivity_myxml file2To the ltButtongt element add the androidonClick attributereslayoutactivity_myxmlltButton androidlayout_width=wrap_content androidlayout_height=wrap_content androidtext=stringbutton_send androidonClick=sendMessage gtThe androidonClick attributersquos value sendMessage is the name of a method in your activity that the system calls when the user clicks the button
1In the javacommycompanymyfirstapp directory open the MyActivityjava file
2Within the MyActivity class add the sendMessage() method stub shown below
javacommycompanymyfirstappMyActivityjava
Called when the user clicks the Send button
public void sendMessage(View view) Do something in response to button
In order for the system to match this method to the method name given to androidonClick the signature must be exactly as shownSpecifically the method mustbullBe publicbullHave a void return valuebullHave a View as the only parameter (this will be the View that was clicked)
Build an IntentIn MyActivityjava inside the sendMessage() method create an Intent to start an activity Called DisplayMessageActivity with the following codejavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass)
Note The reference to DisplayMessageActivity will raise an error if yoursquore using an IDE such as Android Studio because the class doesnrsquot exist yet Ignore the error for now yoursquoll create the class soon
The constructor used here takes two parameters
bullA Context as its first parameter (this is used because the Activity class is a subclass of Context)
bullThe Class of the app component to which the system should deliver the Intent (in this case the activity that should be started)
Android Studio indicates that you must import the Intent class
At the top of the file import the Intent classjavacommycompanymyfirstappMyActivityjava
import androidcontentIntent
Tip In Android Studio press Alt + Enter (option + return on Mac) to import missing classes
Inside the sendMessage() method use findViewById() to get the EditText element
javacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message)
Assign the text to a local message variable and use the putExtra() method to add its text value to the intentjavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view)
Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message)An Intent can carry data types as key-value pairs called extras The putExtra() method takes the key name in the first parameter and the value in the second parameter
At the top of the MyActivity class add the EXTRA_MESSAGE definition as follows
javacommycompanymyfirstappMyActivityjava
public class MyActivity extends ActionBarActivity
public final static String EXTRA_MESSAGE = commycompanymyfirstappMESSAGE
bullFor the next activity to query the extra data you should define the key for your intents extra using a public constant Its generally a good practice to define keys for intent extras using your apps package name as a prefix This ensures the keys are unique in case your app interacts with other apps
In the sendMessage() method to finish the intent call the startActivity() method passing it theIntent object created in step
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
you have an app that shows an activity
(a single screen) with a text field and a button
In this lesson yoursquoll add some code to
MyActivity that starts a new activity when the user clicks
the Send button
Respond to the Send Button1In Android Studio from the reslayout directory edit theactivity_myxml file2To the ltButtongt element add the androidonClick attributereslayoutactivity_myxmlltButton androidlayout_width=wrap_content androidlayout_height=wrap_content androidtext=stringbutton_send androidonClick=sendMessage gtThe androidonClick attributersquos value sendMessage is the name of a method in your activity that the system calls when the user clicks the button
1In the javacommycompanymyfirstapp directory open the MyActivityjava file
2Within the MyActivity class add the sendMessage() method stub shown below
javacommycompanymyfirstappMyActivityjava
Called when the user clicks the Send button
public void sendMessage(View view) Do something in response to button
In order for the system to match this method to the method name given to androidonClick the signature must be exactly as shownSpecifically the method mustbullBe publicbullHave a void return valuebullHave a View as the only parameter (this will be the View that was clicked)
Build an IntentIn MyActivityjava inside the sendMessage() method create an Intent to start an activity Called DisplayMessageActivity with the following codejavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass)
Note The reference to DisplayMessageActivity will raise an error if yoursquore using an IDE such as Android Studio because the class doesnrsquot exist yet Ignore the error for now yoursquoll create the class soon
The constructor used here takes two parameters
bullA Context as its first parameter (this is used because the Activity class is a subclass of Context)
bullThe Class of the app component to which the system should deliver the Intent (in this case the activity that should be started)
Android Studio indicates that you must import the Intent class
At the top of the file import the Intent classjavacommycompanymyfirstappMyActivityjava
import androidcontentIntent
Tip In Android Studio press Alt + Enter (option + return on Mac) to import missing classes
Inside the sendMessage() method use findViewById() to get the EditText element
javacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message)
Assign the text to a local message variable and use the putExtra() method to add its text value to the intentjavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view)
Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message)An Intent can carry data types as key-value pairs called extras The putExtra() method takes the key name in the first parameter and the value in the second parameter
At the top of the MyActivity class add the EXTRA_MESSAGE definition as follows
javacommycompanymyfirstappMyActivityjava
public class MyActivity extends ActionBarActivity
public final static String EXTRA_MESSAGE = commycompanymyfirstappMESSAGE
bullFor the next activity to query the extra data you should define the key for your intents extra using a public constant Its generally a good practice to define keys for intent extras using your apps package name as a prefix This ensures the keys are unique in case your app interacts with other apps
In the sendMessage() method to finish the intent call the startActivity() method passing it theIntent object created in step
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
Respond to the Send Button1In Android Studio from the reslayout directory edit theactivity_myxml file2To the ltButtongt element add the androidonClick attributereslayoutactivity_myxmlltButton androidlayout_width=wrap_content androidlayout_height=wrap_content androidtext=stringbutton_send androidonClick=sendMessage gtThe androidonClick attributersquos value sendMessage is the name of a method in your activity that the system calls when the user clicks the button
1In the javacommycompanymyfirstapp directory open the MyActivityjava file
2Within the MyActivity class add the sendMessage() method stub shown below
javacommycompanymyfirstappMyActivityjava
Called when the user clicks the Send button
public void sendMessage(View view) Do something in response to button
In order for the system to match this method to the method name given to androidonClick the signature must be exactly as shownSpecifically the method mustbullBe publicbullHave a void return valuebullHave a View as the only parameter (this will be the View that was clicked)
Build an IntentIn MyActivityjava inside the sendMessage() method create an Intent to start an activity Called DisplayMessageActivity with the following codejavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass)
Note The reference to DisplayMessageActivity will raise an error if yoursquore using an IDE such as Android Studio because the class doesnrsquot exist yet Ignore the error for now yoursquoll create the class soon
The constructor used here takes two parameters
bullA Context as its first parameter (this is used because the Activity class is a subclass of Context)
bullThe Class of the app component to which the system should deliver the Intent (in this case the activity that should be started)
Android Studio indicates that you must import the Intent class
At the top of the file import the Intent classjavacommycompanymyfirstappMyActivityjava
import androidcontentIntent
Tip In Android Studio press Alt + Enter (option + return on Mac) to import missing classes
Inside the sendMessage() method use findViewById() to get the EditText element
javacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message)
Assign the text to a local message variable and use the putExtra() method to add its text value to the intentjavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view)
Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message)An Intent can carry data types as key-value pairs called extras The putExtra() method takes the key name in the first parameter and the value in the second parameter
At the top of the MyActivity class add the EXTRA_MESSAGE definition as follows
javacommycompanymyfirstappMyActivityjava
public class MyActivity extends ActionBarActivity
public final static String EXTRA_MESSAGE = commycompanymyfirstappMESSAGE
bullFor the next activity to query the extra data you should define the key for your intents extra using a public constant Its generally a good practice to define keys for intent extras using your apps package name as a prefix This ensures the keys are unique in case your app interacts with other apps
In the sendMessage() method to finish the intent call the startActivity() method passing it theIntent object created in step
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
1In the javacommycompanymyfirstapp directory open the MyActivityjava file
2Within the MyActivity class add the sendMessage() method stub shown below
javacommycompanymyfirstappMyActivityjava
Called when the user clicks the Send button
public void sendMessage(View view) Do something in response to button
In order for the system to match this method to the method name given to androidonClick the signature must be exactly as shownSpecifically the method mustbullBe publicbullHave a void return valuebullHave a View as the only parameter (this will be the View that was clicked)
Build an IntentIn MyActivityjava inside the sendMessage() method create an Intent to start an activity Called DisplayMessageActivity with the following codejavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass)
Note The reference to DisplayMessageActivity will raise an error if yoursquore using an IDE such as Android Studio because the class doesnrsquot exist yet Ignore the error for now yoursquoll create the class soon
The constructor used here takes two parameters
bullA Context as its first parameter (this is used because the Activity class is a subclass of Context)
bullThe Class of the app component to which the system should deliver the Intent (in this case the activity that should be started)
Android Studio indicates that you must import the Intent class
At the top of the file import the Intent classjavacommycompanymyfirstappMyActivityjava
import androidcontentIntent
Tip In Android Studio press Alt + Enter (option + return on Mac) to import missing classes
Inside the sendMessage() method use findViewById() to get the EditText element
javacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message)
Assign the text to a local message variable and use the putExtra() method to add its text value to the intentjavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view)
Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message)An Intent can carry data types as key-value pairs called extras The putExtra() method takes the key name in the first parameter and the value in the second parameter
At the top of the MyActivity class add the EXTRA_MESSAGE definition as follows
javacommycompanymyfirstappMyActivityjava
public class MyActivity extends ActionBarActivity
public final static String EXTRA_MESSAGE = commycompanymyfirstappMESSAGE
bullFor the next activity to query the extra data you should define the key for your intents extra using a public constant Its generally a good practice to define keys for intent extras using your apps package name as a prefix This ensures the keys are unique in case your app interacts with other apps
In the sendMessage() method to finish the intent call the startActivity() method passing it theIntent object created in step
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
In order for the system to match this method to the method name given to androidonClick the signature must be exactly as shownSpecifically the method mustbullBe publicbullHave a void return valuebullHave a View as the only parameter (this will be the View that was clicked)
Build an IntentIn MyActivityjava inside the sendMessage() method create an Intent to start an activity Called DisplayMessageActivity with the following codejavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass)
Note The reference to DisplayMessageActivity will raise an error if yoursquore using an IDE such as Android Studio because the class doesnrsquot exist yet Ignore the error for now yoursquoll create the class soon
The constructor used here takes two parameters
bullA Context as its first parameter (this is used because the Activity class is a subclass of Context)
bullThe Class of the app component to which the system should deliver the Intent (in this case the activity that should be started)
Android Studio indicates that you must import the Intent class
At the top of the file import the Intent classjavacommycompanymyfirstappMyActivityjava
import androidcontentIntent
Tip In Android Studio press Alt + Enter (option + return on Mac) to import missing classes
Inside the sendMessage() method use findViewById() to get the EditText element
javacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message)
Assign the text to a local message variable and use the putExtra() method to add its text value to the intentjavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view)
Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message)An Intent can carry data types as key-value pairs called extras The putExtra() method takes the key name in the first parameter and the value in the second parameter
At the top of the MyActivity class add the EXTRA_MESSAGE definition as follows
javacommycompanymyfirstappMyActivityjava
public class MyActivity extends ActionBarActivity
public final static String EXTRA_MESSAGE = commycompanymyfirstappMESSAGE
bullFor the next activity to query the extra data you should define the key for your intents extra using a public constant Its generally a good practice to define keys for intent extras using your apps package name as a prefix This ensures the keys are unique in case your app interacts with other apps
In the sendMessage() method to finish the intent call the startActivity() method passing it theIntent object created in step
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
Build an IntentIn MyActivityjava inside the sendMessage() method create an Intent to start an activity Called DisplayMessageActivity with the following codejavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass)
Note The reference to DisplayMessageActivity will raise an error if yoursquore using an IDE such as Android Studio because the class doesnrsquot exist yet Ignore the error for now yoursquoll create the class soon
The constructor used here takes two parameters
bullA Context as its first parameter (this is used because the Activity class is a subclass of Context)
bullThe Class of the app component to which the system should deliver the Intent (in this case the activity that should be started)
Android Studio indicates that you must import the Intent class
At the top of the file import the Intent classjavacommycompanymyfirstappMyActivityjava
import androidcontentIntent
Tip In Android Studio press Alt + Enter (option + return on Mac) to import missing classes
Inside the sendMessage() method use findViewById() to get the EditText element
javacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message)
Assign the text to a local message variable and use the putExtra() method to add its text value to the intentjavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view)
Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message)An Intent can carry data types as key-value pairs called extras The putExtra() method takes the key name in the first parameter and the value in the second parameter
At the top of the MyActivity class add the EXTRA_MESSAGE definition as follows
javacommycompanymyfirstappMyActivityjava
public class MyActivity extends ActionBarActivity
public final static String EXTRA_MESSAGE = commycompanymyfirstappMESSAGE
bullFor the next activity to query the extra data you should define the key for your intents extra using a public constant Its generally a good practice to define keys for intent extras using your apps package name as a prefix This ensures the keys are unique in case your app interacts with other apps
In the sendMessage() method to finish the intent call the startActivity() method passing it theIntent object created in step
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
Note The reference to DisplayMessageActivity will raise an error if yoursquore using an IDE such as Android Studio because the class doesnrsquot exist yet Ignore the error for now yoursquoll create the class soon
The constructor used here takes two parameters
bullA Context as its first parameter (this is used because the Activity class is a subclass of Context)
bullThe Class of the app component to which the system should deliver the Intent (in this case the activity that should be started)
Android Studio indicates that you must import the Intent class
At the top of the file import the Intent classjavacommycompanymyfirstappMyActivityjava
import androidcontentIntent
Tip In Android Studio press Alt + Enter (option + return on Mac) to import missing classes
Inside the sendMessage() method use findViewById() to get the EditText element
javacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message)
Assign the text to a local message variable and use the putExtra() method to add its text value to the intentjavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view)
Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message)An Intent can carry data types as key-value pairs called extras The putExtra() method takes the key name in the first parameter and the value in the second parameter
At the top of the MyActivity class add the EXTRA_MESSAGE definition as follows
javacommycompanymyfirstappMyActivityjava
public class MyActivity extends ActionBarActivity
public final static String EXTRA_MESSAGE = commycompanymyfirstappMESSAGE
bullFor the next activity to query the extra data you should define the key for your intents extra using a public constant Its generally a good practice to define keys for intent extras using your apps package name as a prefix This ensures the keys are unique in case your app interacts with other apps
In the sendMessage() method to finish the intent call the startActivity() method passing it theIntent object created in step
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
The constructor used here takes two parameters
bullA Context as its first parameter (this is used because the Activity class is a subclass of Context)
bullThe Class of the app component to which the system should deliver the Intent (in this case the activity that should be started)
Android Studio indicates that you must import the Intent class
At the top of the file import the Intent classjavacommycompanymyfirstappMyActivityjava
import androidcontentIntent
Tip In Android Studio press Alt + Enter (option + return on Mac) to import missing classes
Inside the sendMessage() method use findViewById() to get the EditText element
javacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message)
Assign the text to a local message variable and use the putExtra() method to add its text value to the intentjavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view)
Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message)An Intent can carry data types as key-value pairs called extras The putExtra() method takes the key name in the first parameter and the value in the second parameter
At the top of the MyActivity class add the EXTRA_MESSAGE definition as follows
javacommycompanymyfirstappMyActivityjava
public class MyActivity extends ActionBarActivity
public final static String EXTRA_MESSAGE = commycompanymyfirstappMESSAGE
bullFor the next activity to query the extra data you should define the key for your intents extra using a public constant Its generally a good practice to define keys for intent extras using your apps package name as a prefix This ensures the keys are unique in case your app interacts with other apps
In the sendMessage() method to finish the intent call the startActivity() method passing it theIntent object created in step
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
At the top of the file import the Intent classjavacommycompanymyfirstappMyActivityjava
import androidcontentIntent
Tip In Android Studio press Alt + Enter (option + return on Mac) to import missing classes
Inside the sendMessage() method use findViewById() to get the EditText element
javacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message)
Assign the text to a local message variable and use the putExtra() method to add its text value to the intentjavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view)
Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message)An Intent can carry data types as key-value pairs called extras The putExtra() method takes the key name in the first parameter and the value in the second parameter
At the top of the MyActivity class add the EXTRA_MESSAGE definition as follows
javacommycompanymyfirstappMyActivityjava
public class MyActivity extends ActionBarActivity
public final static String EXTRA_MESSAGE = commycompanymyfirstappMESSAGE
bullFor the next activity to query the extra data you should define the key for your intents extra using a public constant Its generally a good practice to define keys for intent extras using your apps package name as a prefix This ensures the keys are unique in case your app interacts with other apps
In the sendMessage() method to finish the intent call the startActivity() method passing it theIntent object created in step
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
Inside the sendMessage() method use findViewById() to get the EditText element
javacommycompanymyfirstappMyActivityjava
public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message)
Assign the text to a local message variable and use the putExtra() method to add its text value to the intentjavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view)
Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message)An Intent can carry data types as key-value pairs called extras The putExtra() method takes the key name in the first parameter and the value in the second parameter
At the top of the MyActivity class add the EXTRA_MESSAGE definition as follows
javacommycompanymyfirstappMyActivityjava
public class MyActivity extends ActionBarActivity
public final static String EXTRA_MESSAGE = commycompanymyfirstappMESSAGE
bullFor the next activity to query the extra data you should define the key for your intents extra using a public constant Its generally a good practice to define keys for intent extras using your apps package name as a prefix This ensures the keys are unique in case your app interacts with other apps
In the sendMessage() method to finish the intent call the startActivity() method passing it theIntent object created in step
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
Assign the text to a local message variable and use the putExtra() method to add its text value to the intentjavacommycompanymyfirstappMyActivityjava
public void sendMessage(View view)
Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message)An Intent can carry data types as key-value pairs called extras The putExtra() method takes the key name in the first parameter and the value in the second parameter
At the top of the MyActivity class add the EXTRA_MESSAGE definition as follows
javacommycompanymyfirstappMyActivityjava
public class MyActivity extends ActionBarActivity
public final static String EXTRA_MESSAGE = commycompanymyfirstappMESSAGE
bullFor the next activity to query the extra data you should define the key for your intents extra using a public constant Its generally a good practice to define keys for intent extras using your apps package name as a prefix This ensures the keys are unique in case your app interacts with other apps
In the sendMessage() method to finish the intent call the startActivity() method passing it theIntent object created in step
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
At the top of the MyActivity class add the EXTRA_MESSAGE definition as follows
javacommycompanymyfirstappMyActivityjava
public class MyActivity extends ActionBarActivity
public final static String EXTRA_MESSAGE = commycompanymyfirstappMESSAGE
bullFor the next activity to query the extra data you should define the key for your intents extra using a public constant Its generally a good practice to define keys for intent extras using your apps package name as a prefix This ensures the keys are unique in case your app interacts with other apps
In the sendMessage() method to finish the intent call the startActivity() method passing it theIntent object created in step
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
bullFor the next activity to query the extra data you should define the key for your intents extra using a public constant Its generally a good practice to define keys for intent extras using your apps package name as a prefix This ensures the keys are unique in case your app interacts with other apps
In the sendMessage() method to finish the intent call the startActivity() method passing it theIntent object created in step
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
With this new code the complete sendMessage() method thats invoked by the Send button now looks like this
javacommycompanymyfirstappMyActivityjava Called when the user clicks the Send button public void sendMessage(View view) Intent intent = new Intent(this DisplayMessageActivityclass) EditText editText = (EditText) findViewById(Ridedit_message) String message = editTextgetText()toString() intentputExtra(EXTRA_MESSAGE message) startActivity(intent)The system receives this call and starts an instance of the Activity specified by the Intent Now you need to create the DisplayMessageActivity class in order for this to work
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
Create the Second Activitybull All subclasses of Activity must implement the onCreate() method This method is where the activity
bull receives the intent with the message then renders the message Also the onCreate() method must
bull define the activity layout with the setContentView() method This is where the activity performs the initial setup of the activity components
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
Create a new activity using Android Studio
demo
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
bullActivity Name DisplayMessageActivity
bull Layout Name activity_display_message
bullTitle My Message
bullHierarchical Parent commycompanymyfirstappMyActivity
bullPackage name commycompanymyfirstapp
In Android Studio in the java directory select the package commycompanymyfirstapp right-click and select New gt Activity gt Blank Activity
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
1Open the DisplayMessageActivityjava fileThe class already includes an implementation of the required onCreate() method You will update the implementation of this method later It also includes an implementation of onOptionsItemSelected() which handles the action bars Up behavior Keep these two methods as they are for now
2Remove the onCreateOptionsMenu() methodYou wont need it for this app
If youre developing with Android Studio you can run the app now but not much happens Clicking the Send button starts the second activity but it uses a default Hello world layout provided by the template Youll soon update the activity to instead display a custom text view
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
To your stringsxml file add the new activitys title as followsltresourcesgt ltstring name=title_activity_display_messagegtMy Messageltstringgt
ltresourcesgt
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
In your manifest file AndroidManifestxml within the Application element add the ltactivitygt element for your DisplayMessageActivity class as followsltapplication gt ltactivity androidname=commycompanymyfirstappDisplayMessageActivity androidlabel=stringtitle_activity_display_message androidparentActivityName=commycompanymyfirstappMyActivity gt ltmeta-data androidname=androidsupportPARENT_ACTIVITY androidvalue=commycompanymyfirstappMyActivity gt ltactivitygt
ltapplicationgt
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
Receive the Intent
Every Activity is invoked by an Intent regardless of how the user navigated there You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within the intent1In the javacommycompanymyfirstapp directory edit the DisplayMessageActivityjava file2In the onCreate() method remove the following line3setContentView(Rlayoutactivity_display_message)
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
1Get the intent and assign it to a local variableIntent intent = getIntent()
2At the top of the file import the Intent classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
3Extract the message delivered by MyActivity with the getStringExtra() methodString message=intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
Display the Message
1In the onCreate() method create a TextView objectTextView textView = new TextView(this)2Set the text size and message with setText()textViewsetTextSize(40)textViewsetText(message)3Then add the TextView as the root view of the activityrsquos layout by passing it to setContentView()setContentView(textView)4At the top of the file import the TextView classIn Android Studio press Alt + Enter (option + return on Mac) to import missing classes
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
The complete onCreate() method for DisplayMessageActivity now looks like thisOverridepublic void onCreate(Bundle savedInstanceState) superonCreate(savedInstanceState)
Get the message from the intent Intent intent = getIntent() String message = intentgetStringExtra(MyActivityEXTRA_MESSAGE)
Create the text view TextView textView = new TextView(this) textViewsetTextSize(40) textViewsetText(message)
Set the text view as the activity layout setContentView(textView)
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-
- Starting Another Activity Lesson 3
- Slide 2
- Respond to the Send Button
- Slide 4
- Slide 5
- Build an Intent
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Create the Second Activity
- demo
- In Android Studio in the java directory select the package
- Slide 18
- Slide 19
- Slide 20
- Receive the Intent
- Slide 22
- Display the Message
- Slide 24
- Slide 25
- Slide 26
-