Android Studio User Interface

module = Module(
    code="ELEE1146",
    name="Mobile Applications for Engineers",
    credits=15,
    module_leader="Seb Blair BEng(H) PGCAP MIET MIHEEM FHEA"
)
Android Studio User Interface

Designing an Android App


  • Designing apps is like constructing a building

  • The Big Picture
  • Follow these steps:
    • JetPack Composeable elements
    • Create an Activity (Kotlin class) for every screen
    • Code each Kotlin class with appropriate objects and actions
    • Test the application in the emulator
Android Studio User Interface

Designing an Android App

Android Studio User Interface

Using the Android User Interface



  • The interface is a window on the screen of any mobile device

  • Android apps run on various form factors such as smartphones, smart watches, tablets, televisions, etc.
    • Form factor refers to the screen size, configuration, or physical layout of a device

  • The layout is designed with the Composable class
Android Studio User Interface

Resources: String


  • strings.xml is a default file that is part of every Android application and contains commonly used strings for an application. It is located in res/values folder

  • Every string is composed of a key, also called the id property, which is the name of a control, and a default value, which is the text associated with the control
<resources>
    <string name="app_name">Your App's Name</string>
    <string name="welcome_message">Welcome to the app!</string>
    <string name="login_button">Log In</string>
    <string name="signup_button">Sign Up</string>
    <string name="error_message">An error has occurred. Please try again.</string>
    <string name="logout_message">You have been logged out.</string>

    <!-- With placeholders -->
    <string name="greeting_message">Hello, %1$s!</string>

    <!-- With formatting -->
    <string name="item_count">You have %1$d items in your cart.</string>

    <!-- Multiline -->
    <string name="terms_conditions">By using this app, you agree to our Terms &amp; Conditions.</string>

    <!-- Array example -->
    <string-array name="days_of_week">
        <item>Monday</item>
        <item>Tuesday</item>
        <item>Wednesday</item>
        <item>Thursday</item>
        <item>Friday</item>
        <item>Saturday</item>
        <item>Sunday</item>
    </string-array>

    <!-- Plurals example -->
    <plurals name="number_of_messages">
        <item quantity="one">%d new message</item>
        <item quantity="other">%d new messages</item>
    </plurals>
</resources>

Android Studio User Interface

Compose Layouts

Android Studio User Interface

Columns

Android Studio User Interface

Rows

Android Studio User Interface

Units of Measurement


The text property uses text from a string resource to display within the control.


The textSize property can use various units of measurement, as shown below


Unit of Measure Abbreviation Example
Inches in "0.5in"
Millimeters mm "20mm"
Pixels px "100px"
Density-independent pixels dp or dip "100dp" or "100dip"
Scaled-independent pixels sp "10sp"

The preferred unit of measurement is often sp, which stands for scaled-independent pixels

Android Studio User Interface

Adding a File to the Resources Folder

  • Android creates a Drawable resource for any of these images when you save them in the res/drawable folder

Name Description
drawable Generic resources for any screen
mipmap - xxhdpi Resources for extra, extra high-density screens
mipmap - xhdpi Resources for extra high-density screens
mipmap - hdpi Resources for high-density screens
mipmap - mdpi Resources for extra, medium-density screens
mipmap - ldpi Resources for low-density screens

  • Android projects have multiple drawable folders that are named after the resolution of the device
Android Studio User Interface

Creating Activities


  • An Activity is one of the core components of an Android application

  • Each screen is considered an activity
    • The point at which the application makes contact with users

  • Planning a program
    • Gather and analyse the program requirements
    • Design the user interface
    • Design the program processing objects
    • Code the program
    • Test the program
Android Studio User Interface

Creating Activities


  • Adding a Class File


    • A class describes a group of objects and serves as a blueprint, or template, for creating those objects

    • An object is a specific, concrete instance of a class

    • When you create an object, you instantiate it; meaning you define one particular variation of the object
Android Studio User Interface

The Android Manifest File


  • The Android Manifest file contains:
    • the name of the Java application
    • a listing of each activity
    • permissions needed to access other Android functions (like accessing the Internet)
    • the minimum level of the Android APL

  • Adding an Activity to the Android Manifest
    • When applications have more than one activity, the Manifest must have an intent to navigate among multiple activities
    • An intent in Android is an abstract description of an operation to be performed
Android Studio User Interface

The Android Manifest File

Android Studio User Interface

Coding the Kotlin Activity


  • A method is a set of Kotlin statements that can be included inside a Kotlin class to perform specific tasks

  • The method body contains a collection of statements that define what the method does.

  • Coding an onCreate Method

  • Requires corresponding setContent Kotlin code to display a specific screen





override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
          ...
        }
    }
Android Studio User Interface

Moving between composables


  • NavController: Handles the app's navigation and manages the back stack of composable screens.

  • NavHost: Hosts the navigation graph, specifying the composable destinations.


    val navController = rememberNavController()
    NavHost(
        navController = navController,
        startDestination = "home"
    ) {
        composable("home") { HomeScreen(navController) }
        composable("details") { DetailsScreen(navController) }
    }
    
Android Studio User Interface

@Composable and composable




  • @Composable: This is an annotation used to mark a function as composable. It tells the compiler that this function can be used in the UI hierarchy to build the UI in a declarative way. Any function marked with @Composable can define part of the UI.

  • composable(): This is a function used within a NavHost to declare a navigation destination. It defines which composable function should be rendered when a user navigates to that destination.



@Composable
fun MyScreen() {
    Text("Hello")
}


NavHost(navController, startDestination = "home") {
    composable("home") { HomeScreen() }
}
Android Studio User Interface

Resources

In Android Studio, resources (like strings, layouts, drawables, etc.) are referenced numerically because of how they are handled in the underlying Android system for efficiency and performance.



  • Efficiency/Performance: The Android build system compiles all resources into a single class (R.java or R.kt) where each resource is assigned a unique integer identifier. Using integers for referencing resources allows for faster lookup during runtime compared to using text-based identifiers.


  • Naming Convention: "File-based resource names must contain only lowercase a-z, 0-9, or underscore... must not start with a number" - source the Compiler


avocado_toast_1.png // Acceptable
1_Avocado-toast.png // Not Acceptable
Android Studio User Interface

Now do the lab!

@Composable defines UI components. composable() defines navigation destinations