0% found this document useful (0 votes)
6 views

I3350 - Lecture 4 - App Activity

android dev course 5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

I3350 - Lecture 4 - App Activity

android dev course 5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

App Activity

Mobile Application development


What is an Activity?
• An Activity is an application component
• Represents one window (one screen)
• Java class, typically one Activity in one file

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

1. Define layout in XML


2. Define Activity Java class
– extends AppCompatActivity These steps are done
3. Connect Activity with Layout automatically by Android Studio.
– Set content view in onCreate()
4. Declare Activity in the Android manifest

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

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_ma
in);
} Resource is layout in this XML file
}

7
4. Declare main activity in manifest

MainActivity needs to include intent-


filter to start from launcher
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

8
App with two activities

9
What is an intent?

An Intent is a description of an operation to be


performed.
An Intent is an object used to request an action
from another app component via the Android
system. Originator App component

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);

• Use the Intent to start the Activity


○ startActivity(intent);

14
App with two activities

Java class: MainActivity Java class: SecondActivity

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

Java class: 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?

Inside the method specified with the onClick of the button

21
3 How to use the intent?
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText_message);

String message = editText.getText().toString();


Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("message", message);
startActivity(intent);
}

22
3 How to use the intent?
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText_message);

String message = editText.getText().toString();


Intent intent = new Intent(this,
Grabbing SecondActivity.class);
the EditText Object from the layout.
1) The type is EditText
intent.putExtra("message",
2) Needs to be message);
casted
3) Use findViewById(R.id.[view_id])
startActivity(intent);“editText_message” was chosen by us in the layout xml file
4) You can do the same steps for any view in the layout
}

23
3 How to use the intent?
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText_message);

String message = editText.getText().toString();


Intent intent = new Intent(this, SecondActivity.class); Extract the value from the
EditText view using getText()
intent.putExtra("message", message); then toString() and saving it
into a new String “message”
startActivity(intent);
}

24
3 How to use the intent?
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText_message);

String message = editText.getText().toString();


Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("message", message); • Creating the intent and
setting it to the class
startActivity(intent); name responsible of the
second activity we have
} created
• ‘this’ is the context of
MainActivity

25
3 How to use the intent?
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText_message);

String message = editText.getText().toString();


Intent intent = new Intent(this, SecondActivity.class);
Use
intent.putExtra("message", message); putExtra([user-chosen-
name], [value])
startActivity(intent); to fill the intent object with
data
}

26
3 How to use the intent?
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText_message);

String message = editText.getText().toString();


Intent intent = new Intent(this, SecondActivity.class);
Start/Call the second activity.
intent.putExtra("message", message);
This will make your phone
switch to another screen
startActivity(intent); view (activity) showing the
layout of the second activity.
}

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.

• But how does the second activity recieves this value?

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");

// Get the message from the intent


Intent intent = getIntent();
String message = intent.getStringExtra("message");

// Setting the value of the TextView


TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(message);
}
}

30

You might also like