Lab 4
Lab 4
Lab Journal: 4
The View objects are usually called "widgets" and can be one of many subclasses, such as
Button or TextView. The ViewGroup objects are usually called "layouts" can be one of many
types that provide a different layout structure, such as LinearLayout or ConstraintLayout .
Activity:
The Activity class is a crucial component of an Android app, and the way activities are launched
and put together is a fundamental part of the platform's application model. Unlike programming
paradigms in which apps are launched with a main() method, the Android system initiates code
in an Activity instance by invoking specific callback methods that correspond to specific stages
of its lifecycle shown in given figure.
Task No.1: Implement All activities callback functions and print to log when they are
called.
Coding:
package com.example.lab04;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import
android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
}
@Override protected void
onStart() { super.onStart();
Log.d("MainActivity", "OnStart Called");
}
@Override protected void
onPause() {
super.onPause();
Log.d("MainActivity", "OnPause Called");
}
@Override protected void
onResume() {
super.onResume();
Log.d("Main Activity", "OnResume Called");
}
@Override protected void
onStop() { super.onStop();
Log.d("MainActivity", "OnStop Called");
}
@Override protected void
onRestart() {
super.onRestart();
Log.d("MainActivity", "OnRestart Called");
}
@Override protected void
onDestroy() {
super.onDestroy();
Log.d("MainActivity", "OnDestroy Called");
}}
Output:
Task No.2: Create the following layout using Linear and Constraint Layout.
Coding:
XML File:
<?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=".MainActivity"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="170dp"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My Android Application"
android:layout_marginRight="100dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="130dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp">
<Button
android:id="@+id/button12"
android:layout_width="153dp"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:text="Hello" />
<Button
android:id="@+id/button13"
android:layout_width="141dp"
android:layout_height="wrap_content"
android:text="World" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="280dp"
android:layout_marginLeft="150dp"
android:text="Made by:" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginLeft="130dp"
android:text="M Faisal" />
</LinearLayout>
</LinearLayout>
Output:
Conclusion:
In this lab we learned how to implement the concepts of layouts and activities. We used these
concepts in the given tasks and completed them successfully.