PRACTICAL 1 Topic: Introduction to Android, Introduction to Android Studio IDE, Application Fundamentals: Creating a Project, Android Components, Activities, Services, Content Providers, Broadcast Receivers, Interface overview, Creating Android Virtual device, USB debugging mode, Android Application Overview. Simple “Hello World” program. Creating a project: MainActivity.java package com.example.hello; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout. activity_main ); } } activity_main.xml <? xml version="1.0" encoding="utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout 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" > < TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </ androidx.constraintlayout.widget.ConstraintLayout > Application in AVD: Create and manage virtual devices: To open the AVD Manager, do one of the following: Select Tools > AVD Manager. Click AVD Manager AVD Manager icon in the toolbar. Enable USB debugging The very first step is to enable USB debugging on your Android device. To do this follow these steps On your phone (or tablet) go to Settings=> About Phone Tap Build Number 7 times, after 7th time it will say You are now a developer You will notice Developer’s Options are now available. Go to the Developer option and enable USB debugging Install USB driver Next step is to install USB driver for your Android device. For this follow instructions from your device manufacturer. For example, I am using Android smartphone of Huawei, so I just downloaded Huawei USB driver from their official website. If your device uses Google USB driver you can download from this link http://developer.android.com/sdk/win-usb.html. After installation you need to update it. Make sure your device is connected through a USB cable. Go to the Control Panel => Device Manager then locate and right click your Android device and click Update driver software. Note: Make sure your Android device is not sleeping while connected through USB cable. Step 3: Run your app No you can run your Android app. Right click on the app and click Run . Or simply select run option from the tool bar menu shown below. Android Studio screen A window Select Deployment Target will appear, and a list of available devices will appear. Choose your device and click OK . Android Studio will run your application in your Android device. Deployment target Click on “ Use same selection for future launches ” if you want to save this setting for later apps. PRACTICAL 2 Types of Resources The following are the most common types of resources within Android apps: In addition, note the following key files stored within the values folder mentioned above: Example Demonstration the use of various resource files. Note: In Drawable folder, add the image that will be displayed on the click of the button. Path for Drawable folder is: C:\Users\Administrator\AndroidStudioProjects\ <ProjectName> \app\src\ main\res\drawable In this example the image used is screenshot.png Note: Image name should be in lowercase and should NOT contain any special characters, uppercase letters and digits. colors.xml <? xml version="1.0" encoding="utf-8" ?> < resources > < color name="colorPrimary" >#008577</ color > < color name="colorPrimaryDark" >#00574B</ color > < color name="colorAccent" >#D81B60</ color > < color name="btn_color" >#FFFF51</ color > < color name="btn2_color" >#FAAF00</ color > </ resources > Android Resources: (Color, String, Drawable) strings.xml < resources > < string name="app_name" >MyBackColor</ string > < string name="txt_text" >Change Background color and image</ string > < string name="btn_text" >Show Image</ string > < string name="btn2_text" >Show BackColor</ string > < string name="img_content" >Image is displayed on button click</ string > </ resources > Activity_main.xml <? xml version="1.0" encoding="utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/constraintLayout1" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > < TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/txt_text" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.075" /> < ImageView android:id="@+id/imageView" android:layout_width="244dp" android:layout_height="237dp" android:layout_marginTop="84dp" android:contentDescription="@string/img_content" app:layout_constraintStart_toStartOf="@+id/textView" app:layout_constraintTop_toBottomOf="@+id/textView" /> < Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="92dp" android:background="@color/btn_color" android:onClick="showPicture" android:text="@string/btn_text" app:layout_constraintStart_toStartOf="@+id/imageView" app:layout_constraintTop_toBottomOf="@+id/imageView" /> < Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="48dp" android:layout_marginLeft="48dp" android:layout_marginTop="92dp" android:background="@color/btn2_color" android:onClick="showBColor" android:text="@string/btn2_text" app:layout_constraintStart_toEndOf="@+id/button" app:layout_constraintTop_toBottomOf="@+id/imageView" /> </ androidx.constraintlayout.widget.ConstraintLayout > MainAcitivity.java package com.example.mybackcolor; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout. activity_main ); } ConstraintLayout ctl ; ImageView img ; public void showBColor(View view) { ctl =(ConstraintLayout)findViewById(R.id. constraintLayout1 ); ctl .setBackgroundColor(Color. CYAN ); } public void showPicture(View view) { img =(ImageView)findViewById(R.id. imageView ); img .setImageResource(R.drawable. screenshot ); } } In Drawable folder add the image to display: OUTPUT: PRACTICAL 3 Activity Life Cycle, Activity methods, Multiple Activities, Life Cycle of fragments and multiple fragments. Activity Lifecycle: onCreate(): Called by the OS when the activity is first created. This is where you initialize any UI elements or data objects. You also have the savedInstanceState of the activity that contains its previously saved state, and you can use it to recreate that state. onStart(): Just before presenting the user with an activity, this method is called. It’s always followed by onResume(). In here, you generally should start UI animations, audio based content or anything else that requires the activity’s contents to be on scre en. onResume(): As an activity enters the foreground, this method is called. Here you have a good place to restart animations, update UI elements, restart camera previews, resume audio/video playback or initialize any components that you release during onPause(). onPause(): This method is called before sliding into the background. Here you should stop any visuals or audio associated with the activity such as UI animations, music playback or the camera. This method is followed by onResume() if the activity returns to the foreground or by onStop() if it becomes hidden. onStop(): This method is called right after onPause(), when the activity is no longer visible to the user, and it’s a good place to save data that you want to commit to the disk. It’s followed b y either onRestart(), if this activity is coming back to the foreground, or onDestroy() if it’s being released from memory. onRestart(): Called after stopping an activity, but just before starting it again. It’s always followed by onStart(). onDestroy(): T his is the final callback you’ll receive from the OS before the activity is destroyed. You can trigger an activity’s desctruction by calling finish(), or it can be triggered by the system when the system needs to recoup memory. If your activity includes any background threads or other long-running resources, destruction could lead to a memory leak if they’re not released, so you need to remember to stop these processes here as well. Activity Life Cycle and Programming Activities Example of activity lifecycle and multiple activities: activity_main.xml <? xml version="1.0" encoding="utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout 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" > < TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Activity LifeCycle Demo" android:textSize="24sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.467" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.11" /> < Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="196dp" android:onClick="goToActivity2" android:text="Goto Activity 2" app:layout_constraintStart_toStartOf="@+id/textView" app:layout_constraintTop_toBottomOf="@+id/textView" /> </ androidx.constraintlayout.widget.ConstraintLayout > MainActivity.java package com.example.activitylifecycle; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout. activity_main ); Log. i ( "CREATE" , "Activity created" ); } protected void onStart() { super .onStart(); Log. i ( "START" , "Activity started" ); } protected void onStop() { super .onStop(); Log. i ( "STOP" , "Activity stopped" ); } protected void onRestart() { super .onRestart(); Log. i ( "RESTART" , "Activity restarted" ); } protected void onResume() { super .onResume(); Log. i ( "RESUME" , "Activity resumed" ); } protected void onPause() { super .onPause(); Log. i ( "PAUSE" , "Activity paused" ); } protected void onDestroy() { super .onDestroy(); Log. i ( "DESTROY" , "Activity destroyed" ); } public void goToActivity2(View view) { Intent g1= new Intent( this ,SecondActivity. class ); startActivity(g1); } } activity_second.xml <? xml version="1.0" encoding="utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > < TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="100dp" android:layout_marginLeft="100dp" android:layout_marginTop="148dp" android:text="Welcome to Activity 2" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> < Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="140dp" android:onClick="goToActivity3" android:text="Goto Activity 3" app:layout_constraintStart_toStartOf="@+id/textView2" app:layout_constraintTop_toBottomOf="@+id/textView2" /> </ androidx.constraintlayout.widget.ConstraintLayout > SecondActivity.java package com.example.activitylifecycle; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; public class SecondActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout. activity_second ); } public void goToActivity3(View view) { Intent g2= new Intent( this ,ThirdActivity. class ); startActivity(g2); } } activity_third.xml <? xml version="1.0" encoding="utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > < TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="100dp" android:layout_marginLeft="100dp" android:layout_marginTop="132dp" android:text="Welcome to Activity 3" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> < Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="112dp" android:onClick="goToMainActivity" android:text="Goto Main Activity" app:layout_constraintStart_toStartOf="@+id/textView3" app:layout_constraintTop_toBottomOf="@+id/textView3" /> </ androidx.constraintlayout.widget.ConstraintLayout > ThirdActivity.java package com.example.activitylifecycle; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; public class ThirdActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout. activity_third ); } public void goToMainActivity(View view) { Intent g2= new Intent( this ,MainActivity. class ); startActivity(g2); } } NOTE: Register the two activities’ java class files in AndroidManifest.xml as: < activity android:name=".SecondActivity" ></ activity > < activity android:name=".ThirdActivity" ></ activity > Create the java class files before registering in manifest file. For adding multiple activities: Right click on layout New Layout Resource File For adding multiple java class files: Right click on com.example.<projectname> New Java Class For opening logcat: Select View menu Tools Windows Logcat (Alt+6) Creating multiple java files and activity files: Output for Multiple Activities: Output for Activity Life Cycle in Logcat Run: