Android about screen example

Overview Screen

In this document

Key classes

Sample code

The overview screen (also referred to as the recents screen, recent task list, or recent apps) is a system-level UI that lists recently accessed activities and tasks. The user can navigate through the list and select a task to resume, or the user can remove a task from the list by swiping it away. With the Android 5.0 release (API level 21), multiple instances of the same activity containing different documents may appear as tasks in the overview screen. For example, Google Drive may have a task for each of several Google documents. Each document appears as a task in the overview screen.

Figure 1. The overview screen showing three Google Drive documents, each represented as a separate task.

Normally you should allow the system to define how your tasks and activities are represented in the overview screen, and you don’t need to modify this behavior. However, your app can determine how and and when activities appear in the overview screen. The ActivityManager.AppTask class lets you manage tasks, and the activity flags of the Intent class let you specify when an activity is added or removed from the overview screen. Also, the attributes let you set the behavior in the manifest.

Adding Tasks to the Overview Screen

Using the flags of the Intent class to add a task affords greater control over when and how a document gets opened or reopened in the overview screen. When you use the attributes you can choose between always opening the document in a new task or reusing an existing task for the document.

Using the Intent flag to add a task

When you create a new document for your activity, you call the startActivity() method of the ActivityManager.AppTask class, passing to it the intent that launches the activity. To insert a logical break so that the system treats your activity as a new task in the overview screen, pass the FLAG_ACTIVITY_NEW_DOCUMENT flag in the addFlags() method of the Intent that launches the activity.

Note: The FLAG_ACTIVITY_NEW_DOCUMENT flag replaces the FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET flag, which is deprecated as of Android 5.0 (API level 21).

If you set the FLAG_ACTIVITY_MULTIPLE_TASK flag when you create the new document, the system always creates a new task with the target activity as the root. This setting allows the same document to be opened in more than one task. The following code demonstrates how the main activity does this:

Note: Activities launched with the FLAG_ACTIVITY_NEW_DOCUMENT flag must have the android:launchMode=»standard» attribute value (the default) set in the manifest.

When the main activity launches a new activity, the system searches through existing tasks for one whose intent matches the intent component name and the Intent data for the activity. If the task is not found, or the intent contained the FLAG_ACTIVITY_MULTIPLE_TASK flag, a new task will be created with the activity as its root. If it finds one, it brings that task to the front and passes the new intent to onNewIntent() . The new activity gets the intent and creates a new document in the overview screen, as in the following example:

Using the activity attribute to add a task

An activity can also specify in its manifest that it always launches into a new task by using the attribute, android:documentLaunchMode . This attribute has four values which produce the following effects when the user opens a document with the application:

» intoExisting » The activity reuses an existing task for the document. This is the same as setting the FLAG_ACTIVITY_NEW_DOCUMENT flag without setting the FLAG_ACTIVITY_MULTIPLE_TASK flag, as described in Using the Intent flag to add a task, above. » always » The activity creates a new task for the document, even if the document is already opened. Using this value is the same as setting both the FLAG_ACTIVITY_NEW_DOCUMENT and FLAG_ACTIVITY_MULTIPLE_TASK flags. » none” » The activity does not create a new task for the document. The overview screen treats the activity as it would by default: it displays a single task for the app, which resumes from whatever activity the user last invoked. » never » The activity does not create a new task for the document. Setting this value overrides the behavior of the FLAG_ACTIVITY_NEW_DOCUMENT and FLAG_ACTIVITY_MULTIPLE_TASK flags, if either of these are set in the intent, and the overview screen displays a single task for the app, which resumes from whatever activity the user last invoked.

Читайте также:  Will rock для андроид

Note: For values other than none and never the activity must be defined with launchMode=»standard» . If this attribute is not specified, documentLaunchMode=»none» is used.

Removing Tasks

By default a document task is automatically removed from the overview screen when its activity finishes. You can override this behavior with the ActivityManager.AppTask class, with an Intent flag, or with an attribute.

You can always exclude a task from the overview screen entirely by setting the attribute, android:excludeFromRecents to true .

You can set the maximum number of tasks that your app can include in the overview screen by setting the attribute android:maxRecents to an integer value. The default is 16. When the maximum number of tasks is reached, the least recently used task is removed from the overview screen. The android:maxRecents maximum value is 50 (25 on low memory devices); values less than 1 are not valid.

Using the AppTask class to remove tasks

In the activity that creates a new task in the overview screen, you can specify when to remove the task and finish all activities associated with it by calling the finishAndRemoveTask() method.

Note: Using the finishAndRemoveTask() method overrides the use of the FLAG_ACTIVITY_RETAIN_IN_RECENTS tag, discussed below.

Retaining finished tasks

If you want to retain a task in the overview screen, even if its activity has finished, pass the FLAG_ACTIVITY_RETAIN_IN_RECENTS flag in the addFlags() method of the Intent that launches the activity.

