Topic 3: Understanding Android Components Part-2

This is our second part of previous topic Understanding Android Components

Topic 3: Understanding Android Components Part-2

Hello Devs, those Who follow this series know that our last blog was so long and I am not able to explain all the Android components on that blog and I tell you that I wrote another blog for that remaining component. So here you go to catch up on the other remaining components. In this blog, we discuss about Manifest file, Layout, Resources and Assets.

Manifest File

The AndroidManifest.xml file is a crucial part of any Android project. It serves several important purposes:

  1. Declaring App Components: The manifest file declares all components of the app, such as activities, services, broadcast receivers, and content providers. Each component needs to be declared in the manifest file to ensure that the Android system knows about them and can interact with them.

  2. Defining App Permissions: The manifest file specifies the permissions that the app requires to access certain features or data on the device. For example, if the app needs access to the internet, it must declare the INTERNET permission in the manifest file.

  3. Setting App Configuration: Various configuration settings for the app are specified in the manifest file, such as the app's minimum and target SDK versions, the screen orientations supported by the app, hardware requirements, and more.

  4. Specifying App Metadata: The manifest file can include metadata about the app, such as the app's name, icon, version number, theme, and other attributes that affect the app's appearance and behaviour.

Here's a simple example of an AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- Other app components (services, broadcast receivers, etc.) would be declared here -->
    </application>

    <!-- Permissions required by the app -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- More permissions can be declared here -->

    <!-- Minimum and target SDK versions -->
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
</manifest>

In this example:

  • The <manifest> element declares the package name of the app.

  • The <application> element contains configuration settings and declares the app's components, such as activities.

  • The <activity> element declares the main activity of the app and specifies an intent filter to make it the launcher activity.

  • The <uses-permission> element declares permissions required by the app, such as INTERNET permission.

  • The <uses-sdk> element specifies the minimum and target SDK versions required by the app.

The AndroidManifest.xml file is crucial for the Android system to understand the structure, requirements, and capabilities of the app. It's the entry point for the Android system to interact with the app and plays a key role in the app's installation, execution, and security.

Layouts

In Android development, a layout is a crucial component that defines the structure and appearance of the user interface(UI) of an application. It acts as a container for other UI elements like buttons, text fields, images, etc., arranging them in a specified manner on the screen. Layouts in Android are implemented using XML files located in the res/layout directory of the project.

1. LinearLayout:

LinearLayout arranges its children's elements either horizontally or vertically.

Example: Suppose you want to create a login screen with a vertical arrangement of labels and input fields.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Username"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your username"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your password"
        android:inputType="textPassword"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"/>

</LinearLayout>

Considerations:

  • LinearLayout is simple and easy to use for arranging views in a single direction.

  • It's efficient for simple layouts but can lead to nested layout hierarchies if overused.

  • Layout weights can be used to distribute space among child views.

2. RelativeLayout:

RelativeLayout positions its children relative to each other or relative to the parent container.

Example: Suppose you want to create a profile screen with a profile picture at the top and user information below it.

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

    <ImageView
        android:id="@+id/profile_picture"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/profile_picture"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="John Doe"
        android:layout_below="@id/profile_picture"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Software Developer"
        android:layout_below="@id/profile_picture"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"/>

</RelativeLayout>

Considerations:

  • RelativeLayout provides more control overview positioning compared to LinearLayout.

  • It's suitable for complex layouts where views need to be positioned relative to each other.

  • Nested RelativeLayouts can lead to complex and hard-to-maintain layouts.

3. ConstraintLayout:

ConstraintLayout offers flexible layout design by creating relationships between UI elements using constraints.

