0% found this document useful (0 votes)
45 views35 pages

Index: SR No. Name of The Program

Uploaded by

prince arora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views35 pages

Index: SR No. Name of The Program

Uploaded by

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

INDEX

Sr No. Name of the program


1. Installing Java and setup of Jdk.

2. System requirement for Android Studio.

3. Download and Install Android Studio (Sdk).

4. Creating Android Virtual Device

5. Creating a New Project in Android Studio.

6. To change Icon Image of Android application.

7. Run and Stop application in Android application.

8. To design an Application using Constraint Layout.

9. To design an Application using Linear Layout.

10. To design an Application using Relative Layout.

11. To Running an Android Java app on a physical device(your phone).

12. To illustrate various android activities.

13. To design an android application to show your own name.


Experiment 1: Installing Java and setup of Jdk

Step 1: Download the Java Development Kit (JDK)

1. Go to the Official Oracle Website:


o Visit the Oracle JDK download page.
o Alternatively, you can use OpenJDK, an open-source
implementation of Java, available from the OpenJDK website.

2. Choose the JDK Version:


o Select the version of Java you want to install (e.g., Java SE 17,
Java SE 11, etc.).
o Click on the appropriate version based on your operating
system (Windows, macOS, Linux).

3. Download the Installer:


o For Windows: Download the .exe file.
o For macOS: Download the .dmg file.
o For Linux: Download the appropriate package (e.g., .tar.gz
for manual installation, or use your package manager).

Step 2: Install the JDK

Windows

1. Run the Installer:


o Locate the downloaded .exe file and double-click it to start
the installation process

2. Follow the Installation Wizard:


o Click "Next" to precede through the setup steps.
o Choose the installation directory (you can leave it as the
default unless you have a specific preference).
o Click "Install or Next" to begin the installation

3. Complete the Installation:


o Once the installation is complete, click "Close."

macOS

1. Open the DMG File:


o Double-click the downloaded .dmg file to mount it.
2. Run the Installer:
o Double-click the .png file inside the mounted image to start
the installation.
3. Follow the Installation Instructions:
o Proceed through the installation prompts by clicking
"Continue" and "Install."
4. Complete the Installation:
o Enter your macOS administrator password when prompted.
o Once the installation is complete, close the installer.

Linux

1. Install via Package Manager (for Debian-based systems like Ubuntu):


o Open a terminal and run:
o Replace 17 with the desired version number.
2. Manual Installation:
o Update your PATH environment variable to include the
bin directory of the JDK:
o Update the JAVA_HOME environment variable:
o These changes can be added to your .bashrc or .bash_profile
for persistence.

Step 3: Set Up Environment Variables (Windows Only)

1. Open System Properties:


o Right-click on "This PC" or "My Computer" on your desktop or
in File Explorer.
o Click on "Properties" > "Advanced system settings"
> "Environment Variables."

2. Set the JAVA_HOME Variable:


o In the "System variables" section, click "New."
o Set the variable name as JAVA_HOME.
o Set the variable value to the path where the JDK is installed
(e.g., C:\Program Files\Java\jdk-21
3. Update the PATH Variable:
o In the "System variables" section, find the Path variable and
select it, then click "Edit."
o Add a new entry with the path to the JDK's bin directory (e.g.,
C:\Program Files\Java\jdk-21\bin).
4. Apply Changes:

o Click "OK" to close all windows and apply the changes.

Step 4: Verify the Installation

1. Open a Command Prompt (Windows) or Terminal (macOS/Linux).


2. Check the Java Version:
o Type the following command and press Enter:
o You should see output showing the installed version of Java.
3. Check the JDK Installation:
o To verify the JDK, run:
o This should display the version of the Java compiler.

If everything is set up correctly, Java is now installed and ready to use on your system.
Experiment 2: System Requirement for
Android Studio

Minimum System Requirements for Android Studio

Windows

o Operating System: Windows 8/10/11 (64-bit)


o Processor: Intel or AMD processor with support for 64-bit (x86_64) architecture
o RAM: 8 GB minimum, 16 GB recommended
o Storage: At least 8 GB of available disk space, 16 GB recommended (plus 1 GB for
the Android SDK, emulator system images, and caches)
o Screen Resolution: 1280 x 800 minimum
o Graphics: Dedicated GPU recommended for hardware acceleration (Intel HD
Graphics 4000 or higher, or Nvidia GeForce 820M or higher)

macOS

o Operating System: macOS 10.14 (Mojave) or higher


o Processor: Apple Silicon (M1 or M2) or Intel processor with support for 64-bit
(x86_64) architecture
o RAM: 8 GB minimum, 16 GB recommended
o Storage: At least 8 GB of available disk space, 16 GB recommended
o Screen Resolution: 1280 x 800 minimum
o Graphics: GPU that supports OpenGL ES 2.0 or higher

Linux

o Operating System: GNU/Linux with a 64-bit distribution, Debian, Ubuntu, Fedora,


or similar
o Processor: Intel or AMD processor with support for 64-bit (x86_64) architecture
o RAM: 8 GB minimum, 16 GB recommended
o Storage: At least 8 GB of available disk space, 16 GB recommended
o Screen Resolution: 1280 x 800 minimum
o Graphics: GPU that supports OpenGL ES 2.0 or higher