To achieve the same effect, set the attribute android:autoRemoveFromRecents to false . The default value is true for document activities, and false for regular activities. Using this attribute overrides the FLAG_ACTIVITY_RETAIN_IN_RECENTS flag, discussed previously.

Источник

Screen Orientations in Android with Examples

Screen Orientation, also known as screen rotation, is the attribute of activity element in android. When screen orientation change from one state to other, it is also known as configuration change.

States of Screen orientation
There are various possible screen orientation states for any android application, such as:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

  • ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
  • ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
  • ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
  • ActivityInfo.SCREEN_ORIENTATION_USER
  • ActivityInfo.SCREEN_ORIENTATION_SENSOR
  • ActivityInfo.SCREEN_ORIENTATION_BEHIND
  • ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
  • ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
  • ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
  • ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT

The initial orientation of the Screen has to be defined in the AndroidManifest.xml file.

AndroidManifest.xml

How to change Screen orientation?
Here is an example of an Android application that changes screen orientation for Landscape and Portrait mode.

We will create two activities of different screen orientation.

  • The first activity will be as “portrait” orientation and
  • Second activity as “landscape” orientation state.

Источник

How to Code a Settings Screen in an Android App

The material design team at Google gives a clear definition of what settings in your Android app should do:

App settings let users indicate preferences for how an app should behave.

Google also states that your users should navigate to the app settings either from the side navigation or toolbar menu—with an item labeled Settings.

Including settings in your app gives your users the power to control some of the functionality of your app. This makes your users happy instead of angry—because they are now in control of how the app behaves.

It’s highly recommended to give access to app settings. This will provide a better user experience for your users, which leads to a better review on the Google Play store, which then eventually gives rise to a higher number of app downloads (which boost revenue).

I assume you must have interacted with an app’s settings on your device, for example by selecting a default ringtone, or by controlling your privacy on the app. Almost all of the most popular apps you’ve downloaded or will download on the Google Play store include a settings screen for you to control the behaviour of the application.

An example of a popular app that has a settings screen is the Chrome Android app from Google. In this app’s settings screen, users can choose the default search engine, change the notification behaviour, control user privacy, etc. You can see this yourself by downloading the Chrome app from the Google Play store (if you don’t already have it on your device). The following screenshot is from the Chrome app, displaying the app’s settings screen.

Читайте также:  Генератор речи для андроид

In this post, you’ll learn how to create an app settings screen from scratch and also how to read the values the user has selected from the app settings. For an additional bonus, you’ll also learn how to use the Android Studio templates feature to quickly bootstrap your project with a settings screen.

A sample project (in Kotlin) for this tutorial can be found on our GitHub repo so you can easily follow along.

Prerequisites

To be able to follow this tutorial, you’ll need:

  • A basic understanding of some Android APIs (such as SharedPreferences )
  • Android Studio 3.0 or higher
  • Kotlin plugin 1.1.51 or higher

1. Create an Android Studio Project

Fire up Android Studio and create a new project (you can name it SettingsScreenDemo ) with an empty activity called SettingsActivity . Make sure to also check the Include Kotlin support check box.

2. Creating a PreferenceFragment

To support API Level 11 (Honeycomb) and above, we can use the PreferenceFragment . This class is simply a Fragment that shows a hierarchy of Preference objects as lists.

In the code above, we created a nested class SettingsFragment inside SettingsActivity (because the SetttingsFragment class is so small). Note that our class SettingsFragment extends the PreferenceFragment superclass and has a method addPrerenceFromResource inside onCreate() . In this method, we gave it a resource ID R.xml.preference of the preference XML to launch—when the Fragment is loaded. Finally, we host the Fragment to the activity by simply using the FragmentTransaction to add it to the UI—inside onCreate() of SettingsActivity .

3. Creating Our Preferences

Create an XML file and name it preferences.xml. Save this file inside the res/xml directory in your app project. Note that you can give this file any name, but it’s highly recommended to stick with the conventional name «preferences». Also, you typically should have only one such file in an app project.

The root node for our preferences.xml file should be a

Other attributes you can add to a CheckBoxPreference are:

  • android:summaryOff : sets the summary for the Preference in a preference screen when it’s unchecked.
  • android:summaryOn : sets the summary for the Preference in a preference screen when it’s checked.
  • android:disableDependentsState : The state (true for on, or false for off) that causes dependents to be disabled. May be a boolean value, such as «true» or «false».

Switch Preference

SwitchPreference performs similar functionality to the CheckBoxPreference . It provides a two-state («on» or «off») toggleable option. This uses a Switch widget that allows the user to slide left («off») and right («on»). This Preference also includes the attributes described for the CheckBoxPreference above. In addition, it has the following attributes:

  • android:switchTextOff : sets the text used on the switch itself when in the «off» state.
  • android:switchTextOn : sets the text used on the switch itself when in the «on» state.

EditText Preference

This Preference , when clicked, shows a dialog for the user to enter an input text. It uses the EditText widget—including all the attributes of that widget that you’re already familiar with.

