0% found this document useful (0 votes)
10 views12 pages

Mad Questions.

The document provides an overview of advanced Android components including ListFragments, Dialogs, Storage, and more. It covers the creation and management of custom ListFragments, DialogFragments, PreferenceFragments, and various UI elements like ImageView and GridView, along with examples and code snippets. Additionally, it discusses data storage options such as SharedPreferences, file systems, and SQLite databases, including how to implement them in Android applications.

Uploaded by

bb2345469
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

Mad Questions.

The document provides an overview of advanced Android components including ListFragments, Dialogs, Storage, and more. It covers the creation and management of custom ListFragments, DialogFragments, PreferenceFragments, and various UI elements like ImageView and GridView, along with examples and code snippets. Additionally, it discusses data storage options such as SharedPreferences, file systems, and SQLite databases, including how to implement them in Android applications.

Uploaded by

bb2345469
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Advanced Android Components: ListFragments, Dialogs, Storage & More

1. Discuss how to create a custom ListFragment and handle item selection.

To create a custom ListFragment in Android, extend the `ListFragment` class, set an adapter in `onActivityCreated()`,

and override `onListItemClick()` to handle selection events. ListFragment simplifies list-related operations by

automatically providing a ListView. Its ideal for reusable UI components.

Example:

```java

public class CustomListFragment extends ListFragment {

@Override

public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

List<String> fruits = Arrays.asList("Apple", "Banana", "Mango");

ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, fruits);

setListAdapter(adapter);

@Override

public void onListItemClick(ListView l, View v, int position, long id) {

String item = (String) getListAdapter().getItem(position);

Toast.makeText(getActivity(), "Clicked: " + item, Toast.LENGTH_SHORT).show();

```

2. Explain DialogFragment along with its attributes and features. Give an example.
Advanced Android Components: ListFragments, Dialogs, Storage & More

A DialogFragment is a fragment that displays a dialog window floating over the current activity. It helps manage dialog

lifecycle events such as orientation changes more reliably than `AlertDialog`.

Features include customization, reusability, and integration with FragmentManager.

Example:

```java

public class MyDialogFragment extends DialogFragment {

@Override

public Dialog onCreateDialog(Bundle savedInstanceState) {

return new AlertDialog.Builder(getActivity())

.setTitle("Confirm")

.setMessage("Are you sure?")

.setPositiveButton("Yes", null)

.setNegativeButton("No", null)

.create();

```

3. Discuss how to implement a custom DialogFragment and handle user interactions. Provide

a detailed code example.

You can design a custom DialogFragment layout using XML and inflate it within the fragment class. Use setView() in

AlertDialog.Builder to apply the custom layout, and set listeners to handle input or button actions.
Advanced Android Components: ListFragments, Dialogs, Storage & More

Example:

```java

public class CustomDialogFragment extends DialogFragment {

@Override

public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

LayoutInflater inflater = requireActivity().getLayoutInflater();

View view = inflater.inflate(R.layout.dialog_custom, null);

builder.setView(view)

.setTitle("Login")

.setPositiveButton("Submit", (dialog, id) -> {

EditText username = view.findViewById(R.id.username);

// Handle input

})

.setNegativeButton("Cancel", null);

return builder.create();

```

4. Explain PreferenceFragment along with its attributes and features. Give an example.

PreferenceFragment is used to create a settings UI from an XML resource. It automatically renders preference widgets

and manages user settings storage.


Advanced Android Components: ListFragments, Dialogs, Storage & More

Example:

```java

public class SettingsFragment extends PreferenceFragmentCompat {

@Override

public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

setPreferencesFromResource(R.xml.preferences, rootKey);

```

5. Create and manage a settings screen using PreferenceFragment. Provide a detailed code

example.

To create a settings screen, define preferences in an XML resource and load them in a PreferenceFragmentCompat.

Display this fragment using a FragmentTransaction.

XML (res/xml/preferences.xml):

```xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<EditTextPreference android:key="username" android:title="Username" android:defaultValue="User" />

</PreferenceScreen>

```

Activity:

```java

getSupportFragmentManager()
Advanced Android Components: ListFragments, Dialogs, Storage & More

.beginTransaction()

.replace(android.R.id.content, new SettingsFragment())

.commit();

```

6. Explain ImageView along with its attributes and features. Give an example.

ImageView is a UI element that displays images in Android. Key attributes include `src`, `scaleType`, and

`adjustViewBounds`. It can display drawable resources or remote images using libraries like Glide.

Example:

```java

ImageView imageView = findViewById(R.id.imageView);

imageView.setImageResource(R.drawable.sample);

```

7. Explain GridView along with its attributes and features. Give an example.

GridView shows items in a two-dimensional, scrollable grid. Attributes include `numColumns`, `verticalSpacing`, and

`stretchMode`.

Example:

```java

GridView gridView = findViewById(R.id.grid);

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data);

gridView.setAdapter(adapter);

```
Advanced Android Components: ListFragments, Dialogs, Storage & More