Recommended Specifications for Optimal Performance

o RAM: 16 GB or more for better performance, especially when running


multiple emulators or other intensive tasks.
o SSD Storage: SSDs are highly recommended for faster performance, especially for
disk I/O-intensive tasks such as building projects and running emulators.
o Graphics: A dedicated GPU will significantly improve the performance of the
Android Emulator and any graphics-intensive tasks.
Experiment 3: Download and Install Android
Studio(Sdk)
The Android SDK (Software Development Kit) is a collection of tools, libraries, and resources
that developers use to create applications for the Android operating system. It provides
everything needed to develop, compile, test, and debug Android apps, making it a crucial
component for Android app development.

Step 1: System Requirements

Make sure your system meets the following requirements:

o Windows: 64-bit Windows 8/10/11


o macOS: macOS 10.14 (Mojave) or higher, x86_64 or ARM-based.
o Linux: 64-bit Ubuntu 18.04+, KDE, Gnome desktop.

Step 2: Download Android Studio

1. Visit Android Studio's official website:


o Go to the official Android Studio download
page: https://developer.android.com/studio

2. Download the correct version:


o Select the download button for your operating system (Windows, macOS, Linux).
o Agree to the Terms and Conditions.
o The download will start automatically.

Step 3: Locate the file

Locate the (.exe file) in your Downloads folder in pc and then double click on the file to
begin the installation of Android Studio.

For Windows:

1. Run the installer (.exe file) and click on “Next” to precede the installation.

2. Select components:
o In the setup wizard, select components to install. By default, Android Studio,
the Android SDK, the Android Virtual Device (AVD), and the Android Emulator
are
selected.
3. Choose SDK installation location:
o Choose where to install the SDK and Android Studio (you can use the
default location).
4. Install:
o Click “Next” to complete the installation and wait for the process to finish.

For macOS:

1. Open the DMG file you downloaded.


2. Drag Android Studio into Applications:
o Drag the Android Studio icon into the “Applications” folder.
3. Open Android Studio:
o Go to Applications and double-click Android Studio to open it.
4. Follow the Setup Wizard to configure and install required components.

For Linux:

1. Extract the tar.gz file:


o Extract the downloaded .tar.gz file to a location like /usr/local/.
2. Move the folder:
o Use the terminal to navigate to the extracted folder.
3. Follow the setup wizard.

Step 4: First-Time Setup in Android Studio

1. Start Android Studio:


o When you run Android Studio for the first time, the “Android Studio Setup
Wizard” will guide you through the setup process and you will see the
Welcome page.

2. Download Android SDK:


o Android Studio will automatically download the required Android SDK. Make
sure to select the SDK versions you’ll need based on the devices and Android
versions you’re developing for.

3. Choose the install type:


o You will be asked if you want to do a standard or custom installation.
o Standard Installation is recommended for most users, as it installs the
default SDK and tools.

4. Accept the License Agreement for SDK tools :


o Accept the Agreement and you need to click on intel-android extra license
as shown in image.
o Then after the SDK components are download you can click on finish. Then
you will see the Android Studio Home page.

Step 5: Create an Android Virtual Device

After the SDK components are downloaded you can see the Android Studio Home page. You
first have to create a Virtual Device to for your New project or app to Run.

o So now on Android Studio Home page you can click on the More Actions and then click
on the Virtual Device Manager
o Now in the Virtual device manager you have to create a virtual device so click on
“Create a Virtual Device”.
o After clicking on it you have to select the Hardware and which device (Phone, tablet,
Wear OS etc.) You like and then if you has selected Phone and selected the Phone
model also, then click on “Next”.
o After this you have to select an System Image, You can go for the Recommended
System Image and as their name is showing below you can chose recommended (API
35) and then click on it to begin the downloading after the download is complete then
click on finish (if already downloaded then click on Finish),
o You can also change your AVD name if you want to then, click on finish. Your
Virtual Device has been created.

Step 6: Create a new project:

o Go to File > New Project and follow the steps to create a simple “Hello
World” app.
2. Run the app:
o Click the Run button, select your emulator or physical device (if connected),
and ensure that the app runs successfully.

Step 7: (Optional) Install Device Drivers for Physical Testing (Windows only)

If you want to test your apps on a physical Android device:

1. Enable Developer Mode on your device:


o Go to Settings > About Phone and tap Build Number seven times to
enable Developer Options.
2. Enable USB Debugging:
o Go to Settings > Developer Options and enable USB Debugging.
3. Install device drivers:
o For Windows, you may need to install OEM drivers for your Android device.
You can find the drivers from the manufacturer's website (e.g., Samsung,
Xiaomi, Google Pixel).
Experiment 4: Creating android virtual
devices

To create a virtual Device follows these steps below:

Step 1: Open the AVD Manager

1. Launch Android Studio: Start Android Studio on your computer.


2. Access AVD Manager:
o In the top-right corner of Android Studio, click the "AVD Manager" icon (a
smartphone with an Android logo).
o Alternatively, go to Tools > Virtual Device Manager in the menu.

Step 2: Create a New Virtual Device

1. Click "Create Virtual Device": In the AVD Manager window, click the
"Create Virtual Device" button.

