Unit3 MAD
Unit3 MAD
Unit3 MAD
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.
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.
Styles and themes are declared in a style resource file in res/values/, usually
named styles.xml.
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:
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
Providing Up behavior for your app is optional, but a good design practice, to
provide consistent navigation for the activities in your app.
Step 4:
For navigating back from the Settings Activity to Main Activity on pressing the
back button
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.
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.