Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Android Studio 4.2 Development Essentials - Java Edition

You're reading from  Android Studio 4.2 Development Essentials - Java Edition

Product type Book
Published in Aug 2021
Publisher Packt
ISBN-13 9781803238814
Pages 782 pages
Edition 1st Edition
Languages
Author (1):
Neil Smyth Neil Smyth
Profile icon Neil Smyth

Table of Contents (87) Chapters

1. Introduction 2. Setting up an Android Studio Development Environment 3. Creating an Example Android App in Android Studio 4. Creating an Android Virtual Device (AVD) in Android Studio 5. Using and Configuring the Android Studio AVD Emulator 6. A Tour of the Android Studio User Interface 7. Testing Android Studio Apps on a Physical Android Device 8. The Basics of the Android Studio Code Editor 9. An Overview of the Android Architecture 10. The Anatomy of an Android Application 11. An Overview of Android View Binding 12. Understanding Android Application and Activity Lifecycles 13. Handling Android Activity State Changes 14. Android Activity State Changes by Example 15. Saving and Restoring the State of an Android Activity 16. Understanding Android Views, View Groups and Layouts 17. A Guide to the Android Studio Layout Editor Tool 18. A Guide to the Android ConstraintLayout 19. A Guide to Using ConstraintLayout in Android Studio 20. Working with ConstraintLayout Chains and Ratios in Android Studio 21. An Android Studio Layout Editor ConstraintLayout Tutorial 22. Manual XML Layout Design in Android Studio 23. Managing Constraints using Constraint Sets 24. An Android ConstraintSet Tutorial 25. A Guide to using Apply Changes in Android Studio 26. An Overview and Example of Android Event Handling 27. Android Touch and Multi-touch Event Handling 28. Detecting Common Gestures Using the Android Gesture Detector Class 29. Implementing Custom Gesture and Pinch Recognition on Android 30. An Introduction to Android Fragments 31. Using Fragments in Android Studio - An Example 32. Modern Android App Architecture with Jetpack 33. An Android Jetpack ViewModel Tutorial 34. An Android Jetpack LiveData Tutorial 35. An Overview of Android Jetpack Data Binding 36. An Android Jetpack Data Binding Tutorial 37. An Android ViewModel Saved State Tutorial 38. Working with Android Lifecycle-Aware Components 39. An Android Jetpack Lifecycle Awareness Tutorial 40. An Overview of the Navigation Architecture Component 41. An Android Jetpack Navigation Component Tutorial 42. An Introduction to MotionLayout 43. An Android MotionLayout Editor Tutorial 44. A MotionLayout KeyCycle Tutorial 45. Working with the Floating Action Button and Snackbar 46. Creating a Tabbed Interface using the TabLayout Component 47. Working with the RecyclerView and CardView Widgets 48. An Android RecyclerView and CardView Tutorial 49. A Layout Editor Sample Data Tutorial 50. Working with the AppBar and Collapsing Toolbar Layouts 51. An Android Studio Primary/Detail Flow Tutorial 52. An Overview of Android Intents 53. Android Explicit Intents – A Worked Example 54. Android Implicit Intents – A Worked Example 55. Android Broadcast Intents and Broadcast Receivers 56. A Basic Overview of Java Threads, Handlers and Executors 57. An Overview of Android Services 58. Implementing an Android Started Service – A Worked Example 59. Android Local Bound Services – A Worked Example 60. Android Remote Bound Services – A Worked Example 61. An Android Notifications Tutorial 62. An Android Direct Reply Notification Tutorial 63. Foldable Devices and Multi-Window Support 64. An Overview of Android SQLite Databases 65. The Android Room Persistence Library 66. An Android TableLayout and TableRow Tutorial 67. An Android Room Database and Repository Tutorial 68. Accessing Cloud Storage using the Android Storage Access Framework 69. An Android Storage Access Framework Example 70. Video Playback on Android using the VideoView and MediaController Classes 71. Android Picture-in-Picture Mode 72. An Android Picture-in-Picture Tutorial 73. Making Runtime Permission Requests in Android 74. Android Audio Recording and Playback using MediaPlayer and MediaRecorder 75. Working with the Google Maps Android API in Android Studio 76. Printing with the Android Printing Framework 77. An Android HTML and Web Content Printing Example 78. A Guide to Android Custom Document Printing 79. An Introduction to Android App Links 80. An Android Studio App Links Tutorial 81. A Guide to the Android Studio Profiler 82. An Android Biometric Authentication Tutorial 83. Creating, Testing and Uploading an Android App Bundle 84. An Overview of Android Dynamic Feature Modules 85. An Android Studio Dynamic Feature Tutorial 86. An Overview of Gradle in Android Studio Index

80. An Android Studio App Links Tutorial

The goal of this chapter is to provide a practical demonstration of both Android app links and the Android Studio App Link Assistant.

This chapter will add app linking support to an existing Android app, allowing an activity to be launched via an app link URL. In addition to launching the activity, the content displayed will be specified within the path of the URL.

80.1 About the Example App

The project used in this chapter is named AppLinking and is a basic app designed to allow users to find out information about landmarks in London. The app uses a SQLite database accessed through a standard Android content provider class. The app is provided with an existing database containing a set of records for some popular tourist attractions in London. In addition to the existing database entries, the app also lets the user add and delete landmark descriptions.