Example: Suppose you want to create a signup screen with a form layout.

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:id="@+id/username_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Username"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <EditText
        android:id="@+id/username_field"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Enter your username"
        app:layout_constraintTop_toBottomOf="@id/username_label"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <TextView
        android:id="@+id/password_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password"
        app:layout_constraintTop_toBottomOf="@id/username_field"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="16dp"/>

    <EditText
        android:id="@+id/password_field"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Enter your password"
        android:inputType="textPassword"
        app:layout_constraintTop_toBottomOf="@id/password_label"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <Button
        android:id="@+id/signup_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sign Up"
        app:layout_constraintTop_toBottomOf="@id/password_field"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="16dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Considerations:

  • ConstraintLayout is powerful for building complex and responsive layouts with a flat view hierarchy.

  • It reduces the need for nested layouts, improving performance and simplifying layout management.

  • Understanding constraint-based layouts can require some learning curve.

4. FrameLayout:

FrameLayout is useful for stacking UI elements on top of each other.

Example: Suppose you want to create a simple image viewer with an image displayed in the center.

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/image"
        android:scaleType="centerCrop"/>

</FrameLayout>

Considerations:

  • FrameLayout is lightweight and suitable for simple scenarios like displaying a single-child view.

  • It's commonly used for fragments or dynamically adding/removing views.

5. GridLayout:

GridLayout arranges UI elements in rows and columns.

Example: Suppose you want to create a grid of images.

<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="3"
    android:rowCount="2">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image1"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image2"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image3"/>

    <!-- Add more ImageViews here -->

</GridLayout>

Considerations:

  • GridLayout is useful for creating grid-based layouts such as forms, grids of images, or spreadsheet-like displays.

  • Specifying row and column weights appropriately can achieve the desired behaviour on different screen sizes and orientations.

As a developer, this is your choice of how you use these layouts and make your UI better and smoother for the user.

Now we talk about the last topic of our blog and Android components is Resources and Assets.

Resources & Assets

Resources

Resources in Android refer to external elements such as images, strings, colors, layouts, and other non-code files used by your Android application. They are stored in the res directory of your project and are accessible via the resource ID generated by the Android system. Resources are crucial for building user interfaces and providing localized content.

Types of Resources:

  1. Drawable Resources: These are image files (e.g., PNG, JPEG) used for icons, backgrounds, or any other images in your app's UI. They are stored in the res/drawable directory.

  2. Layout Resources: XML files that define the structure of your app's user interface. They specify the arrangement of UI components such as buttons, text fields, etc. Layout resources are stored in the res/layout directory.

  3. String Resources: Textual content used in your app, such as button labels, error messages, etc., are stored in XML files in the res/values directory, typically in strings.xml. Using string resources allows for easy localization and central management of text content.

  4. Color Resources: XML files that define color values used throughout your app's UI. They are stored in the res/values directory, usually in colors.xml.

  5. Style Resources: XML files defining styles and themes for your app's UI components. They are stored in the res/values directory, often in styles.xml.

  6. Dimension Resources: XML files defining dimensions (e.g., margins, paddings) used in your app's layout files. They are stored in the res/values directory, typically in dimens.xml.

Assets

Assets are raw files that are packaged with your application and can be accessed using an AssetManager. Unlike resources, assets are not compiled and optimized by the Android build system. They can be of any file type and are typically used for data files, fonts, or other static content that doesn't require localization.

Usage of Assets:

Assets can be useful for:

  • Storing large binary files like audio or video files.

  • Including custom fonts or icon fonts.

  • Accessing raw data files like JSON or XML configurations.

  • Distributing pre-built databases for your app.

Uff I know this Android Components topic is very long but finally did it. We completed this in two parts and we've reached the end of this topic. But there are more topics coming on this Learn Android from basic to advanced series. So, see you on another topic from this series Type of classes in Android.


Connect with Me:

Hey there! If you enjoyed reading this blog and found it informative, why not connect with me on LinkedIn? 😊 You can also follow my Instagram page for more mobile development-related content. πŸ“²πŸ‘¨β€πŸ’» Let’s stay connected, share knowledge and have some fun in the exciting world of app development! 🌟

Check out my Instagram page

Check out my LinkedIn

Β