2. Choose Hardware:
o You'll see a list of available device profiles (e.g., Pixel 4, Nexus 5X,
Tablet). Choose the device profile that matches the type of device you
want to emulate. Click "Next" after selecting the device.

Step 3: Select a System Image

1. System Image Selection:


o The next screen will show you a list of Android versions and system
images. You can choose a version that matches your target SDK (e.g.,
Android 12, Android 13).
o If the system image is not already downloaded, click the "Download" link
next to the desired version of system image and click "Next".

Step 4: Configure AVD

1. AVD Configuration:
o Here, you can customize the AVD's settings, such as the device name,
orientation, and startup size (portrait or landscape).
o You can also manage advanced settings like the amount of RAM, VM heap, and
internal storage.
o If you want to enable Hardware Accelerated Performance, ensure
that "Emulated Performance" is set to "Hardware - GLES 2.0".

2. Graphics: Choose the graphics setting that suits your machine:


o Automatic: Android Studio will decide the best option based on your hardware.
o Hardware: Use the host GPU for better performance.
o Software: Emulate the GPU in software, which is slower but more compatible.

3. Click "Finish": Once you're satisfied with the configuration, click "Finish" to create the
AVD.

Step 5: Launch the Virtual Device

1. Start the AVD:


o In the AVD Manager, you'll see your newly created AVD listed.
o Click the "Play" button (a green triangle) next to the AVD to launch it.
o The Android Emulator will start, and the virtual device will boot up,
allowing you to interact with it like a physical device

Step 6: Use the Virtual Device for Testing

1. Run Applications:
o You can now run your Android applications on this virtual device
directly from Android Studio.
o Select the virtual device from the "Select Deployment Target"
dialog when you run your app.
Experiment 5: Creating a new project in
Android Studio
Creating a new Android project is straightforward using Android Studio, the official Integrated
Development Environment (IDE) for Android development. Here's a step-by-step guide:

Step 1: Install Android Studio

If you haven't already installed Android Studio, download it from the [official Android Studio
website](https://developer.android.com/studio) and follow the installation instructions for
your operating system.

Step 2: Launch Android Studio

Open Android Studio: After installation, launch Android Studio. The first time you open it, you
may need to complete some initial setup, such as installing necessary components and setting
up an Android SDK.

Step 3: Create a New Project

1. Start a New Project:

o On the "Welcome to Android Studio" screen, click "New Project".


o If you already have an open project, you can select File > New > New Project from the
menu.

2. Choose a Project Template:

o Android Studio offers various templates to start your project, such as "Empty Activity",
"Basic Activity," "Bottom Navigation Activity," etc.
o For a simple starting point, select "Empty Activity" and click "Next".

Step 4: Configure Your Project

1. Name Your Project:

o Enter a “Name” for your project (e.g., "MyFirstApp")


o Specify the Package name (e.g., ‘com.example.myfirstapp’). The package name is a
unique identifier for your app in the Google Play Store.

2. Set the Language:

Choose the programming language you want to use: as recommended “Java” or “Kotlin”.
3. Minimum SDK:

o Select the Minimum API level your app will support. This determines the lowest version
of Android that your app can run on.
o A lower API level means your app will run on more devices, but you may have to do
more work to support older Android versions. Click "Finish" to create the project .

Step 5: Run Your Project

1. Connect a Device or Emulator:

Connect an Android device to your computer via USB, or set up an Android Emulator within
Android Studio to run your app virtually.

2. Run the App:

Click the “Run” ▷ button (green triangle) in the toolbar, or press Shift + F10.Choose your
connected device or emulator from the list.
Android Studio will build your project, install the app on the selected device or emulator, and
launch it.

3. View Your App:


The app should now be running on your device or emulator, displaying the default UI you set up
in `activity_main.xml`.

Step 6: Continue Developing

Add More Activities: You can add more activities, fragments, and features as your app grows.

Test Your App: Use the built-in testing tools in Android Studio to ensure your app works as
expected across different devices and scenarios.

Deploy: When your app is ready, you can prepare it for release and upload it to the Google Play
Store
Experiment 6: To change icon image of
Android application
To change the app icon in an Android app using a vector image, you can do this through
Android Studio’s Image Asset tool, which handles vector images (SVG or XML format). This will
automatically generate the necessary draw able files from the vector image and place them in
the right locations.

Here’s a step-by-step guide to change the icon:

Steps to Change the App Icon with a Vector Image

Step 1: Open Image Asset Tool

1. Right-click on the res folder in your project.


2. Select New > Image Asset.

Step 2: Configure Image Asset

1. Icon Type: Select Launcher Icons (Adaptive and Legacy) from the dropdown menu.
2. Asset Type:
o Select Clip Art if you want to use Android Studio's built-in vector assets.
o If you have your own vector image (SVG or XML), select Local file (SVG, PSD).
3. Path:
o For custom vector images, click on Browse and select the SVG/XML vector
image from your file system.

Step 3: Customize Icon

1. Background Layer:
o Set up the background color or choose none if you want a
transparent background for your icon.
2. Foreground Layer:
o Customize the foreground scale or position of the vector image if needed

Step 4: Generate Icons

1. Preview: In the Image Asset tool, preview how the icon will look in different sizes (MDPI,
HDPI, etc.).
2. Destination Directory: Ensure the destination is set to the mipmap directory (this is
where app launcher icons are stored).
3. Click Next and then Finish to generate the icons for all densities.
Step 5: Build and Run the App

1. Rebuild your project (from Build > Rebuild Project).


2. Run your app to see the updated app icon.
Experiment 7: Run and Stop application in
Android Application
Running and stopping an application in Android Studio is an essential part of the development
process. Here’s a detailed guide to help you through each step.

Running an Android Application

Step 1: Open Your Project in Android Studio

o Launch Android Studio.


o Open the project you want to run.

Step 2: Set Up an Emulator or Physical Device

o Using an Emulator:
o Go to Tools > AVD Manager.
o If you don’t have an emulator, click Create Virtual Device and follow the steps
to configure it (choose a device type, system image, etc.).
o Once set up, click the Play button (▶) to start the emulator.
o Using a Physical Device:
o Connect your Android device to your computer via USB.
o Enable USB Debugging on your device:

 Go to Settings > About Phone > tap on Build Number 7 times to enable
Developer Options.
 Go back to Settings > Developer Options and enable USB Debugging.

o Ensure your device is detected by Android Studio.

Step 3: Select the Run Configuration

o In the top toolbar, locate the Run configuration dropdown (next to the green
Play button).
o Ensure that your app module is selected (e.g., app).

Step 4: Run the Application

o Click the Run button (▶) or press Shift + F10.


o Choose your emulator or connected device from the dialog that appears.
o Click OK to deploy the app. Android Studio will build the project and install the app
on the selected device.
Step 5: View the App Running

o Once the app is installed, it should launch automatically.


o If not, find the app icon on your emulator or device and tap to open it.

Stopping an Android Application

Step 1: Stop the Application from Android Studio

o While your app is running, look at the Run window at the bottom of Android Studio.
o Click the Stop button (■) in the toolbar of the Run window. This will terminate the
app on the emulator or device.

Step 2: Stop the Application on the Device

 Emulator:

o Navigate to the home screen of the emulator.


o Open the Recent Apps (usually a square or the multitasking button).
o Swipe the app away to close it or tap on Close.

 Physical Device:

o Tap the Recent Apps button on your device.


o Swipe the app away or tap Close to stop the application.

Debugging and Logs (Optional)

While the application is running, you can view logs to help with debugging:

o Open the Logcat window by going to View > Tool Windows > Logcat.
o You can filter logs based on tags, levels, or search specific messages.
Experiment 8: To design an Application using
Constraint Layout
A ConstraintLayout is a ViewGroup that allows you to position and size widgets in a flexible way.
ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no
nested view groups).

