I3350 - Lecture 4 - App Activity
I3350 - Lecture 4 - App Activity
2
Examples of activities
3
Apps and activities
• Activities are loosely tied together to make up an app
• First Activity user sees is typically called "main activity"
• An Activity typically has a UI layout
• Layout is usually defined in one or more XML files
• Activity "inflates" layout as part of being created
4
Create new activities
5
1.Define layout in XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Let's Shop for Food!" />
</LinearLayout>
6
3. Connect activity with layout
setContentView(R.layout.activity_ma
in);
} Resource is layout in this XML file
}
7
4. Declare main activity in manifest
8
App with two activities
9
What is an intent?
Intent Action
Android
System
10
What can intents do?
• Start an Activity
– A button click starts a new Activity for text entry
– Clicking Share opens an app that allows you to post a photo
• Start a Service
– Initiate downloading a file in the background
• Deliver a Broadcast
– The system informs everybody that the phone is now charging
11
What can intents do?
• Start an Activity
– A button click starts a new Activity for text entry
– Clicking Share opens an app that allows you to post a photo This will be
covered
• Start a Service
– Initiate downloading a file in the background
• Deliver a Broadcast
– The system informs everybody that the phone is now charging
12
Intent to start an Activity
Explicit Intent
● Starts a specific Activity
Implicit Intent
● Asks system to find an Activity that can handle this request
13
Start an Activity with an explicit
intent
To start a specific Activity, use an explicit
Intent
• Create an Intent
○ Intent intent = new Intent(this, ActivityName.class);
14
App with two activities
15
MainActivity
1
<EditText
android:id="@+id/editText_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="2"
android:hint="Enter your message" />
<Button
android:layout_margin="8dp"
android:layout_weight="1"
android:onClick="sendMessage"
android:text="Send" />
TableLayout is used
The views are placed in one TableRow
The EditText occupies 2/3rd of the row width (weight=2) and the
Java class: MainActivity Button occupies 1/3rd (weight=1)
16
activity_main.xml
1
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TableRow>
<EditText
android:id="@+id/editText_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="2"
android:hint="Enter your message" />
<Button
android:layout_margin="8dp"
android:layout_weight="1"
android:onClick="sendMessage"
android:text="Send" />
</TableRow>
</TableLayout>
17
SecondActivity
2
• To create the second activity:
– Right click on Java folder -> New -> Activity ->
EmptyActivity
– Name it SecondActivity
18
SecondActivity
2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:textSize="40sp"
android:gravity="center" />
</LinearLayout>
Java class: SecondActivity
19
Navigation using Intent object
3
• When the user clicks on SEND
– An explicit intent will be created and
used to start the second activity
– The intent will also be used to send the
text entered in the EditText view to the
second activity
20
3 How to use the intent?
We need to:
- Create the intent object and set it to the
second activity
- Use the “extras” to put in the value/text
entered by the user in the EditText field
- Start the second activity
- All of the above should be placed where?
21
3 How to use the intent?
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText_message);
22
3 How to use the intent?
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText_message);
23
3 How to use the intent?
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText_message);
24
3 How to use the intent?
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText_message);
25
3 How to use the intent?
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText_message);
26
3 How to use the intent?
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText_message);
27
Receiving the intent
4
• The main activity coding is done.
• Pressing the button will actually start the second activity
• The value of the string entered by the user is sent to it.
28
Receiving the intent
4
• Since the value was sent in an intent object,
we also receive it from that same intent
object.
• Steps:
– Grabbing the intent
– Extracting the extra from it
– Setting the value to
In which the
part ofTextView
the code
should all of this be placed?
view.
29
Receiving the intent
4
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
getSupportActionBar().setTitle("Second Activity");
30