In its current form, the app allows the existing records to be searched and new records to be added and deleted.

The project consists of two activities named AppLinkingActivity and LandmarkActivity. AppLinkingActivity is the main activity launched at app startup. This activity allows the user to enter search criteria and to add additional records to the database. When a search locates a matching record, LandmarkActivity launches and displays the information for the related landmark.

...

80.2 The Database Schema

The data for the example app is contained within a file named landmarks.db located in the app -> assets –> databases folder of the project hierarchy. The database contains a single table named locations, the structure of which is outlined in Table 80-1:

80.3 Loading and Running the Project

The project is contained within the AppLinking folder of the sample source code download archive located at the following URL:

https://www.ebookfrenzy.com/retail/androidstudio42/index.php

Having located the folder, open it within Android Studio and run the app on a device or emulator. Once the app is launched, the screen illustrated in Figure 80-1 below will appear:

Figure 80-1

As currently implemented, landmarks are located using the ID for the location. The default database configuration currently contains two records referenced by the IDs “londonbridge” and “toweroflondon”. Test the search feature by entering londonbridge into the ID field and clicking the Find button. When a matching record is found, the second activity (LandmarkActivity) is launched and passed information about the record to be displayed. This information takes the form of extra data added to the Intent object. This information is...

80.4 Adding the URL Mapping

Now that the app has been loaded into Android Studio and tested, the project is ready for the addition of app link support. The objective is for the LandmarkActivity screen to launch and display information in response to an app link click. This is achieved by mapping a URL to LandmarkActivity. For this example, the format of the URL will be as follows:

http://<website domain>/landmarks/<landmarkId>

When all of the steps have been completed, the following URL should, for example, cause the app to display information for the Tower of London:

http://www.yourdomain.com/landmarks/toweroflondon

To add a URL mapping to the project, begin by opening the App Links Assistant using the Tools -> App Links Assistant menu option. Once open, the assistant should appear as shown in Figure 80-3:

Figure 80-3

Click on the Open URL Mapping Editor button to begin mapping a URL to an activity. Within the mapping screen, click on the ‘...

80.5 Adding the Intent Filter

As explained in the previous chapter, an intent filter is needed to allow the target activity to be launched in response to an app link click. In fact, when the URL mapping was added, the intent filter was automatically added to the project manifest file. With the URL mapping selected in the App Links Assistant URL mapping list, scroll down the screen until the intent filter Preview section comes into view. The preview should contain the modified AndroidManifest.xml file with the newly added intent filters included:

Figure 80-7

80.6 Adding Intent Handling Code

The steps taken so far ensure that the correct activity is launched in response to an appropriately formatted app link URL. The next step is to handle the intent within the LandmarkActivity class so that the correct record is extracted from the database and displayed to the user. Before making any changes to the code within the LandmarkActivity.java file, it is worthwhile reviewing some areas of the existing code. Open the LandmarkActivity.java file in the code editor and locate the onCreate() and handleIntent() methods which should currently read as follows:

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

 

    binding = ActivityLandmarkBinding.inflate(getLayoutInflater());

    View view = binding.getRoot();

    setContentView(view);

 

    handleIntent(getIntent...

80.7 Testing the App Link

Test that the intent handling works by selecting a suitable device or emulator in the toolbar as the deployment target, returning to the App Links Assistant panel and clicking on the Test App Links button. When prompted for a URL to test, enter the URL (using the domain referenced in the app link mapping) for the londonbridge landmark ID before clicking on the Run Test button:

Figure 80-9

Before the activity appears, it is likely that Android will display a panel (Figure 80-10) within which a choice needs to be made as to how the app link is to be handled:

Figure 80-10

Until the app link has been associated with a web site, Android will display this selection panel every time the activity is launched using a View intent action unless the user selects the Always option.

80.8 Associating an App Link with a Web Site

As outlined in the previous chapter, an app link may be associated with a web site by creating a Digital Asset Links file and installing it on the web site. Although the steps to generate this file will be covered in this chapter, it will only be possible to test these instructions using your own app (with a unique application ID) and if you have access to an https based web server onto which the assets file can be installed.

To generate the Digital Asset Links file, display the App Links Assistant and click on the Open Digital Asset Links File Generator button. This will display the panel shown in Figure 80-11:

Figure 80-11

Enter the URL of the site onto which the assets file is to be uploaded and verify that the application ID matches the package name. Choose either a keystore file containing the SHA signing key for your project, or use the menu to select either the release or debug signing configuration as used by Android...

80.9 Summary

This chapter has demonstrated the steps involved in implementing App Link support within an Android app project. Areas covered in this chapter include the use of the App Link Assistant in Android Studio, App Link URL mapping, intent filters, handling website association using Digital Asset File entries and App Link testing.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Android Studio 4.2 Development Essentials - Java Edition
Published in: Aug 2021 Publisher: Packt ISBN-13: 9781803238814
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime}

Column

Type

Description

_id

String

The primary index, this column contains string values that uniquely identify the landmarks in the database.

Title

String

The name of the landmark (e.g. London Bridge).

description

String

A description of the landmark.

personal

Boolean

Indicates whether the record is personal...