ConstraintLayout vs. other layouts:


ConstraintLayout can satisfy the requirement of both RelativeLayout and LinearLayout: Set
relationships between sibling views and the parent layout (like RelativeLayout) and set
weights for dynamic UI (which was only possible in LinearLayout). Even though it’s awesome,
it fails to serve the purpose with simple UI layouts.

Something that you might know about ConstraintLayout


It is easier to use with Android Studio's Layout Editor. ConstraintLayout is mainly designed for
the new programmers so that they can easily design the layout using the Visual Editor instead of
building the layout by hand via XML
No nested view groups require for complex UI.

Step 1: Create a New Project


To create a new project in Android Studio please select the Java as the programming
language.

Step 2: Open Your Layout XML File

1. After creating your project, navigate to the res/layout folder in the project view.
2. Open the activity_main.xml file. By default, you’ll have a ConstraintLayout if
you selected an empty activity. If not, change the root layout to ConstraintLayout.

1. <androidx.constraintlayout.widget.ConstraintLayout
2. xmlns:android="http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. android:layout_width="match_parent"
5. android:layout_height="match_parent">
6.
7. <!-- Add widgets here -->
8.
9. </androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Add a Button

1. Add a Button to your layout and constrain it to the parent layout.

1. <Button
2. android:id="@+id/button"
3. android:layout_width="wrap_content"
4. android:layout_height="wrap_content"
5. android:text="Click Me"
6. app:layout_constraintTop_toTopOf="parent"
7. app:layout_constraintStart_toStartOf="parent"/>
2. This code places the button at the top-left corner of the parent layout.

Step 4: Add a TextView Below the Button

1. Add a TextView and constrain it below the button.

1. <TextView android:id="@+id/textView"
2. android:layout_width="wrap_content"
3. android:layout_height="wrap_content" android:text="Hello World!"
4. app:layout_constraintTop_toBottomOf="@id/button"
5. app:layout_constraintStart_toStartOf="parent" />

2. This places the TextView right below the button.

Step 5: Use the Layout Editor (Drag and Drop)

1. Open the Design tab at the bottom of your layout XML file.
2. Drag and drop a new widget (such as a Button, TextView, or ImageView) from the
Palette onto the ConstraintLayout.
3. Use the Constraint Handles (circular dots on the widget) to create constraints to other
widgets or the parent layout.
o Drag from one handle to another widget or edge to form a constraint.

Step 6: Centre a Widget Horizontally

1. If you want to centre a widget horizontally or vertically within its parent, you can use
bias or match constraints.

