Unit3 MAD

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

UNIT : 3

Designing by declaration:
In Android Studio, you can declare your layout in two ways:
 XML
Use Android Studio's main menu to create a layout XML file. The XML
vocabulary corresponds to View classes and subclasses, such as those for widgets
and layouts.
 Runtime
Create View and ViewGroup objects programmatically, and manipulate their
properties.

Here are some other declarations you can make in Android Studio:
 App permissions
Include the appropriate <uses-permission> element in your app's manifest file to
declare a permission that your app might request.
 Interfaces
Use the interface keyword to declare an interface, which provides total
abstraction. All methods in an interface are declared with an empty body and are
public, and all fields are public, static, and final by default.
 ID
Use the syntax form "@+id/name" for the ID value. The plus symbol indicates
that this is a new resource ID, and the aapt tool creates a new resource integer in
the R.java class, if it doesn't already exist.
You can declare your layout in two ways:
1. Declare UI elements in XML. Android provides a straightforward XML
vocabulary that corresponds to the View classes and subclasses, such as those for
widgets and layouts.
2. Instantiate layout elements at runtime.

Creating the opening screen:


A splash screen is mostly the first screen of the app when it is opened. It is a
constant screen that appears for a specific amount of time and generally shows
for the first time when the app is launched. Splash screen is used to display some
basic introductory information such as the company logo, content, etc just before
the app loads completely.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
Step 2: Working with the activity_main.xml file
Navigate to app > res > layout > activity_main.xml
Step 3: Creating a new activity
Navigate to app>java>your app’s package name> Right-click on
it>New>Activity>Empty Activity and specify name as MainActivity2.
Step 4: Working with the MainActivity file
Navigate to app > java > your app’s package name > MainActivity file
Step 5: Working with the activity_main2.xml file
Navigate to app>res>layout>activity_main2.xml

Step 6: Adding a style for MainActivity in the AndroidManifest.xml file


Navigate to app>AndroidManifest.xml file

Using alternative resources:


Android automatically applies alternative resources to an app based on the device
configuration when the resources are saved in directories with specific
qualifiers. When a resource is requested, Android checks for alternative resource
directories that contain the requested resource file and finds the best-matching
resource. If there are no alternative resources that match the device configuration,
Android uses the default resources.

Here are some common uses for alternative resources:


 Layout files: For different form factors, such as phone vs tablet, or different screen
orientations, such as portrait vs landscape
 String resources: For different languages, such as English vs Italian
 Drawable resources: For different screen densities
 Style resources: For different platform versions, such as Holo vs Material
To specify that a group of resources are for a specific configuration, add an
appropriate configuration qualifier to the directory name. For example, to create an
alternate layout based on the device configuration in Android Studio, you can tap
the icon, open your layout, and select Create landscape variation in the layout
directory and resources. You should always include a default resource in case the
system can't match any of the qualifiers, or your app may crash.
You can use Android Studio to add new resources and alternative resources in
several ways, depending on the type of resource you want to add. You can also use
the Resource Manager tool window to import, create, manage, and use resources in
your app.
Alternative Resources
1. Create a new directory in res/ named in the form <resources_name>-
<config_qualifier>. Here resources_name will be any of the resources mentioned
in the above table, like layout, drawable etc. ...
2. Save the respective alternative resources in this new directory.

Implementing an alert box:


Alert Dialog shows the Alert message and gives the answer in the form of yes or
no. Alert Dialog displays the message to warn you and then according to your
response, the next step is processed. Android Alert Dialog is built with the use of
three fields: Title, Message area, and Action Button.
Alert Dialog code has three methods:
 setTitle() method for displaying the Alert Dialog box Title
 setMessage() method for displaying the message
 setIcon() method is used to set the icon on the Alert dialog box.
Then we add the two Buttons, setPositiveButton and setNegativeButton to our
Alert Dialog Box as shown below.

Step By Step Implementation

Step 1: Create a New Project in Android Studio


Step 2: Working with the XML Files
Next, go to the activity_main.xml file, which represents the UI of the project.
Below is the code for the activity_main.xml file.

Step 3: Working with the MainActivity File


Go to the MainActivity File and refer to the following code. Below is the code for
the MainActivity File.
Applying Themes:

Here are some ways to apply themes in Android:


 Android Studio
You can customize the theme in Android Studio by selecting Tools, then Material
Theme Wizard. You can then choose from a variety of color combinations. You
can also change the theme in Android Studio by going to File, then Settings, then
Appearance and Behaviors, then Theme.
 Android app
You can change the theme for an entire app in the manifest file, or you can define
a specific theme for an activity. To define a theme for an activity, go to the
activity tag and add the property android:theme. Then, you can select a theme
from the options. When you apply a style definition as a theme, each view within
the activity or application will apply any properties it supports. Views that don't
support the properties will ignore them, and views that only support some of the
properties will only apply those.
 Google Chrome
You can change the theme in Google Chrome by opening the app, tapping More
Settings, then Theme. You can choose the System Default theme to use Chrome
in dark mode when Battery Saver mode is on or your device is set to dark mode

Styles and themes on Android let you separate the details of your app design from
the UI structure and behavior, similar to stylesheets in web design.

A style is a collection of attributes that specifies the appearance for a single View.
A style can specify attributes such as font color, font size, background color, and
much more.

