VNSGU BCA Sem 3: Mobile App Dev (305-2) Practical Solutions - April 2025 Set A
Paper Information
| Attribute | Value |
|---|---|
| Subject | Mobile Application Development - I |
| Subject Code | 305-2 |
| Set | A |
| Semester | 3 |
| Month/Year | April 2025 |
| Max Marks | 25 |
| Paper | View Paper | Download PDF |
Questions & Solutions
Q1: Fullscreen & Screen Orientation Toggle
Max Marks: 20
Create a Mobile Application in Android to hide title bar and open in Full Screen Size and change the orientation of the screen on button click.
1. Concept Explanation
Hiding the Title Bar and Action Bar
In Android, the title bar (Action Bar) can be hidden in two ways:
- XML Theme Config: Setting the theme in
themes.xmlto aNoActionBarvariant (e.g.Theme.MaterialComponents.DayNight.NoActionBar). - Programmatic Call: Using
supportActionBar?.hide()inside the Activity'sonCreate()method.
Running in Fullscreen Mode
To display an Activity in true fullscreen (hiding the status bar, navigation bar, etc.):
- Modern APIs (API 30+): Use
WindowInsetsControllerto hide system bars. - Legacy APIs: Use
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN).
Toggling Screen Orientation Programmatically
You can change the screen orientation of an activity on-the-fly by setting the requestedOrientation property:
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPEActivityInfo.SCREEN_ORIENTATION_PORTRAIT
2. Algorithm & Step-by-Step Logic
- Design User Interface (
activity_main.xml): Add a singleButtonin the center of aConstraintLayoutto act as the orientation toggler. - Hide Title Bar programmatically:
- In
MainActivity.kt, callsupportActionBar?.hide().
- In
- Apply Fullscreen Flags:
- Retrieve the window reference and apply layout flags to hide status/navigation bars.
- Implement Orientation Toggle Logic:
- Bind a click listener to the button.
- Check the current screen orientation via
resources.configuration.orientation. - If it is Portrait (
Configuration.ORIENTATION_PORTRAIT), setrequestedOrientationto Landscape. - If it is Landscape, set
requestedOrientationto Portrait.
3. Implementation Code & Output
View Solution & Output
Layout File: res/layout/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"
android:background="#1E1E1E"
tools:context=".MainActivity">
<!-- Central Orientation Toggle Button -->
<Button
android:id="@+id/btnToggleOrientation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Toggle Screen Orientation"
android:textSize="16sp"
android:backgroundTint="#FFBC3B"
android:textColor="#1A1A37"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity File: src/main/java/com/example/fullscreenapp/MainActivity.kt
// 📱 VNSGU BCA Sem 3 - Mobile Application Development (Practical 305-2)
// 🚀 Action: Configure Fullscreen, hide title bar, and toggle orientation dynamically.
package com.example.fullscreenapp
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.view.WindowInsets
import android.view.WindowInsetsController
import android.view.WindowManager
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 1. Programmatic Title Bar Hiding
supportActionBar?.hide()
// 2. Programmatic Fullscreen Activation
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Modern API 30+ implementation
window.insetsController?.let { controller ->
controller.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
} else {
// Legacy implementation
@Suppress("DEPRECATION")
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
setContentView(R.layout.activity_main)
// 3. Orientation Toggling Logic on Button Click
val toggleButton: Button = findViewById(R.id.btnToggleOrientation)
toggleButton.setOnClickListener {
val currentOrientation = resources.configuration.orientation
if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
// Change to Landscape
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
Toast.makeText(this, "Switched to Landscape Mode", Toast.LENGTH_SHORT).show()
} else {
// Change to Portrait
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
Toast.makeText(this, "Switched to Portrait Mode", Toast.LENGTH_SHORT).show()
}
}
}
}
Manifest File: AndroidManifest.xml (Excerpt)
Ensure you add android:configChanges to your activity node. This tells the system that your activity will handle orientation changes itself, preventing the activity from restarting and reloading its state when the screen rotates:
<activity
android:name=".MainActivity"
android:exported="true"
android:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
💡 Common Examination Mistakes & Tips
Key Pitfalls to Avoid
- Call order issues: Always call
supportActionBar?.hide()or layout parameters before callingsetContentView(R.layout.activity_main). Otherwise, the layout is drawn before settings are applied, which can lead to visual glitches. - No Configuration Handling: By default, Android restarts the current Activity when screen orientation changes. Always add
android:configChanges="orientation|screenSize"to your activity insideAndroidManifest.xmlto avoid data loss. - Modern vs Legacy APIs: Show knowledge of
WindowInsetsControllerfor SDK 30+ instead of relying solely on deprecatedWindowManagerflag settings.