1. <Button android:id="@+id/centerButton"
2. android:layout_width="wrap_content"
3. android:layout_height="wrap_content"
4. android:layout_width= "148dp"
5. android:layout_height="73d"
6. android:text="Centered"
7. app:layout_constraintTop_toTopOf="parent"
8. app:layout_constraintBottom_toBottomOf="parent"
9. app:layout_constraintStart_toStartOf="parent"
10. app:layout_constraintEnd_toEndOf="parent"
11. app:layout_constraintEnd_toEndOf="parent"
12. app:layout_constraintStart_toStartOf="parent"
13. app:layout_constraintEnd_toEndOf="parent"
14. app:layout_constraintHorizontal_bias="0.5"
15. app:layout_constraintVertical_bias="0.5"/>

Step 7: Run the App

1. Once your layout is ready, click Run (green play button) to compile and deploy your app
onto a virtual device or physical Android device.
2. You will see the app with your designed UI using ConstraintLayout.
Experiment 9: To design an Application using
Linear Layout
LinearLayout is the most basic layout in android studio, that aligns all the children
sequentially either in a horizontal manner or a vertical manner by specifying
the android:orientation attribute. If one applies android:orientation=”vertical” then
elements will be arranged one after another in a vertical manner and If you
apply android:orientation=”horizontal” then elements will be arranged one after another in a
horizontal manner.

Here’s a step-by-step guide to implementing and designing an app layout using


