[Android] Concepts

Activities, Packages, and Layouts

Android Components:

  • Activity
  • Service
  • Content Provider
  • Broadcast Receiver

Android app knows these components because they’re registered with Android manifest.

Activity

Activities are responsible for most app interactions. An activity is a single focused thing that the user can do. Activities are responsible for creating the window that your application uses to draw and receive events from the system. Activities are written in Java, extending from the Activity class.

An activity creates views to show the user information, and to let the user interact with the activity. Views are a class in the Android UI framework. They occupy a rectangular area on the screen and are responsible for drawing and handling events. An activity determines what views to create (and where to put them), by reading an XML layout file. These XML files are stored in the res folder inside the folder labeled layout.

Example XML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context="com.example.android.exampleapp.MainActivity">

<EditText
android:id="@+id/edit_text_name_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:hint="Enter your name"
android:padding="4dp"
android:textSize="24sp" />

<TextView
android:id="@+id/text_view_name_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:text="Your name appears here"
android:textSize="30sp" />
</LinearLayout>

Type of View: UI Component

UI Components are first type of views, and they are often interactive.

Examples:

Class Name Description
TextView Creates text on the screen; generally non interactive text
EditText Creates a text input on the screen
ImageView Creates an image on the screen
Button Creates a button on the screen
Chronometer Creates a simple timer on screen

The android.widget package contains a list of most of the UI view classes available.

Type of View: Container View

The second are views called “Layout” or “Container” views. They extend from a class called ViewGroup. They are primarily responsible for containing a group of views and determining where they are on screen.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.exampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="A"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="B"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="C"
android:textSize="30sp" />
</LinearLayout>
Class Name Description
LinearLayout Displays views in a single column or row.
RelativeLayout Displays views positioned relative to each other and this view.
FrameLayout A ViewGroup meant to contain a single child view.
ScrollView A FrameLayout that is designed to let the user scroll through the content in the view.
ConstraintLayout This is a newer viewgroup; it positions views in a flexible way.

XML Attributes

Views have attributes in XML which control the properties of the view.

Text

1
android:text="Some text here"

Width and Height

Some of the most important properties are the width property and height property - those must be defined for every view. Remember that all views occupy a rectangular area on the screen - the width and height are the width and height of that area. You can define this in pixels, or better yet dp (stands for density-independent pixels)

1
2
android:layout_width="200dp"
android:layout_height="300dp"

To make layout responsive and adjust to different devices:

1
2
android:layout_width="wrap_content"
android:layout_height="match_parent"

wrap_content will shrink the view to wrap whatever is displayed inside the view.

match_parent will expand the size of the view to be as large as the parent view which it is nested inside of.

How do the XML Layouts relate to the Java Activities?

To associate XML Layout with activity: pass a reference to the layout file as R.layout.name_of_layout.

For example, if layout were named as activity_main.xml this would look like:

1
2
3
4
5
6
7
8
9
10
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// other code to setup the activity
}
// other code
}
}

The R class

When your application is compiled the R class is generated. It creates constants that allow you to dynamically identify the various contents of the res folder, including layouts.

setContentView

setContentView inflates the layout. Essentially what happens is that Android reads your XML file and generates Java objects for each of the tags in your layout file. You can then edit these objects in the Java code by calling methods on the Java objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// MainActivity.java

public class MainActivity extends AppCompatActivity {
private Button hiButton;

@Override
protected void onCreate(bundle saveInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_layout);

hiButton = (Button) findViewById(R.id.hiButton);
hiButton.setText("New Text"); // Dynamically change xml elements
hiButton.setTextColor(Color.BLUE);
}
}

String.xml

  • Restores global string names
1
2
3
<resources>
<string name="button_name">Test Button Name</string>
</resources>
1
2
// Activity.java
hiButton.setText(R.string.button_name);

Manifest.xml

  • Basic app configs setup
  • Register activity to xml
  • Permission requests