Unit - 2
Unit - 2
The use of hardware acceleration has additional requirements on Windows and Linux:
Intel processor on Windows or Linux: Intel processor with support for Intel VT-x, Intel EM64T (Intel
64), and Execute Disable (XD) Bit functionality
AMD processor on Linux: AMD processor with support for AMD Virtualization (AMD-V)
and Supplemental Streaming SIMD Extensions 3 (SSSE3)
AMD processor on Windows: Android Studio 3.2 or higher and Windows 10 April 2018 release or
higher for Windows Hypervisor Platform (WHPX) functionality
To work with Android 8.1 (API level 27) and higher system images, an attached webcam must
have the capability to capture 720p frames.
To add a file to the emulated device, drag the file onto the emulator screen. The file is placed in
the /sdcard/Download/ directory.
Controls for the device's location information are organized under two tabs: Single points and Routes.
Displays The emulator allows you to deploy your app to multiple displays, which support customizable
dimensions and can help you test apps that support multi- window and multi- display.
Cellular The emulator lets you simulate various network conditions. You can approximate the
network speed for different network protocols, or you can specify Full, which transfers data as
quickly as your computer allows. Specifying a network protocol is always slower than Full. You can
also specify the voice and data network status, such as roaming. The defaults are set in the AVD.
Battery You can simulate the battery properties of a device to see how your app performs under different
conditions. Like None, AC charger, Battery Health: Good , dead etc
Phone The emulator lets you simulate incoming phone calls and text messages.
Virtual sensors: Accelerometer, Ambient temperature: This environmental sensor measures ambient air
temperature.
Magnetic field: This position sensor measures the ambient magnetic field on the X, Y, and Z axes, respectively. The
values are in microteslas (μT).
Proximity: This position sensor measures the distance from an object; for example, it can notify a phone that a face
is close to it to make a call. The proximity sensor must be enabled in your AVD to use this control.
Light: This environmental sensor measures illuminance. The values are in lux units.
Pressure: This environmental sensor measures ambient air pressure. The values are in millibar (hPa) units.
Send keyboard shortcuts to: By default, some keyboard combinations will trigger emulator control shortcuts. If you’re
developing an app that includes keyboard shortcuts, such as one targeted at devices with Bluetooth keyboards, you
can change this setting to send all keyboard input to the virtual device, including input that would be a shortcut in the
emulator.
Screenshot save location: Click the folder icon to specify a location to save screenshots of the emulator screen.
Use detected ADB location: If you're running the emulator from Android Studio, you should select this setting (the
default). If you run the emulator from outside Android Studio and want it to use a specific adb executable, deselect
this option and specify the SDK Tools location. If this setting is incorrect, features such as screenshot capture and
drag-and-drop app installation won't work.
Show window frame around device: By default, emulators with device skin files are shown without a surrounding
window frame.
By default, the emulator uses the Android Studio HTTP proxy settings, but this screen allows you to manually define
an HTTP proxy configuration for the emulator. For more information, see Using the emulator with a proxy.
OpenGL ES renderer: Select the graphics acceleration type. (This is equivalant to the -gpu command line option).
Autodetect based on host: Let the emulator choose hardware or software graphics acceleration based on your
computer setup. It checks if your GPU driver matches a list of known faulty GPU drivers, and if it does, the emulator
disables graphics hardware emulation and instead uses the CPU.
ANGLE: (Windows only.) Use ANGLE Direct3D to render graphics in software. (Almost Native Graphics Layer
Engine)
OpenGL ES API level: Select the maximum version of OpenGL ES to use in the emulator.
Autoselect: Let the emulator choose the OpenGL ES version based on the host and guest support.
Renderer maximum (up to OpenGL ES 3.1): Attempt to use the maximum version of OpenGL ES.
Compatibility (OpenGL ES 1.1/2.0): Use the version of OpenGL ES that is compatible with most environments.
Limitations
The Android Emulator doesn't include virtual hardware for the following:
Bluetooth
NFC
SD card insert/eject
Device-attached headphones
USB
A client, which runs on your development machine. You can invoke a client from a shell by
issuing an adb command. Other Android tools such as the ADT plugin and DDMS also create
adb clients.
A server, which runs as a background process on your development machine. The server
manages communication between the client and the adb daemon running on an emulator or
device.
A daemon, which runs as a background process on each emulator or device instance
2. Scroll to the end : Click to jump to the bottom of the log and see the latest log messages. If you
then click a line in the log, the view pauses scrolling at that point.
3. Up the stack trace and Down the stack trace : Click to navigate up and down the stack
traces in the log, selecting the subsequent filenames (and viewing the correspnding line numbers in
the editor) that appear in the printed exceptions. This is the same behavior as when you click on a
filename in the log.
4. Use soft wraps : Click to enable line wrapping and prevent horizontal scrolling (though any
unbreakable strings will still require horizontal scrolling).
5. Print : Click to print the logcat messages. After selecting your print preferences in the dialog that
appears, you can also choose to save to a PDF.
6. Restart : Click to clear the log and restart logcat. Unlike the Clear logcat button, this recovers
and displays previous log messages, so is most useful if Logcat becomes unresponsive and you don't
want to lose your log messages.
7. Logcat header : Click to open the Configure Logcat Header dialog, where you can customize
the appearance of each logcat message, such as whether to show the date and time.
9. Screen record : Click to record a video of the device (for a maximum of 3 minutes).
Remember that logcat continues to collect all messages regardless of the log level setting. The
setting just determines what logcat displays.
It is a debugging tool from the Android software development kit (SDK). Able to monitor operations in the emulator as well as
real devices, DDMS reports the details of each processing thread and time spent whether in the app or Android OS.
The Android Profiler in Android Studio 3.0 and higher replaces the Android Monitor tools. The Android Profiler tools provide
real-time data to help you to understand how your app uses CPU, memory, network, and battery resources.
To open the Profiler window, choose View > Tool Windows > Profiler or click Profile in the toolbar. If
prompted by the Select Deployment Target dialog, select the device on which to profile your app. If you've
connected a device over USB but don't see it listed, ensure that you have enabled USB debugging. If you're using the
Android Emulator or a rooted device, the Android Profiler lists all running processes, even though they might not be
debuggable. When you launch a debuggable app, that process is selected by default.
Android Profiler continues to collect profiling data until you disconnect the device or click End Session.
1 Android Profiler shows the process and device currently being profiled.
2 In the Sessions pane, choose which session to view, or start a new profiling session.
3 Use the zoom buttons to control how much of the timeline to view, or use the Attach to live button to jump to the
real-time updates.
4 The event timeline shows events related to user input, including keyboard activity, volume control changes, and
screen rotations.
5 The shared timeline view, which includes graphs for CPU, memory, network, and energy usage.
This shared timeline view only shows the timeline graphs. To access the detailed profiling tools, click the graph that
corresponds to the performance data that you want to inspect. For example, to access tools to inspect the heap and
track memory allocations, click the Memory graph.
Not all profiling data is visible by default. If you see a message that says "Advanced profiling is unavailable for the
selected process," you can enable advanced profiling in your run configuration to view the additional data.
Sessions
You can save Profiler data as sessions, which are retained until you quit Android Studio. By recording profiling
information in multiple sessions and switching between them, you can compare resource usage in various scenarios.
Enable additional support for older devices (API level < 26)
To show you additional profiling data when running a device with Android 7.1 or lower, Android Studio must inject
monitoring logic into your compiled app.
3. Click the Profiling tab, and then check Enable additional support for older devices (API level < 26).
The standalone Android Studio Profilers allow you to profile your app without running the full Android Studio IDE.
1. Make sure the profiler is not currently running inside of Android Studio.
Windows/Linux: <studio-installation-folder>/bin
macOS: <studio-installation-folder>/Contents/bin
3. Depending on your OS, run profiler.exe or profiler.sh. The Android Studio splash screen appears.
4. Start the Android emulator or connect an Android device and wait for the home screen to load.
Build an intent
An Intent is an object that provides runtime binding between separate components, such as two activities.
The Intent represents an app’s intent to do something. You can use intents for a wide variety of tasks, but in this
lesson, your intent starts another activity.
<activity android:name=".SecondActivity"></activity>
switchActivities();
}) }
startActivity(switchActivityIntent) }
5. Create a back button on the new Activity and call the finish() method on an Activity when the back button
is pressed
UI screen components
A typical user interface of an android application consists of action bar and the application content area.
The basic unit of android application is the activity. A UI is defined in an xml file. During compilation, each element in
the XML is compiled into equivalent Android GUI class with attributes represented by methods.
View and ViewGroups
An activity is consist of views. A view is just a widget that appears on the screen. It could be button e.t.c. One or
more views can be grouped together into one GroupView. Example of ViewGroup includes layouts.
Types of layout
There are many types of layout. Some of which are listed below −
Linear Layout
Absolute Layout
Table Layout
Frame Layout
Relative Layout
Linear Layout
Linear layout is further divided into horizontal and vertical layout. It means it can arrange views in a single column or
in a single row. Here is the code of linear layout(vertical) that includes a text view.
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello” />
</LinearLayout>
AbsoluteLayout
The AbsoluteLayout enables you to specify the exact location of its children. It can be declared like this.
<AbsoluteLayout
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android” >
<Button
android:layout_width=”188dp”
android:layout_height=”wrap_content”
android:text=”Button”
android:layout_x=”126px”
android:layout_y=”361px” />
</AbsoluteLayout>
TableLayout
The TableLayout groups views into rows and columns. It can be declared like this.
<TableLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_height=”fill_parent”
android:layout_width=”fill_parent” >
<TableRow>
<TextView
android:text=”User Name:”
android:width =”120dp”
/>
<EditText
android:id=”@+id/txtUserName”
android:width=”200dp” />
</TableRow>
</TableLayout>
RelativeLayout
The RelativeLayout enables you to specify how child views are positioned relative to each other.It can be declared
like this.
<RelativeLayout
android:id=”@+id/RLayout”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android” >
</RelativeLayout>
FrameLayout
The FrameLayout is a placeholder on screen that you can use to display a single view. It can be declared like this.
<ImageView
android:src = “@drawable/droid”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
</FrameLayout>
Apart form these attributes, there are other attributes that are common in all views and ViewGroups. They are listed
below −
1
layout_width
2
layout_height
3
layout_marginTop
5
layout_marginLeft
6
layout_marginRight
7
layout_gravity
8
layout_weight
Specifies how much of the extra space in the layout should be allocated to the View
Units of Measurement
When you are specifying the size of an element on an Android UI, you should remember the following units of
measurement.
1
dp
2
sp
Scale-independent pixel. This is similar to dp and is recommended for specifying font sizes
3
pt
Point. A point is defined to be 1/72 of an inch, based on the physical screen size.
4
px
Screen Densities
1
Low density (ldpi)
120 dpi
2
Medium density (mdpi)
160 dpi
3
High density (hdpi)
240 dpi
4
Extra High density (xhdpi)
320 dpi
Optimizing layouts
dp or dip ( Density-independent Pixels) — an abstract unit that is based on the physical density of the screen.
These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will
change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both “dip” and
sp (Scaleable Pixels OR scale-independent pixels) — this is like the dp unit, but it is also scaled by the user’s font
size preference. It is recommended you use this unit when specifying font sizes, so they will be adjusted for both the
screen density and user’s preference. Note, the Android documentation is inconsistent on what sp actually stands for,
one doc says “scale-independent pixels”, the other says “scaleable pixels”.
Density Bucket -> Screen Display => Physical Size => Pixel Size
ldpi -> 120 dpi => 0.5 x 0.5 in => 0.5 in * 120 dpi = 60x60 px
mdpi -> 160 dpi => 0.5 x 0.5 in => 0.5 in * 160 dpi = 80x80 px
hdpi -> 240 dpi => 0.5 x 0.5 in => 0.5 in * 240 dpi = 120x120 px
xhdpi -> 320 dpi => 0.5 x 0.5 in => 0.5 in * 320 dpi = 160x160 px
xxhdpi -> 480 dpi => 0.5 x 0.5 in => 0.5 in * 480 dpi = 240x240 px
xxxhdpi -> 640 dpi => 0.5 x 0.5 in => 0.5 in * 640 dpi = 320x320 px