ConstraintLayout in Android Studio:`

Step 1: Create a New Android Project

1. Open Android Studio.


2. Click Create New Project.
3. Choose an Empty Activity and click Next.
4. Enter your project name, choose your language (Java), and set the API level (choose
one that supports ConstraintLayout, like API 16 or higher).
5. Click Finish to create the project.

Step 2: Open Your Layout XML File

1. After creating your project, navigate to the res/layout folder in the project view.
2. Open the activity_main.xml file. By default, you’ll have a ConstraintLayout if
you selected an empty activity. If not, change the root layout to ConstraintLayout.

1. <LinearLayout
2. xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:orientation="vertical"
6.
7. < !-- Add your widgets here -->
8.
9. </LinearLayout>

Step 3: Set the Orientation (Vertical or Horizontal)

1. The LinearLayout can arrange views either vertically or horizontally.


2. In the LinearLayout tag, set the android:orientation attribute:
o vertical: Stack the views on top of each other.
o horizontal: Arrange views side by side.

1. android:orientation="vertical" <!-- or "horizontal" -->


Step 4: Add a Button in LinearLayout

Add a Button inside the LinearLayout. It will automatically be positioned according to


the layout's orientation.

1. <Button
2. android:layout_width="wrap_content"
3. android:layout_height="wrap_content"
4. android:text="Click Me"/>

Step 5: Add an Image View Below the Button

Add a ImageView right after the Button. In a vertical LinearLayout, this will be positioned
below the button.

1. <ImageView
2. android:id="@+id/imageView"
3. android:layout_width="148dp"
4. android:layout_height="131dp"
5. android:layout_marginStart="10dp"
6. android:layout_marginTop="180dp"
7. android:layout_marginEnd="10dp"
8. app:srcCompat="@android:drawable/btn_star_big_on" />

o The widgets will be stacked one below the other in vertical orientation.
o If horizontal orientation is used, they will appear side by side.
o You can add Margin for the Image from(Start,End,Top,Bottom)etc.

Step 6: Align Views Using Gravity

Use the android:gravity attribute to align the content inside the views (e.g., center-align
the text within a TextView):

1. <TextView
2. android:layout_width="wrap_content"
3. android:layout_height="wrap_content"
4. android:gravity="center"
5. android:text="Centered Text" />

Step 7: For Horizontal Orientation and Use Weight for Flexible Spacing

1. You can use android:layout_weight to evenly distribute available space among


child views.
2. For example, to split space equally between two buttons:
3. The code below is for Horizontal Orientation.
1. <LinearLayout
2. android:layout_width="match_parent"
3. android:layout_height="wrap_content"
4. android:orientation="horizontal">
5.
6. <Button
7. android:layout_width="0dp"
8. android:layout_height="wrap_content"
9. android:layout_weight="1"
10. android:text="Button 1" />
11.
12. <Button
13. android:layout_width="0dp"
14. android:layout_height="wrap_content"
15. android:layout_weight="1"
16. android:text="Button 2" />
17.
18. </LinearLayout>

Step 8: Set Layout Width and Height

1. Control the size of widgets by using android:layout_width and


android:layout_height attributes:
o wrap_content: The widget sizes to fit its content.
o match_parent: The widget expands to fill the available space.

Step 9: Run the App

1. Once the design is complete, click Run (green play button) to deploy the app to an
Android Virtual Device (AVD) or a physical device.
2. You should now see your designed UI, with widgets laid out in a LinearLayout.
3. You will see your designed layout with the LinearLayout in action.
Experiment 10: To design an Application
using Relative Layout
A Relative Layout is a ViewGroup in Android that allows you to position and size child views in
relation to each other or to the parent container. Unlike other layouts such as LinearLayout,
which arranges elements sequentially (either vertically or horizontally), RelativeLayout lets you
position UI elements based on their relative positioning (above, below, to the left of, etc.) with
respect to other views or the container.

Key features of RelativeLayout:

1. Positioning Views Relative to Each Other: Views can be aligned or positioned relative to
other views (e.g., above, below, to the left, or to the right).
2. Positioning Views Relative to the Parent: Views can be aligned to the parent container's
edges (e.g., align with the parent’s top, bottom, left, or right side).
3. Z-Index Control: Child views can overlap each other, and you can control the layering
by using XML attributes.

Step-by-Step Implementation of RelativeLayout in Android Studio

Step 1: Set Up Your Android Studio Project

1. Open Android Studio.


2. Click on Start a New Android Studio Project.
3. Choose an Empty Activity template.
4. Set the application name, project location, and other configurations.
5. Click Finish to generate the project.

Step 2: Open XML Layout File

 In the res/layout folder, open the activity_main.xml file.


 By default, Android Studio may use ConstraintLayout. Replace it with
RelativeLayout in the XML code.

1. <RelativeLayout
2. xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent">

5. <!-- Add your child views here -->

6. </RelativeLayout>
Step 3: Add UI Elements and Define Relative Positioning

 You can now add child views inside the RelativeLayout.


 Use attributes like android:layout_below,
android:layout_alignParentTop, android:layout_toRightOf, etc., to
position the elements.

For example:

1. <RelativeLayout
2. android:layout_width="match_parent"
3. android:layout_height="match_parent">
4.
5. <!-- A TextView aligned at the top of the parent -->
6.
7. <TextView android:id="@+id/textView1"
8. android:layout_width="wrap_content"
9. android:layout_height="wrap_content"
10. android:text="Welcome"
11. android:layout_alignParentTop="true"
12. android:layout_centerHorizontal="true"/>
13.
14. <!-- A Button placed below the TextView -->
15.
16. <Button android:id="@+id/button1"
17. android:layout_width="wrap_content"
18. android:layout_height="wrap_content"
19. android:text="Click Me"
20. android:layout_below="@id/textView1"
21. android:layout_centerHorizontal="true"
22. android:layout_marginTop="20dp"/>
23. </RelativeLayout>
Step 4: Preview and Adjust Layout

 After adding elements, use the Design tab in Android Studio to visually adjust the layout
if needed.
 Check how views are positioned and make any adjustments in the XML if necessary.

Step 5: Add Logic in Activity

 You can now handle interactions in your MainActivity.java or MainActivity.kt


by finding these views and setting up event listeners.

1. Button button1 = findViewById(R.id.button1);


2. button1.setOnClickListener(new View.OnClickListener() {
3. @Override
4. public void onClick(View v) {
5. // Handle button click}});
Step 6: Customize and Refine the Layout

 Adjust margins, padding, and alignment as needed to fine-tune the layout for your
app's design. Use additional attributes like layout_margin, layout_centerInParent,
layout_alignParentBottom, etc.

Example of a more complex layout:

1. <RelativeLayout
2. xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent">

5. <!-- ImageView aligned to the top-left corner -->


6. <ImageView
7. android:id="@+id/imageView"
8. android:layout_width="100dp"
9. android:layout_height="100dp"
10. android:src="@drawable/sample_image"
11. android:layout_alignParentTop="true"
12. android:layout_alignParentStart="true"
13. android:layout_margin="16dp"/>

14. <!-- TextView placed to the right of the ImageView -->


15. <TextView
16. android:id="@+id/textView2"
17. android:layout_width="wrap_content"
18. android:layout_height="wrap_content"
19. android:text="Image Description"
20. android:layout_toRightOf="@id/imageView"
21. android:layout_alignTop="@id/imageView"
22. android:layout_marginStart="20dp"/>

23. <!-- Button placed below the ImageView -->


24. <Button
25. android:id="@+id/button2"
26. android:layout_width="wrap_content"
27. android:layout_height="wrap_content"
28. android:text="Button 2"
29. android:layout_below="@id/imageView"
30. android:layout_centerHorizontal="true"
31. android:layout_marginTop="30dp"/>

32. </RelativeLayout>

Step 7: Run the App

 Connect your Android device or use an emulator.


 Click Run > Run 'app' to see your layout in action.
Experiment 11: To Running an Android Java
app on a physical device (your phone)

Running an Android Java app on a physical device (your phone) requires a few simple steps to
set up your development environment and prepare your phone for testing. This can be done
with the help of USB debugging and Wireless Debugging. Below are the detailed steps:

Step 1: Prerequisites

o Android Studio: Ensure that Android Studio is installed on your computer. If you
haven't installed it yet.
o USB Cable: You will need a USB cable to connect your phone to your computer.
o Android Device: Any Android phone that you want to use for testing your app.
o Android device must support android 11+ to pair wireless or wi-fi debugging.

Step 2: Enable Developer Options and USB Debugging on Your Phone

Before you can run your app on a physical device, you need to enable Developer Options and
USB Debugging on your phone.

Steps to Enable Developer Options:

o Open Settings on your Android device.


o Scroll to "About phone" and tap it.
o Find "Build number" or “OS Version” (This could be under "Software
information" depending on your device).
o Tap "Build number or OS Version" 7 times until you see a message that says "You
are now a developer!"

 You may need to enter your device's PIN or password to enable Developer Mode.

Steps to Enable USB Debugging:

o After enabling Developer Options, go back to the Settings menu.


o Scroll to "System" (or directly search for "Developer Options" in the search bar).
o Tap on Developer options.
o Scroll down and find "USB Debugging".
o Turn on USB Debugging.
Step 3: Prepare Android Studio

Now that your phone is ready, it’s time to set up Android Studio.

o Open Android Studio.


o Open the project you want to run on your physical device.
o Check for SDK Installation:
 Go to Tools → SDK Manager and ensure that you have installed the Android SDK
for the version of Android that’s on your device.

Step 4: Connect Your Phone to the Computer

o Connect your phone to your computer using a USB cable.


o You may see a prompt on your phone asking you to allow USB debugging. Tap "Allow".
 You might also get a prompt that says "Allow access to data". Tap Allow
if needed.
 Sometimes, you will also see a pop-up asking if you want to trust the
connected computer. Select Always allow and then tap OK.

Step 5: Set Up Android Studio to Recognize Your Device

o In Android Studio, click on the device selector dropdown at the top of the screen
(next to the Run/Debug buttons).
 It may take a few seconds for Android Studio to detect your device.
o Once your phone is detected, it will show up in the list of available devices. Select
your phone from the list.

Step 6: Build and Run the App on Your Device

o Ensure that your phone is selected as the target device.


o Click the green "Run" button or press Shift + F10 to build and run the app.

 Android Studio will now compile the project and install the app on your device.

o The app will launch on your phone after installation.


Step 7: Run the App via Wi-Fi (Second method)

You can also run the app on your device wirelessly if both the computer and phone are on the
same Wi-Fi network:

Note: Phone must support android 11+

o Connect your phone to the computer with a USB cable.


o On your phone, go to Developer Options and enable Wireless Debugging.
o On Android Studio, go to Device Manager and connect your device via Wi-Fi.

Step 1: Connect Your Device via USB Initially

To set up the wireless connection for the first time, you must connect your phone to your
computer via USB.

o Use a USB cable to connect your Android device to your computer.


o Allow any prompts that appear on your phone asking for USB Debugging permissions.

Now that your device is connected via USB, you can enable Wireless Debugging on your device.

o On your Android device, go to Settings → Developer Options.


o Scroll down to the Debugging section.
o Find and enable Wireless Debugging.

Step 2: Pair using wi-fi

o Click on the device selector dropdown at the top of the screen (next to the
Run/Debug buttons).
o Click on Pair Devices using wi-fi.
o Select pair via QR code or Pairing code.

On your Android device:

o Tap Wireless Debugging.


o Then tap Pair device with QR code or pairing code.
Step 3: Disconnect the USB Cable

Once the pairing is successful, you can now disconnect the USB cable. The device will remain
connected over Wi-Fi for debugging purposes.

Step 4: Run Your App on the Device Over Wi-Fi

o In Android Studio, ensure your physical device is selected in the device dropdown
(you should see your device’s name listed under Wi-Fi debugging).

o Click the green "Run" button or press Shift + F10 to build and run the app.

 The app will now be installed and launched on your phone, all over Wi-
Fi, without the need for a USB connection.
Experiment 12: To illustrate various android
activities.
An activity in Android is a specific combination of XML files and JAVA files. It is basically a
container that contains the design as well as coding stuff. XML files provide the design of the
screen and JAVA files deal with all coding stuff like handles, what is happening, design files, etc.
In Android development, activities are fundamental components that represent a single
screen with a user interface in an Android app. Each activity is like a page of an app where users
interact with different elements like buttons, text fields, etc. Activities manage the app's UI and
handle user interactions.

Here’s a list of some common types of activities in Android Studio:


1. No Activity.
2. Basic Activity.
3. Button Navigation Activity.
4. Empty Activity.
5. Full Screen Activity.
6. Google Map Activity.
7. Login Activity.
8. Scrolling Activity.
9. Tabbed Activity.
10. Native C++

These activities are different templates or predefined setups for Android development in
Android Studio. When starting a new project, Android Studio offers these templates to help
developers quickly create common types of activities. Here’s an explanation of each one, its
uses, importance, and key functionalities:

1. No Activity

o Use: As the name suggests, it’s an empty project without any predefined layout or
components. It’s useful when you want complete control over the setup and wish
to create everything from scratch.
o Importance: This is the most basic option, suitable for experienced developers
who prefer starting with a clean slate.
o Functionalities: You will need to manually create the MainActivity and design the
user interface, adding required components like buttons, text fields, etc.
2. Basic Activity

o Use: It creates an app with a single activity and a floating action button (FAB). It
also includes support for the Android AppBar (Toolbar) at the top.
o Importance: This is a good starting point for simple apps and serves as a foundation
for adding more functionalities.
o Functionalities:

 Floating Action Button for quick actions.


 Toolbar for app title and navigation.
 XML layout files (activity_main.xml) with basic elements.

3. Button Navigation Activity

o Use: This template includes a layout with a bottom navigation bar, which is
commonly used in modern apps to navigate between three or more top-
level destinations.
o Importance: Ideal for apps that need to switch between different fragments or
activities, like a social media app where you navigate between Home, Search, and
Profile screens.
o Functionalities:

o Bottom navigation bar.


o Built-in fragment handling for navigation between multiple sections.

4. Empty Activity

o Use: Similar to the "No Activity" option, but it provides a basic activity class and
layout file. This is useful when you want to start simple and add more complexity as
needed.
o Importance: It gives a bare-bones structure that still includes an Activity class
and XML layout, useful for small-scale apps or demos.
o Functionalities:

 Main activity class (MainActivity.java or MainActivity.kt).


 Empty layout file for creating a custom user interface.

5. Full-Screen Activity

o Use: This activity provides a layout that takes up the entire screen, hiding the status
and navigation bars. It’s used for immersive apps, like media players or games.

o Importance: Full-screen activities enhance user immersion, making them ideal


for content consumption apps (videos, photos, etc.) and games.
o Functionalities:

 Full-screen mode (no status or navigation bars).


 Example code for handling user interaction in full-screen mode (e.g., tapping
to show or hide UI elements).

6. Google Maps Activity

o Use: This template integrates Google Maps into your app, allowing users to interact
with maps, view their location, and more.
o Importance: It’s essential for apps that involve location-based services, like
navigation, tracking, or local business searches.
o Functionalities:

 Pre-configured Google Maps API setup.


 Displays a Google Map in the activity with user location tracking.
 Example code for interacting with the map (zoom, markers, etc.).

7. Login Activity

o Use: It provides a pre-built login screen with user input fields (username, password)
and buttons (sign-in, sign-up).
o Importance: Many apps need authentication, so this template is valuable for
adding secure login functionality quickly.
o Functionalities:

 Input fields for email/username and password.


 Buttons for logging in and signing up.
 Integration with Firebase Authentication or other login mechanisms (like
Google or Facebook login).

8. Scrolling Activity

o Use: This template creates an activity with a scrollable layout. It is helpful for apps
that need to display a long list of content or a page that exceeds the screen height.
o Importance: Scrolling activity is crucial for content-heavy apps like blogs, news apps,
or documentation.
o Functionalities:

 ScrollView component that allows vertical scrolling.


 Toolbar and floating action button for extra actions.
 Content placeholders where you can add text, images, or lists.
9. Tabbed Activity

o Use: It provides a layout with tabs at the top, allowing users to switch between
different views or fragments.
o Importance: It is commonly used in apps that need to categorize content into
different sections, like a messaging app with "Chats," "Calls," and "Contacts" tabs.
o Functionalities:

 ViewPager or ViewPager2 for swiping between tabs.


 TabLayout to display tabs at the top.
 Fragment integration to display content for each tab.

10. Native C++ Activity

o Use: This template allows you to write part of your app in C++ for high-
performance tasks. It is used in applications where native performance is critical,
such as games, multimedia processing, or apps with heavy computational tasks.
o Importance: Useful when you need to integrate native code for better performance
or when interacting with pre-existing C++ libraries.
o Functionalities:

 A basic activity that interacts with C++ code via the Android NDK
(Native Development Kit).
 C++ source files (.cpp) for writing native code.
 Pre-configured CMake build script for compiling C++ code.
Experiment 13: To design an android
application to show your own name.
To make an application to print own name on an android application, we use Empty Views
Activity. Here we can also customize the appearance of the text by modifying other attributes
such as android:textSize, android:textColor, android:textStyle, etc.

These are the some steps below to show your name on your application in Android
Studio:

Step 1: Open Android Studio

o Launch Android Studio on your computer.

Step 2: Create a New Project

o Click on File > New > New Project.


o Choose Empty Activity and click Next.
o Enter your Project Name (e.g., "HelloWorld"), Package Name
(e.g., "com.example.helloworld"), and Save Location.
o Click Finish

Step 3: Open the XML Layout File

o In the Project Explorer, navigate to the res folder.


o Open the layout subfolder.
o Double-click the activity_main.xml file.

Step 4: Modify the Text in the XML File

o Locate the <TextView> element within the <ConstraintLayout> or


<LinearLayout> in the XML file.
o Change the android:text attribute to your desired name. For example:

Step 5: XML file


1. <?xml version="1.0" encoding="utf-8"?>
2. <androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:id="@+id/main"
6. android:layout_width="match_parent"
7. android:layout_height="match_parent"
8. android:visibility="visible"
9. tools:context=".MainActivity"
10. tools:visibility="visible"
11. android:background="@drawable/bgcolor">
12.
13. <TextView
14. android:id="@+id/idTVHeading1"
15. android:layout_width="308dp"
16. android:layout_height="102dp"
17. android:layout_marginStart="51dp"
18. android:layout_marginTop="125dp"
19. android:layout_marginEnd="52dp"
20. android:layout_marginBottom="125dp"
21. android:text="Manpreet Singh"
22. android:textColor="#034efc"
23. android:textSize="80dp"
24. android:textStyle="bold"
25. app:layout_constraintBottom_toBottomOf="parent"
26. app:layout_constraintEnd_toEndOf="parent"
27. app:layout_constraintStart_toStartOf="parent"
28. app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

You can also change the background color and font color of the android:text

Step 6: Edit above XML file

o Change android:text attribute in above file.


o Replace android:text="Hello World!" by android:text="John Wick"

Step 7: Run the App

o Click the Run button (green triangle) in the toolbar.


o Select a connected Android device or emulator.
o The app will build and run, displaying the modified text "John Wick" on the
AVD or physical device

You might also like