Unit 3
Unit 3
DESIGNING BY DECLARATION:
1. Procedural Design (Imperative Programming)
Used in:
• Traditional Android View system (XML + Java/Kotlin)
Example:
val textView = TextView(this)
textView.text = "Hello, World!"
textView.textSize = 20f
linearLayout.addView(textView)
Characteristics:
• You manually manage UI components.
• Explicit state handling (findViewById, setText, etc.).
• Tedious updates when state changes.
2. Declarative Design
Used in:
• Jetpack Compose (Android)
Example using Jetpack Compose:
@Composable
fun Greeting(name: String) {
Text(text = "Hello, $name!")
}
Characteristics:
• UI is a function of state.
• System handles UI re-rendering.
• No findViewById or manual UI manipulation.
• Code is more concise and reactive.
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
findViewById<Button>(R.id.btnNewGame).setOnClickListener {
// TODO: Start new game activity
}
findViewById<Button>(R.id.btnContinue).setOnClickListener {
// TODO: Continue previous game
}
findViewById<Button>(R.id.btnSettings).setOnClickListener {
// TODO: Open settings screen
}
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="24dp">
<TextView
android:id="@+id/titleText"
android:text="Sudoku"
android:textSize="32sp"
android:textStyle="bold"
android:layout_marginBottom="32dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnNewGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Game" />
<Button
android:id="@+id/btnContinue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continue"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/btnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:layout_marginTop="16dp" />
</LinearLayout>
AlertDialog.Builder(context)
.setTitle("About This App")
.setMessage("Version: $appVersion\n\nDeveloped by Your Name\nVisit:
https://yourwebsite.com")
.setPositiveButton("OK", null)
.show()
}
Call it in your activity or fragment like:
aboutButton.setOnClickListener {
showAboutDialog(this)
}
<TextView
android:id="@+id/about_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="About this app"
android:paddingTop="10dp"
android:textAlignment="center"/>
</LinearLayout>
b) Load it in Kotlin:
fun showCustomAboutDialog(context: Context) {
val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_about, null)
dialogView.findViewById<TextView>(R.id.about_text).text =
"Version: $appVersion\nDeveloped by Your Name"
AlertDialog.Builder(context)
.setView(dialogView)
.setPositiveButton("OK", null)
.show()
}
APPLYING A THEME:
Applying a theme in Android is a powerful way to maintain consistency and customize the
look and feel of your app. Below is a clear explanation of its importance, how to apply a
custom theme, and the difference between styles and themes.
Themes ensure all UI components (buttons, text, background, etc.) follow a uniform
appearance across activities and fragments.
2. Easy Maintenance
You can change your app’s entire look (e.g., from light to dark mode) by updating the theme
in one place instead of modifying each component individually.
3. Brand Identity
Themes help reinforce your app's branding (colors, fonts, logo styles) and improve user
recognition.
4. Reusability
Themes and styles make your UI scalable—apply them across multiple activities or views
with minimal duplication.
You can also define separate values for themes in themes.xml (night) for dark mode.
<style name="MyButtonStyle">
<item name="android:background">#6200EE</item>
<item name="android:textColor">#FFFFFF</item>
</style>
Usage:
<Button
style="@style/MyButtonStyle"
android:text="Click Me" />
<application android:theme="@style/Theme.MyApp">
ADDING A MENU:
In any mobile app, menus provide an intuitive way to access actions and settings without
cluttering the main interface. For the Sudoku app, adding a menu enhances user experience
by offering easy access to important features such as starting a new game, viewing the
app’s settings, or accessing help and about information. By incorporating a menu into the
action bar (toolbar) or a floating action button (FAB), the user can quickly perform tasks
without navigating away from the main screen.
In this case, for the Sudoku app, the menu can include items like New Game, Settings, Help,
and Exit. The New Game item allows the user to start a fresh Sudoku puzzle, while the
Settings item can open a screen for configuring game difficulty or preferences. The Help
item can provide instructions or tips on how to play Sudoku, and the Exit item allows the
user to quit the game. This provides an efficient way to manage app functionality in a clean
and organized manner, all while keeping the main gameplay area uncluttered and user-
friendly.
By using a Toolbar to hold the menu and enabling features like icons and custom themes, the
menu becomes a seamless part of the overall app design, ensuring a polished and intuitive
user interface for all levels of users.
ADDING SETTINGS:
package com.example.sudokuapp
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
2. Create a SettingsFragment
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="difficulty"
android:title="Select Difficulty"
android:summary="%s"
android:defaultValue="medium"
android:entries="@array/difficulty_levels"
android:entryValues="@array/difficulty_values" />
<SwitchPreferenceCompat
android:key="sound"
android:title="Enable Sound"
android:defaultValue="true" />
</PreferenceScreen>
<string-array name="difficulty_levels">
<item>Easy</item>
<item>Medium</item>
<item>Hard</item>
</string-array>
<string-array name="difficulty_values">
<item>easy</item>
<item>medium</item>
<item>hard</item>
</string-array>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_settings"
android:title="Settings"
android:icon="@android:drawable/ic_menu_preferences"
app:showAsAction="ifRoom" />
</menu>
In your MainActivity, handle the click event for the settings menu item.
DEBUGGING:
Debugging
Importance of Debugging
Logging involves placing statements in your code to print messages to the console (Logcat
in Android). These messages help track the flow of execution and inspect variable values at
runtime.
Why Use It:
Drawbacks:
The debugger is an advanced tool integrated into IDEs like Android Studio, which allows
developers to:
• Pause execution
• Inspect app state
• Watch variables
• Trace execution path without modifying code
Core Features:
1. Breakpoints:
Pause execution at specific lines.
Benefits:
Drawbacks: