MOBILE APPLICATION DEVELOPMENT (FALL 2024)
1. What is the purpose of Android emulator?
The Android emulator is a virtual device that runs on your computer and simulates
a real Android phone or tablet. It allows developers to test applications on different
Android versions and screen sizes without needing physical devices.
Purpose:
• Test apps without a real device
• Check performance on multiple Android versions
• Debug apps easily using tools like Logcat
Example:
If you're developing an app that uses GPS, you can use the emulator to simulate
different locations and see how your app behaves.
2. What is Intent?
An Intent is a message that allows components (like activities, services, or
broadcast receivers) to request actions or pass data.
Types of Intent:
• Explicit Intent – Directly starts a specific component
• Implicit Intent – Requests an action without naming the component
Example:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent); // Explicit Intent to start SecondActivity
3. What is AutoCompleteTextView in Android?
AutoCompleteTextView is an editable text field that shows suggestions as the
user types, based on a predefined list of items.
Purpose:
• Helps users type faster
• Reduces input errors
• Improves UX by showing suggestions
Example:
AutoCompleteTextView autoText = findViewById(R.id.autoText);
String[] countries = {"India", "Indonesia", "Italy"};
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, countries);
autoText.setAdapter(adapter);
4. What is the purpose of <resources> in Android?
The <resources> tag is used in XML files (e.g., strings.xml, colors.xml) to
define and manage values separately from code. It supports easier maintenance and
localization.
Purpose:
• Store app data like strings, colors, and dimensions
• Allow multi-language support
• Reuse values across layouts and code
Example:
<resources>
<string name="app_name">MyApp</string>
</resources>
Use in layout or code:
<TextView android:text="@string/app_name"/>
5. What is RelativeLayout?
RelativeLayout is a layout where child views are placed relative to each other or
the parent layout.
Purpose:
• Flexible layout design
• Arrange views based on position rules
Example:
<RelativeLayout ...>
<TextView
android:id="@+id/text1"
android:text="Hello" />
<Button
android:layout_below="@id/text1"
android:text="Click Me" />
</RelativeLayout>
Here, the Button is placed below the TextView.
6. What is Activity?
An Activity is a component that provides a screen with which users can interact.
Every Android app has at least one activity (usually MainActivity).
Purpose:
• Represent a single screen in the app
• Handle user interface and interactions
• Manage lifecycle methods (onCreate, onStart, etc.)
Example:
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
This activity loads a layout file and displays it as the main screen.
LONG QUESTIONS
Q. What is ImageSwitcher in Android? Explain its
working with an example and list its important methods. (15
Marks)
Answer:
Introduction:
In Android, ImageSwitcher is a subclass of ViewSwitcher used to animate image transitions. It
allows switching between images with custom animations (like fade, slide, etc.) and provides a
more dynamic user interface than ImageView.
Features of ImageSwitcher:
• Displays only one image at a time.
• Supports smooth transition animations.
• Requires a ViewFactory to create ImageView instances.
• Supports images from resources, drawables, or URIs.
Life Cycle:
1. Create the ImageSwitcher in XML or Java.
2. Set a ViewFactory to provide the views (typically ImageViews).
3. Set in and out animations for transitions.
4. Use setImageResource() or similar methods to change the image.
Common Methods:
Method Description
setFactory(ViewFactory) Sets a factory to create views for switching.
setImageResource(int) Sets the image from drawable resource.
setImageDrawable(Drawable) Sets image from a Drawable object.
setImageURI(Uri) Sets image from URI.
setInAnimation(Animation) Animation used to show new image.
setOutAnimation(Animation) Animation used to hide current image.
Example:
➤ XML Layout:
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerInParent="true" />
<Button
android:id="@+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Image"
android:layout_below="@id/imageSwitcher"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
➤ Java Code:
public class MainActivity extends AppCompatActivity {
ImageSwitcher imageSwitcher;
Button nextButton;
int index = 0;
int[] images = {R.drawable.a, R.drawable.b, R.drawable.c};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwitcher = findViewById(R.id.imageSwitcher);
nextButton = findViewById(R.id.nextButton);
imageSwitcher.setFactory(() -> {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
return imageView;
});
// Animation
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right));
// First image
imageSwitcher.setImageResource(images[index]);
nextButton.setOnClickListener(v -> {
index = (index + 1) % images.length;
imageSwitcher.setImageResource(images[index]);
});
}
}
Output:
When the app runs, the first image appears. On clicking "Next Image", the images cycle with
sliding animation.
Real-World Use Cases:
• Photo galleries or slideshows.
• Product previews in shopping apps.
• Step-by-step tutorials or onboarding screens.
Conclusion:
ImageSwitcher is a valuable UI component in Android for switching between images with
animations. By using a ViewFactory, it efficiently reuses views and supports smooth transitions,
improving the overall app experience.
Here's a complete 15-mark answer on Fragments, their differences from Activities, and the
Fragment Lifecycle, with detailed explanation, examples, and proper structure for exam
answers.
Q. What are Fragments in Android? Explain the lifecycle
of a Fragment and give any 3 differences between Fragment
and Activity. (15 Marks)
Definition of Fragment:
A Fragment is a reusable portion of the user interface in an Android Activity. It represents a
modular section of an Activity, which can be independently controlled and reused across
multiple activities.
Introduced in Android 3.0 (API 11), fragments are useful for multi-pane UI, such as on tablets,
or to create dynamic and flexible UI designs.
Why Use Fragments?
• To support flexible UIs for large and small screens.
• To create reusable UI components.
• To split complex UI logic into smaller manageable parts.
Example of Fragment Usage:
• One fragment shows a list of items.
• Another fragment displays details when an item is selected.
Fragment Lifecycle:
Fragments have their own lifecycle, but it's tied to the lifecycle of the host Activity.
Detailed Fragment Lifecycle Phases:
Phase Description
onAttach() Called when fragment is first attached to its host activity.
onCreate() Initialize essential components. No UI is created here.
onCreateView() Inflates the layout for the fragment (returns a View).
onViewCreated() Called after the view is created. Used for UI initialization.
onActivityCreated() Called when the host activity's onCreate() is done.
onStart() Fragment becomes visible to the user.
onResume() Fragment is interactive and in the foreground.
onPause() Called when fragment is partially hidden.
Phase Description
onStop() Called when fragment is completely hidden.
onDestroyView() Called when view is removed from the UI.
onDestroy() Cleanup any remaining resources.
onDetach() Fragment is detached from host activity.
Diagram: Fragment Lifecycle vs Activity Lifecycle
Activity: onCreate() → onStart() → onResume() → onPause() → onStop() →
onDestroy()
Fragment: onAttach() → onCreate() → onCreateView() → onStart() → onResume()
← onPause() ← onStop() ← onDestroyView() ← onDestroy()
← onDetach()
3 Key Differences Between Fragment and Activity:
Feature Fragment Activity
Modularity Can be combined within one Activity. Each Activity is standalone.
Lifecycle Tied to the host Activity’s lifecycle. Independent lifecycle.
Reuse Can be reused in multiple activities. Cannot be reused directly.
Simple Example Code (Fragment Layout + Java)
➤ Fragment XML (fragment_example.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello from Fragment!" />
</LinearLayout>
➤ Fragment Java Class
public class ExampleFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_example, container, false);
}
}
➤ Load Fragment in Activity
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentContainer, new ExampleFragment())
.commit();