Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Android 9 Development Cookbook
Android 9 Development Cookbook

Android 9 Development Cookbook: Over 100 recipes and solutions to solve the most common problems faced by Android developers , Third Edition

eBook
€8.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Android 9 Development Cookbook

Layouts

In this chapter, we will cover the following topics:

  • Defining and inflating a layout
  • Using RelativeLayout
  • Using LinearLayout
  • Creating tables—TableLayout and GridLayout
  • RecyclerView replaces ListView
  • Changing layout properties during runtime

Introduction

In Android, the user interface is defined in a layout. A layout can be declared in XML or created dynamically in code. (It's recommended to declare the layout in XML rather than in code to keep the presentation layer separate from the implementation layer.) A layout can define an individual ListItem, a fragment, or even the entire activity. Layout files are stored in the /res/layout folder and referenced in code with the following identifier: R.layout.<filename_without_extension>.

Android provides a useful variety of Layout classes that contain and organize individual elements of an activity (such as buttons, checkboxes, and other Views). The ViewGroup object is a container object that serves as the base class for Android's family of Layout classes. The Views placed in a layout form a hierarchy, with the topmost layout being the parent.

Android...

Defining and inflating a layout

When using the Android Studio wizard to create a new project, it automatically creates the res/layout/activity_main.xml file (as shown in the following screenshot). It then inflates the XML file in the onCreate() callback with setContentView(R.layout.activity_main):

For this recipe, we will create two slightly different layouts and switch between them with a button.

Getting ready

Create a new project in Android Studio and call it InflateLayout. Once the project is created, expand the res/layout folder so we can edit the activity_main.xml file. Use the default Phone & Tablet settings on the Target Android devices and select Empty Activity on the Add...

Using RelativeLayout

As mentioned in the Introduction section, RelativeLayout allows Views to be position-relative to each other and the parent. RelativeLayout is particularly useful for reducing the number of nested layouts, which is very important for reducing memory and processing requirements.

Getting ready

Create a new project and call it RelativeLayout. Android Studio defaults to using a ConstraintLayout , which we will replace with a RelativeLayout for this example. Use the default Phone & Tablet settings on the Target Android devices and select Empty Activity on the Add an Activity to Mobile dialog.

...

Using LinearLayout

Another common layout option is LinearLayout, which arranges the child Views in a single column or single row, depending on the orientation specified. The default orientation (if not specified) is vertical, which aligns the Views in a single column.

LinearLayout has a key feature not offered in RelativeLayout—the weight attribute. We can specify a layout_weight parameter when defining a View to allow the View to dynamically size based on the available space. Options include having a View fill all the remaining space (if a View has a higher weight), having multiple Views fit within the given space (if all have the same weight), or spacing the Views proportionally by their weight.

We will create LinearLayout with three EditText Views to demonstrate how the weight attribute can be used. For this example, we will use three EditText Views-one to enter...

Creating tables – TableLayout and GridLayout

When you need to create a table in your UI, Android provides two convenient layout options: TableLayout (along with TableRow) and GridLayout (added in API 14). Both layout options can create similar-looking tables, but each using a different approach. With TableLayout, rows and columns are added dynamically as you build the table. With GridLayout, row and column sizes are defined in the layout definition.

Neither layout is better, it's just a matter of using the best layout for your needs. We'll create a 3 x 3 grid using each layout to give a comparison, as you could easily find yourself using both layouts, even within the same application.

Getting ready

To stay...

RecyclerView replaces ListView

As the name implies, ListView is designed for displaying lists of information.  If you have prior experience on Android, you’ve probably come across the ListView and possibly GridView controls before. If not while coding, most likely you’ve used it as an app, as it’s one of the most commonly used controls available. For most applications and users, the old ListView was probably sufficient and didn’t pose any problems. As an example, most users could probably see their list of emails in their inbox without any problems.  But for some, they might have so many emails in their inbox that when scrolling through their list, their device would stutter (slight pauses when scrolling). Unfortunately, ListView has many such performance problems.

The most significant performance issue with ListView is caused by creating...

Changing layout properties during runtime

In Android development, it's generally the preferred practice to define the UI with XML and the application code in Java, keeping the user interface code separate from the application code. There are times where it is much easier or more efficient, to alter (or even build) the UI from the Java code. Fortunately, this is easily supported in Android.

In this recipe, we will obtain a reference to the LayoutParams object to change the margin during runtime.

Getting ready

Here, we will set up a simple layout with XML and use a LinearLayout.LayoutParams object to change the margins of a View during runtime.  Create a new project using with an Empty Activity called RuntimeProperties...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Uncover the latest features in Android 9 Pie to make your applications stand out
  • Develop Android Pie applications with the latest mobile technologies, from set up to security
  • Get up-to-speed with Android Studio 3 and its impressive new features

Description

The Android OS has the largest installation base of any operating system in the world. There has never been a better time to learn Android development to write your own applications, or to make your own contributions to the open source community! With this extensively updated cookbook, you'll find solutions for working with the user interfaces, multitouch gestures, location awareness, web services, and device features such as the phone, camera, and accelerometer. You also get useful steps on packaging your app for the Android Market. Each recipe provides a clear solution and sample code you can use in your project from the outset. Whether you are writing your first app or your hundredth, this is a book that you will come back to time and time again, with its many tips and tricks on the rich features of Android Pie.

Who is this book for?

If you are new to Android development and want to take a hands-on approach to learning the framework, or if you are an experienced developer in need of clear working code to solve the many challenges in Android development, you will benefit from this book. Either way, this is a resource you'll want to keep on your desk as a quick reference to help you solve new problems as you tackle more challenging projects.

What you will learn

  • Develop applications using the latest Android framework while maintaining backward-compatibility with the support library
  • Create engaging applications using knowledge gained from recipes on graphics, animations, and multimedia
  • Work through succinct steps on specifics that will help you complete your project faster
  • Add location awareness to your own app with examples using the latest Google Play services API
  • Utilize Google Speech Recognition APIs for your app

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 19, 2018
Length: 464 pages
Edition : 3rd
Language : English
ISBN-13 : 9781788991216
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Oct 19, 2018
Length: 464 pages
Edition : 3rd
Language : English
ISBN-13 : 9781788991216
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 99.97
Android Programming with Kotlin for Beginners
€29.99
Android 9 Development Cookbook
€32.99
Android Programming for Beginners
€36.99
Total 99.97 Stars icon
Banner background image

Table of Contents

17 Chapters
Activities Chevron down icon Chevron up icon
Layouts Chevron down icon Chevron up icon
Views, Widgets, and Styles Chevron down icon Chevron up icon
Menus and Action Mode Chevron down icon Chevron up icon
Fragments Chevron down icon Chevron up icon
Home Screen Widgets, Search, and the System UI Chevron down icon Chevron up icon
Data Storage Chevron down icon Chevron up icon
Alerts and Notifications Chevron down icon Chevron up icon
Using the Touchscreen and Sensors Chevron down icon Chevron up icon
Graphics and Animation Chevron down icon Chevron up icon
A First Look at OpenGL ES Chevron down icon Chevron up icon
Multimedia Chevron down icon Chevron up icon
Telephony, Networks, and the Web Chevron down icon Chevron up icon
Location and Using Geofencing Chevron down icon Chevron up icon
Getting Your App Ready for the Play Store Chevron down icon Chevron up icon
Getting Started with Kotlin Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.4
(9 Ratings)
5 star 55.6%
4 star 0%
3 star 11.1%
2 star 0%
1 star 33.3%
Filter icon Filter
Top Reviews

Filter reviews by




Christopher Adamson Jan 26, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I like the cookbook style from Packt, and it makes sense for Android development in general. It was a good read.
Amazon Verified review Amazon
Josh May 07, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I'm very impressed with this book. The author gets straight to the point, and covers plenty of information without covering too much or too little. I highly recommend this book.
Amazon Verified review Amazon
Sand George Ionut Nov 05, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
good
Amazon Verified review Amazon
Paul Plachetka Aug 06, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Das Buch ist äußerst einfach verständlich für Leute die nicht gerade neu in Java sind und komplett von vorn mit Android Development beginnen wollen. Auch wenn es um Android 9 geht, ist es ziemlich aktuell, in Android 10 ist jetzt nicht sehr viel dazu gekommen. Die Kapitel sind von Einführung, über erste Elemente und Konzepte zu komplexeren Techniken geordnet und sind gut verständlich. Ich bin zwar noch nicht durch mit dem Buch, dennoch lässt sich auch bedingt durch die simple IDE, die Einem schon viel abnimmt, sehr viel selbst anfangen. Das einzige Manko das ich hätte, wäre, dass die Bebilderung in Schwarz-Weiß ist und die IDE im Dark Mode abbildet, was das erkennen von vermeintlich gemeinten Details in der IDE sehr schwer macht.
Amazon Verified review Amazon
Ein Kunde Jul 31, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Ich versuche bereits seit etwa zwei Jahren den Einstieg in die Android-Programmierung zu finden. Angeblich soll die Android-Programmierung mit Android-Studio schwierig und komplex sein. Um die Programmierung zu lernen habe ich mich durch ein relativ gut bewertetes deutschsprachiges Buch bis auf Seite 153 gequält und trotz mehrfacher Anläufe, Wiederholung der bereits gelesenen Abschnitte, markieren mit einem Farbmarker etc. einfach kein Bein auf den Boden gekriegt. Auch die Bewertungen der anderen deutschsprachigen Bücher ließen nichts besseres erwarten.Nun habe ich mir dieses englischsprachige Buch besorgt und in knapp einer Stunde die ersten vier "Programmierrezepte" durchprobiert. Der Lernerfolg kann einfach nur als gigantisch bezeichnet werden. Ich habe in dieser einen Stunde mehr gelernt, als in den vielen, vielen frustrierenden Stunden mit meinem deutschen Buch.Das Buch ist hierbei so aufgebaut, dass dieses viele kleine Programmbeispiele aufweist, die teilweise aufeinander aufbauen. Die Programmierbeispiele sind kurz und übersichtlich und enthalten jeweils eine Schritt für Schritt-Anleitung mit allen wichtigen Schritten. Es setzt die Strategie "Teile und Herrsche" perfekt um. Durch die Kürze der Beispiele ergibt sich ein phänomenaler Lerneffekt, weil extrem prägnant die tatsächlich nicht ganz trivialen Zusammenhänge zwischen den einzelnen Dateien eines Android Studio Projektes herausgearbeitet werden.Ob das Buch etwas für komplette Programmieranfänger ist, kann ich nicht beurteilen. Wenn ihr aber schon etwas Programmiererfahrung habt und Euch englisch keine Probleme bereitet, kann ich Euch das Buch wärmstens empfehlen!Um Euch selbst ein Bild zu machen empfehle ich Euch die ersten beiden Programmierrezepte einfach mal selber auszuprobieren. Die ersten Seiten des Buches mit den ersten beiden Programmierrezepten sind bei Amazon online verfügbar.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.