8. Create a custom GridView adapter and handle item clicks. Provide a detailed code

example.

To customize GridView, extend `BaseAdapter` and override required methods. Set the adapter to GridView and use

`setOnItemClickListener()` for clicks.

Example:

```java

public class ImageAdapter extends BaseAdapter {

private Context context;

private int[] images;

public ImageAdapter(Context c, int[] imgs) {

context = c;

images = imgs;

public View getView(int position, View convertView, ViewGroup parent) {

ImageView imageView = new ImageView(context);

imageView.setImageResource(images[position]);

return imageView;

public int getCount() { return images.length; }

public Object getItem(int position) { return null; }

public long getItemId(int position) { return 0; }

```
Advanced Android Components: ListFragments, Dialogs, Storage & More

9. Explain ImageSwitcher along with its attributes and features. Give an example.

ImageSwitcher is a specialized ViewSwitcher to animate transitions between images. You define a ViewFactory to

create ImageViews and use setImageResource() to update images.

Example:

```java

imageSwitcher.setFactory(() -> new ImageView(getApplicationContext()));

imageSwitcher.setImageResource(R.drawable.image1);

```

10. Create an image slideshow using ImageSwitcher. Provide a detailed code example.

To make a slideshow, configure ImageSwitcher with animations and update images at intervals or on button click.

Example:

```java

imageSwitcher.setFactory(() -> new ImageView(this));

imageSwitcher.setInAnimation(this, android.R.anim.fade_in);

imageSwitcher.setOutAnimation(this, android.R.anim.fade_out);

imageSwitcher.setImageResource(images[currentIndex]);

```

11. Explain WebView along with its attributes and features. Give an example.

WebView is a view for displaying web pages. Enable JavaScript using `getSettings().setJavaScriptEnabled(true)`. It

allows in-app web content rendering.


Advanced Android Components: ListFragments, Dialogs, Storage & More

Example:

```java

WebView webView = findViewById(R.id.webview);

webView.getSettings().setJavaScriptEnabled(true);

webView.loadUrl("https://www.google.com");

```

12. Create a hybrid application using WebView and handle interactions. Provide a detailed

code example.

A hybrid app combines native Android with web features using WebView. Use JavaScriptInterface to call native code.

Example:

```java

webView.getSettings().setJavaScriptEnabled(true);

webView.addJavascriptInterface(new WebAppInterface(this), "Android");

webView.loadUrl("file:///android_asset/www/index.html");

```

13. Explain SharedPreferences along with its attributes and features. Give an example.

SharedPreferences is used to store simple key-value data. Its suitable for user settings and small data.

Example:

```java

SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);

SharedPreferences.Editor editor = prefs.edit();


Advanced Android Components: ListFragments, Dialogs, Storage & More

editor.putString("username", "John");

editor.apply();

```

14. Create a preference management system using SharedPreferences. Provide a detailed

code example.

You can wrap SharedPreferences in a custom manager class to simplify usage across the app.

Example:

```java

public class PreferenceManager {

private SharedPreferences prefs;

public PreferenceManager(Context context) {

prefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE);

public void saveUser(String username) {

prefs.edit().putString("user", username).apply();

public String getUser() {

return prefs.getString("user", "default");

```

15. Use of File Systems in Android with example.


Advanced Android Components: ListFragments, Dialogs, Storage & More

Android supports internal and external storage. Internal storage is private to the app. External is public and requires

permissions. Files can be read/written using FileInputStream and FileOutputStream.

Example:

```java

FileOutputStream fos = openFileOutput("data.txt", Context.MODE_PRIVATE);

fos.write("Hello".getBytes());

fos.close();

```

16. File reading/writing in internal and external storage. Provide code.

Internal:

```java

FileOutputStream fos = openFileOutput("data.txt", Context.MODE_PRIVATE);

fos.write("Hello".getBytes());

fos.close();

```

External:

```java

File file = new File(getExternalFilesDir(null), "demo.txt");

FileOutputStream fos = new FileOutputStream(file);

fos.write("External data".getBytes());

fos.close();

```
Advanced Android Components: ListFragments, Dialogs, Storage & More

17. Explain SQLite databases and features. Give an example.

SQLite is a lightweight database embedded in Android. Supports SQL syntax, transactions, and multiple tables. Data is

persistent.

Example:

```java

SQLiteDatabase db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);

db.execSQL("CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT)");

db.execSQL("INSERT INTO users(name) VALUES('Alice')");

```

18. Manage SQLite database with multiple tables and relationships. Provide code.

Use SQLiteOpenHelper to manage complex database structures. You can define foreign keys and handle upgrades.

Example:

```java

public class DBHelper extends SQLiteOpenHelper {

public DBHelper(Context context) {

super(context, "MyDB", null, 1);

public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT)");

db.execSQL("CREATE TABLE orders(id INTEGER PRIMARY KEY, userId INTEGER, item TEXT, FOREIGN

KEY(userId) REFERENCES users(id))");

}
Advanced Android Components: ListFragments, Dialogs, Storage & More

```

You might also like