Note that the value stored in the SharedPreferences is a string.

List Preference

This kind of Preference will display a list of entries in a dialog when tapped. Here, you can specify a pair of string-array resources in your preference XML. This string-array resource simply contains a collection of strings. This resource is located at res/values/arrays.xml .

Here is our sample ListPreference using this resource.

We set the entries and entry values using the android:entries and android:entryValues attributes respectively.

  • android:entries : the human-readable array to present as a list.
  • android:entryValues : the array to find the value to save for a preference when an entry from entries is selected.

For example, if we use the number of minutes in each duration as the entry values, when the user picks a time duration (e.g. 30 minutes), the corresponding integer value will be stored in SharedPreferences (e.g. 30).

MultiSelect List Preference

This one is similar to ListPreference but instead of having radio buttons, we have check boxes. In other words, the user can select multiple items in the dialog. Note that the result is stored in a “string set” in the SharedPreferences . This can be retrieved using getStringSet() .

Ringtone Preference

When a RingtonePreference is tapped, a dialog is shown containing the list of available ringtones on the device or emulator.

  • android:showDefault : whether the Default ringtone option will be shown.
  • android:showSilent : whether a Silent option will be shown in the list. The user can select this option if they don’t want to play any ringtone.

Note that the value stored in the SharedPreferences for this preference is a special string. This special string is a URI which points to a ContentProvider .

Читайте также:  Умный будильник для android wear

4. Creating Setting Groups

It becomes a problem when you have a long list of preferences or settings, because users may have some trouble scanning or understanding them. To solve this problem, we can group our preferences. Take a look at the screenshot of the Chrome app I showed you in the beginning again—notice it grouped the preferences into two categories: Basics and Advanced. This makes it easier for the user to understand the preferences and to not make the list seem too overwhelming.

Let’s now look at how simple it is to perform this task.

We simply surround the preferences we want to group in a

Here, we added a element inside the

6. Binding the Preference Summary to the Value Selected

Let’s now see how to update the preference summary with the value selected by the user.

In this class, we created a helper method called bindPreferenceSummaryToValue() , which is inside our companion object, to update the preference summary text with the value the user has selected. We passed it a Preference object as an argument. The findPreference() will return a Preference using the Preference ‘s key.

We have a variable sBindPreferenceSummaryToValueListener that is an instance of Preference.OnPreferenceChangeListener . This is simply a preference change listener that will help us update the preference summary to the value the user has selected. We check for special cases, like when the selected preference is a RingtonePreference or a ListPreference . For those preference types, we do some special handling to get the summary string. If the preference is neither (like an EditTextPreference ), we just set the summary to the preference’s string value.

Inside the bindPreferenceSummaryToValue() , we set the preference change listener by calling onPreferenceChangeListener (in Java, it’s setOnPreferenceChangeListener instead) on the Preference object.

Now run the project again to see how it all works!

7. Retrieving Preference Values

To start getting preference values for the settings screen, we call getDefaultSharedPreference() which is inside the PreferenceManager class—passing it a Context object of the preferences whose values are wanted. Note that we get the values from the default SharedPreferences for our application.

You call the corresponding getter method for the type we want to retrieve the value from in SharedPreferences . You pass it the key as the first argument, and the default value is the second argument.

8. Bonus: Using Android Studio Templates

Now that you have learnt about the APIs involved to create a settings screen from scratch in Android, I’ll show you a shortcut that will make it quicker next time. You can choose to use a template instead of coding a settings screen from scratch.

Android Studio provides code templates that follow the Android design and development best practices. These existing code templates (available in Java and Kotlin) can help you quickly kick-start your project. One such template can be used to create a settings screen.

To use this handy feature for a new project, first fire up Android Studio.

Enter the application name and click the Next button. You can leave the defaults as they are in the Target Android Devices dialog.

Click the Next button again.

In the Add an Activity to Mobile dialog, scroll down and select Settings Activity. Click the Next button after that.

In the last dialog, you can rename the Activity name, layout name or title if you want. Finally, click the Finish button to accept all configurations.

Android Studio has now helped us to create a project with a settings activity. Really cool! You are strongly advised to explore the code generated.

You can use templates for an already existing Android Studio project too. Simply go to File > New > Activity > Settings Activity.

Note that the templates that come included with Android Studio are good for simple layouts and making basic apps, but if you want to really kick-start your app, you might consider some of the app templates available from Envato Market.

They’re a huge time saver for experienced developers, helping them to cut through the slog of creating an app from scratch and focus their talents instead on the unique and customised parts of creating a new app

Conclusion

In this tutorial, you learned how to create app settings in Android from scratch. We also explored how to easily and quickly use the Android Studio templates to create app settings.

I highly recommend checking out the official material design guidelines for settings to learn more about how to properly design and use settings in Android. Also, check out the official API guide to learn more about other APIs for creating a settings activity.

To learn more about coding for Android, check out some of our other courses and tutorials here on Envato Tuts+!

Источник

Оцените статью