A theme is a collection of attributes that's applied to an entire app, activity, or view


hierarchy—not just an individual view. When you apply a theme, every view in the
app or activity applies each of the theme's attributes that it supports. Themes can
also apply styles to non-view elements, such as the status bar and window
background.

Styles and themes are declared in a style resource file in res/values/, usually
named styles.xml.

Figure 1. Two themes applied to the same activity: Theme.AppCompat (left)


and Theme.AppCompat.Light (right).

Adding a Menu:
Here are some steps to add a menu in Android:
1. Open your Android project in Project mode
2. Go to app > src > main > res
3. Right-click on the res folder, then select New > Android Resource Directory
4. Select Menu from the Values drop-down section
5. Right-click on the menu folder, then select Menu Resource File
6. Name your menu file
7. Create a menu XML file
8. Add items to your menu
9. Create icons for your menu items
10.Inflate your menu resource
11.Detect user interaction
12.Respond to menu item selection

You can also create different types of menus, such as options menus, contextual
menus, popup menus, and menu groups.

Adding Settings:
Settings let users change the functionality and behavior of an app. Settings can
affect background behavior, such as how often the app synchronizes data with the
cloud, or they can be wider-reaching, such as changing the contents and
presentation of the user interface.
To integrate user configurable settings into your app, use the AndroidX Preference
library. This library manages the user interface and interacts with storage so that
you define only the individual settings that the user can configure. The library
comes with a Material Design theme that provides a consistent user experience
across devices and OS versions.
Step by step implementation

Step 1:

Create a Settings Activity in which will add our settings menu. To create a menu
and add items to it, first create a menu folder and add a menu resource file it.
Step 2:

After creating the menu, we need to override two methods,

In onCreateOptionsMenu method we inflate our settings_menu file in our Main


Activity and in the second method we setup an explicit intent to open up Settings
Activity.

Step 3:

Now we will set the Settings Activity as the child activity of the Main Activity by
making following changes in the Android Manifest file.

Up navigation

Up navigation, sometimes referred to as ancestral or logical navigation, is used to


navigate within an app based on the explicit hierarchical relationships between
screens. With Up navigation, your activities are arranged in a hierarchy, and
“child” activities show a left-facing arrow in the action bar that returns the user to
the “parent” activity.

Providing Up behavior for your app is optional, but a good design practice, to
provide consistent navigation for the activities in your app.

android:launchMode=“singleTop”: When returning from Settings Activity, it


does not recreates the instance of the activity instead calls the already existing.

Step 4:

For navigating back from the Settings Activity to Main Activity on pressing the
back button

Add following dependencies to your gradle file and sync it.


Create a Settings Fragment and extend it with PreferenceFragmentCompat and
override the onCreatePreferences method.

Step 5:

Next we will create a xml folder in res and add a settings_pref.xml to it. Here we
will create our settings menu.

Step 6:

Now to read from the Shared Preferences and making changes to our text, we will
make following changes to the MainActivity.java file.
Debugging with log messages:
You can use log messages to debug your Android app by viewing logs in real
time using the Logcat window in Android Studio. Each log message includes a
date, timestamp, process and thread ID, tag, package name, priority, and
message.
To view log messages, you can:
1. Build and run your app on a physical device or emulator
2. Select View > Tool Windows > Logcat from the menu bar
You can also create custom log messages in your app using the Log class
provided by Android. For example, you can use the following methods to
create different types of messages:
 Log.d(TAG, "message"): For debug messages
 Log.i(TAG, "message"): For informational messages
 Log.w(TAG, "message"): For warnings
 Log.e(TAG, "message"): For errors
You can replace "TAG" with a string identifier specific to your class or
component.

You can use the Logcat window to:


 View messages: See information, verbose messages, and errors
 Debug: Locate bugs even when you can't directly debug and suspend your code
 Collect messages: View messages thrown by an emulator to debug a crashed
application
To view logs in the Logcat window, you can:
1. Build and run your app on a physical device or emulator
2. Select View > Tool Windows > Logcat from the menu bar
3. Select your app as the debuggable process by the package name
4. Select Show only selected application to tell the logcat tab to log only for that app
Each log has the following information:
Date, Timestamp, Process and thread ID, Tag, Package name, Priority, and
Message.

Different tags have a unique color to help identify the type of log. Log entries
have a priority of:
FATAL, ERROR, WARNING, INFO, DEBUG, and VERBOSE.

Debugging with debugger


You can debug with a debugger in Android Studio or on a real device:
 Android Studio
You can start a debugging session using the Debug button or menu option. You can
also attach a debugger to a running app without restarting it by clicking Attach
Debugger to Android Process. Once you've attached the debugger, you can set
breakpoints, resume the program, and use buttons like Step Into, Step Over, and Step
Out. You can also inspect variables and expressions, view the system log, and more.
 Real device
You can debug an app on a real device using BrowserStack. You can sign up for a
free trial, upload your app, choose an Android device, and start debugging. You can
also enable USB debugging on your Android device by going to Settings > Developer
options and enabling the USB Debugging option.

Step by step procedure:


1. Attach the debugger to an app.
2. Launch an app with the debugger already attached.
3. Gain familiarity with the debugger pane.
4. Set a breakpoint.
5. Resume the program from the debugger.
6. Use the Step Into button.
7. Use the Step Over button.
8. Use the Step Out button.

You might also like