Movatterモバイル変換


[0]ホーム

URL:


Packt
Search iconClose icon
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.
Profile icon
Account
Close icon

Change country

Modal Close icon
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timerSALE ENDS IN
0Days
:
00Hours
:
00Minutes
:
00Seconds
Home> Mobile> Cross Platform Mobile Development> Android Programming for Beginners
Android Programming for Beginners
Android Programming for Beginners

Android Programming for Beginners: Build in-depth, full-featured Android apps starting from zero programming experience , Third Edition

Arrow left icon
Profile Icon John Horton
Arrow right icon
€23.99€26.99
Full star iconFull star iconFull star iconFull star iconHalf star icon4.2(12 Ratings)
eBookApr 2021742 pages3rd Edition
eBook
€23.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€23.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature iconInstant access to your Digital eBook purchase
Product feature icon Download this book inEPUB andPDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature iconDRM FREE - Read whenever, wherever and however you want
Product feature iconAI Assistant (beta) to help accelerate your learning
OR

Contact Details

Modal Close icon
Payment Processing...
tickCompleted

Billing Address

Table of content iconView table of contentsPreview book icon Preview Book

Android Programming for Beginners

Chapter 2: First Contact: Java, XML, and the UI Designer

At this stage, we have a working Android development environment and we have built and deployed our first app. It is obvious, however, that the auto-generated code from Android Studio is not going to make the next top-selling app on Google Play. We need to explore this auto-generated code so we can begin to understand Android and then learn how to build upon this useful template. With this aim in mind, in this chapter, we will do the following:

  • See how to get technical feedback from our apps
  • Examine the Java code and UI XML code from our first app
  • Get our first taste of using the Android StudioUser Interface(UI) designer
  • Learn some core Java fundamentals and how they relate to Android
  • Write our first Java code

First, let's see how to get feedback from our apps.

Technical requirements

Examining the logcat output

In the previous chapter, we mentioned that our app was running in debug mode on the emulator or real device, so we can monitor it and get feedback when things go wrong. So, where is all this feedback then?

You might have noticed a whole load of scrolling text at the bottom of the Android Studio window. If not, click on theLogcat tab, as shown by the highlighted area labeled1 in the next figure:

Note

The emulator must be running, or a real device must be attached in debugging mode, for you to see the following window. Furthermore, if you restarted Android Studio for some reason and have not executed the app since restarting, then theLogcat window will be empty. Refer to the first chapter to get the app running on an emulator or a real device.

Figure 2.1 – The Logcat tab

Figure 2.1 – The Logcat tab

You can drag the window to make it taller, just like you can in most other Windows applications if you want to see more.

This window is calledlogcat or is sometimes referred to as theconsole. It is our app's way of telling us what is going on underneath what the user sees. If the app crashes or has errors, the reason orclues to the reason will appear here. If we need to output debugging information, we can do so here as well.

Note

The app we are building should not have any problems at this stage, but in the future, if you just cannot work out why your app is crashing, copying and pasting a bit of text from logcat into Google will often reveal the reason.

Filtering the logcat output

You might havenoticed that most if not all of the contents of logcat is almost unintelligible. That's OK. For now, we are only interested in errors, which will be highlighted in red, and the debugging information, about which we will learn next. So that we see less of the unneeded text in our logcat window, we can turn on some filters to make things clearer.

In the previous figure, I highlighted two more areas, as2 and3. Area2 is a drop-down list that controls the first filter. Left-click it now and change it fromVerbosetoInfo. We have cut down the text output significantly. We will see how this is useful when we have made some changes to our app and redeployed it. We will do this after we have explored the code and the assets that make up our project. Also, double-check in the area that is labeled3 that saysShow only the selected application. If it doesn't, left-click on it and change it toShow only the selected application now.

Now we can look at what Android Studio automatically generated for us and then we can set about changing and adding to the code to personalize it beyond what we got from the project creation phase.

Exploring the project Java and the main layout XML

We are going to look at the resource files that have the code that defines our simple UI layout and the file that has our Java code. At this stage, we will not try to understand it all, as we need to learn some more basics before it makes sense to do so. What we will see, however, is the basic contents and structure of both files, so we can reconcile their contents with what we already know about Android resources and Java.

Examining the MainActivity.java file

Let's look at the Java code first. If, for some reason, this isn't currently visible, you can see this code by left-clicking on theMainActivity.java tab, as shown in the next figure:

Figure 2.2 – MainActivity.java tab

Figure 2.2 – MainActivity.java tab

As we are not looking at the intricate details of the code, an annotated screenshot is more useful than reproducing the actual code in text form. Regularly refer to the next figure while reading on in this section:

Figure 2.3 – Java code

Figure 2.3 – Java code

The first thing to note is that I have added a few empty lines among the code to space things out a little bitand present a clearer image.

Code folding (hiding) in Android Studio

Now look at the left-hand sideof the figure where multiple parts are labeled9. This points to all the little + and - buttons in the editor, which can collapse and expand parts of the code. I have indeed collapsed some parts of the code, and other parts I have left visible. So, what you can see on your screen is slightly different to what you will see if you look at the figure. In Android Studio, play with the + and – buttons for a while to practice hiding and unhiding sections of the code. You might want to get your screen to look like the figure, but this is not a requirement to continue. The technical term for hiding code like this isfolding.

The package declaration

Part1 is called thepackage declaration and, as you can see, it is the package name we chose when we created the projectpreceded by the wordpackage. Every Java file will have a package declaration at the top.

Importing classes

Part2 is eight lines of code that all begin with the wordimport. After the wordimport, we can see there are various dot-separated words. The last word of each line is the name of the class that line imports into our project and all the earlier words in each line are the packages and sub-packages that contain these classes.

For example, this next line imports theAppCompatActivity class from theandroidx.appcompat.app package and sub-packages:

import androidx.appcompat.app.AppCompatActivity;

Note

The semicolon at the end of the lines shows the compiler that it is the end of that line of code.

This means that in this file wewill have access to these classes. In fact, it is these classes that the auto-generated code uses to make the simple app that we saw in action in the previous chapter.

We will not discuss all these classes in this chapter. It is just the concept that we can do this importing that is significant right now. Note that we can add extra classes from any package at any time and we will when we improve upon our app shortly.

The class

Part3 of our code is called theclass declaration. Here is that line in full; I have highlighted one part of it:

public classMainActivity extends AppCompatActivity {

The class declaration is the start of a class. Notice the highlighted part,MainActivity. This is the name Android Studio gave the class when we created the project and it is also the same as theMainActivity.java filename as we would expect having discussed Java classes previously.

The class and the file can be renamed, but since this is the key/main activity of our app,MainActivity seems appropriate. Theextends keyword means that our class calledMainActivity will be of the typeAppCompatActivity.

We can, and will, use some classes without thisextends part. We useextends here because we want to use all the code that went into theAppCompatActivity class, as well as adding our own code to it as well. So, weextend it. All this and more will become clear inChapter 10, Object-Oriented Programming.

Finally, for part3, look at the opening curly brace at the end of the line:{. Now look at the bottom of the figure at part4 of our code. This closing curly brace} denotes the end of the class. Everything in between the opening and closing curly braces,{...}, is part of theMainActivity class.

Methods inside the class

Now look at part5 of the code. Here is that line of code in full with the key part for our discussion highlighted:

protected voidonCreate(Bundle savedInstanceState) {

This is a methodsignature. The highlighted part,onCreate, is the methodname. We make a methodexecute its code by using its name. We say we arecalling a method when we do this.

Although we will not concern ourselves now with the details of the parts of the code on either side of the method name, you might have noticedBundle, one of the classes we imported at part2 of our code. If we removed that relatedimport line, Android Studio would not know whatBundle was and it would be unusable and indicated with a red underline as an error.

Our code would then not compile and run. Notice the very last thing in the preceding line of code is an opening curly brace,{. This denotes the start of the code contained within theonCreate method. Now jump to part6 of our code and you will see a closing curly brace,}. You might have guessed that this is the end of the method. Everything in between the opening and closing curly braces of theonCreate method is the code that executes when the method is called.

We do not need to go into what this code does yet, but as an overview, it sets up the appearance/layout of the app by referring to a resource file that was auto-generated by Android Studio when we created the project. I have highlighted the resource files with an outline in the previous figure.

Parts7 and8 are also methods that I have collapsed to make the image and this discussion more straightforward. Their names areonCreateOptionsMenu andonOptionsItemSelected.

We know enough about our Java code to make some progress. We will see this code again and change it later in this chapter.

Summary of the Java code so far

It is true that contained within the codewe have just had an overview of, there is some complex syntax. However, what we are doing is building up just enough knowledge about this code, so we can work with it to begin to make fast progress in learning Java and Android without having to learn hundreds of pages of Java theory first. By the end of the book, all the code will make sense, but to make quick progress now, we just need to accept that some of the details will remain a mystery for a little while longer.

Examining the app layout file

Now we will look at justone of the many.xml files. There are several different layout files and we will meet them all throughout the course of the book, but let's start with the most recognizable one, which decides most of the appearance of our app.

In the project explorerwindow, left-click on theres folder and then left-click on thelayout folder. Now double left-click on thefragment_first.xml file. The XML code contents of the file is now displayed in the main window of Android Studio.

We can ignore theres generated folder.

We will explore this XML code soon but first find and left-click theDesign button (shown next) to switch to the design view:

Figure 2.4 – To open design view

Figure 2.4 – To open design view

Now we can see the design view that shows us what the XML code will cause to be displayed when the app is run in the emulator:

Figure 2.5 – App display

Figure 2.5 – App display

The preceding figureshould look familiar because it shows the layout of our first app that we ran at the end of the previous chapter – the one with theHello first fragment text and theNext button. If you look in thefragment_second.xml file from the same folder asfragment_first.xml, you will see the second layout we saw in the previous chapter, the one that had thePrevious button on it. In fact, there are even more layout-related files than we might first expect, but we will get to discussing them in this chapter and the next.

Most of the work that we do throughout the book when we design apps will be done in this design view. It is important, however, to know what is going on behind the scenes.

The design view is a graphical representation of the XML code contained in thefragment_first.xml file. Click on theCode tab (near theDesign tab in the previous figure) to see the XML code which forms the layout. I have annotated a screenshot of the XML text, so we can discuss it next:

Figure 2.6 – Screenshot of the XML text

Figure 2.6 – Screenshot of the XML text

The first thing tonote is that this file does not represent the entire layout. It does however represent most of the surface areaand the entireHello first fragment message and theNext button. Also, on the left-hand side, we can see the now-familiar + and – icons so we can fold and unfold sections of the code.

UI layout elements

If we first look at the part of the codelabeled1, we can see that the very first thing is…ConstraintLayout.... Now,ConstraintLayout is a UI element that is used to wrap other parts of the UI.

When we add a new element to a UI in Android, we always start a line with a< followed by the element's name.

The code that follows that rather long and cumbersome-looking line defines theattributes this element will have. This can include dozens of different things depending upon the type of UI element it is. Here, among a bit of other XML, we can see things such aslayout_width andlayout_height. All these attributes define how theConstraintLayout element will appear on the user's screen. The attributes for theConstraintLayout element end at the first> labeled1b.

If we look at the bottom of our XMLscreenshot, we will see some code labeled2. This code,</…ConstraintLayout>, marks the end of theConstraintLayout element. Anything in between the closing> of the element's attributes and</…ConstraintLayout>, which defines its end, is considered a child of the element. So, we can see that ourConstraintLayout has/contains two child elements. Let's look at those children now.

UI text elements

Using what we just learned, we can say that the UI element that starts at position3 in the screenshot is called aTextView. Just like its parent, it starts with a< and its name:<TextView.... If we look further into ourTextView element, we can see it has several attributes. It has atext attribute that is set to"Hello first fragment". This of course is the exact text that our app shows to the user. It also haslayout_width andlayout_height attributes that are both set to"wrap_content". This tellsTextView it can take up as much space as the content it has needs. As we will see throughout the book, there are many more attributes available for this and other UI elements. The final attribute inTextView isid, and we will see how we and Android use theid attribute in the next section when we improve this first app.

Notice that the code at4 in our XML screenshot is/>. This marks the end of theTextView element. This is slightly different to how the end of theConstraintLayout element was written. When an element in XML has no children, we can just end it like this:/>. When the element has children and its end comes further on in the code from where its attributes are defined, it is much clearer to end the element by repeating its name like this:</…ConstraintLayout>.

Note

You might be wondering why the element name forTextView is clear and concise (simplyTextView) yet the full name for theConstraintView is preceded by complicated apparent clutter (androidx.constraintlayout.widget.ConstraintLayout). ThisConstraintLayout element is a special layout that is used to ensure our app's compatibility with older versions ofAndroid. As we will see in a minute when we add buttons to the app, most elements have simple and concise names.

UI Button elements

Now we should be able to quickly identify that the code that starts at5 in the screenshot is aButton element. Just like its parent, it starts with a< and its name:<Button.... If we look further into ourButton, we can see it has several attributes. It has a text attribute that is set to"Next". This of course is the exact text displayed on the button the user can click. It also haslayout_width andlayout_heightattributes that are both set to"wrap_content". This, as with theTextView element, causes the onscreen button to take up as much space as the content it has needs. The final attribute inButton isid, and for buttons this is often a vital attribute, even more so than for some other parts of the UI. As theid attribute can distinguish this button from other buttons, we can program different functionality for different buttons based on the value held in theid attribute. We will see this principle in action soon.

Notice that the code at6 in our XML screenshot is/>. As we have come to know, this marks the end of theButton element.

We will edit and add to this XML code in the next section and learn more about the attributes.

Note

The elements of a layout are often referred to aswidgets.

Adding buttons to the main layout file

Here we will add a couple of button widgets to the screen and we will then see a fast way to make them actually do something. We will add a button in two different ways, firstly using the visual designer and secondly by adding to and editing the XML code directly.

Adding a button via the visual designer

To get started with adding our first button,fragment_first.xml, open it in the editor and switch back to the design view by clicking theDesign tab (shown next):

Figure 2.7 – Design tab

Figure 2.7 – Design tab

Notice to the left-hand side of the layout we have a window that is called thePalette and is shown next:

Figure 2.8 – Palette window

Figure 2.8 – Palette window

The palette is divided into two parts. The left-hand list has the categories of UI elements and allows you to select a category, and then the right-hand side shows you all the available UI elements from the currently selected category.

Make sure that theCommon category is selected as shown in the previous figure. Now, left-click and hold on theButton widget and then drag it onto the layout somewhere near the top center.

It doesn't matter if it is not exact. It is good to practice getting it right, however. So, if you are not happy with the position of your button, then you can left-click it to select it on the layout and then tap theDelete key on the keyboard to get rid of it. Now you can repeat the previous step until you have a new neatly placed button that you are happy with, as here:

Figure 2.9 – Updating the layout

Figure 2.9 – Updating the layout

At this point, we could run theapp on the emulator or on a real device and the button would be there – kind of. If we clicked it, there would even be a simple animation to represent the button being pressed and released. Feel free to try this now if you like. If you do, you will notice that the button isn't positioned as you expect it to be:

Figure 2.10 – Button not positioned correctly

Figure 2.10 – Button not positioned correctly

Don't concernyourself with this apparent anomaly for now; we will look into this over the next few sections.

Next, we are going to edit the attributes of our button in theAttributes window.

Editing the button's attributes

Make sure thebutton is selected by left-clicking it. Now find theAttributes window to the right of the editing window as shown next:

Figure 2.11 – Attributes window

Figure 2.11 – Attributes window

In the previous figure, youcan see that we have access to a wide selection of attributes from the currently selected UI element. To reveal more of the attributes, we click the different categories of attributes and scroll through them using the scrollbar to the right. If they are not open already by default, left-click on theCommonAttributes andAll Attributes sections' arrows to reveal their options.

Now you can see the full details of the button and we can set about editing it. It might seem surprising the substantial number of attributes that something as simple as a button has. This is a sign of the versatility and power that the Android API provides for UI manipulation.

As you can see, there is a large array of different attributes that we can edit right here in the UI designer. InChapter 13, Anonymous Classes – Bringing Android Widgets to Life, we will also edit and manipulate these attributes using our Java code.

For now, we will edit just one attribute. Scroll theAttributes window until you see theonClick attribute in theCommon Attributes section and then left-click it to select it for editing, as shown here:

Figure 2.12 – Common Attributes section

Figure 2.12 – Common Attributes section

Note

If you ever have trouble finding an attribute, you can always find it in theAll Attributes section, where attributes are arranged in alphabetical order. Therefore, theonClick attribute can also be found about two-thirds of the way down the lengthy list of theAll Attributes section.

TypetopClick in theonClick attribute's edit box and pressEnter on the keyboard. Be sure to use the same case, including theslightly counterintuitive lowercaset and uppercaseC.

TheAttributes window will look like this when you are done:

Figure 2.13 – onClick option

Figure 2.13 – onClick option

What we have done here is named the Java method in our code that we want to call when this button is clicked by the user. The name is arbitrary but as this button is in the top part of the screen, the name seems meaningful and easy to remember. The odd casing that we used is a convention that will help us keep our code clear and easy to read. We will see the benefits of this as our code gets longer and more complicated.

Of course, thetopClick method doesn't exist yet. Android Studio is very helpful, but there are some things we need to do ourselves. We will write this method using Java code after we have added another button to our UI. You could run the app at this point and it would still work. But if you click the button, it will crash, and you will get an error because the method does not exist. Android Studio is forewarning us of this impending crash by outlining theonClick attribute in red, as shown in the previous figure. If you hover the mouse cursor over this red outline, you will see the details of the problem:Corresponding method handler… Not found.

Examining the XML code for the new button

Before we add our final button for this project. Click theCode tab to switch back to seeing the XML code that makes our UI.

Notice that there is a new block of code among the XML that we examined earlier. Here is an image of the new block of code:

Figure 2.14 – New block of code among the XML

Figure 2.14 – New block of code among the XML

Also, notice the following details, which should correspond to what we know about XML and Android UI elements:

  • The new code starts with the text<Button and ends with/>.
  • The code has a range of attributes that define the button, includinglayoutWidth andlayoutHeight.
  • The code includes theonClick attribute that we just added with a value of"topClick".
  • ThetopClick value of theonClick attribute is underlined in red, showing the missing method error.
  • The start and end of the code representing the button are enclosed within theConstraintLayout element.

As in the design view, you can hover the mouse cursor over the red-underlinedtopClick code to reveal the details of the problem:Corresponding method handler… Not found.

Note

During the writing of this book, Android Studio updated the way it shows errors in XML. Currently, it highlights the error in red, not underlining in red as shown in the figures and descriptions. The underlining is clearer in black and white print so they have been left as they are.

We can see that the issue is that Android Studio expects a method calledtopClick to be implemented within our Java code. We will do this as soon as we have added that second button.

Adding a button by editing the XML code

Just for variety and to prove that we can, we will now add another button using only XML code, not the UI designer. Most of the time, we will use the UI designer, but this quick exercise should cement in your mind the relationship between the UI designer and the underlying XML code.

We will achieve this by copying and pasting the code for the existing button. We will then make some minor edits to the pasted code.

Left-click just before the button code that starts<Button. Notice that the beginning and end of the code now have a slight highlight:

Figure 2.15 – Button code

Figure 2.15 – Button code

This has identified the part of the code we want to copy. Now left-click and drag to select all the button code, including the highlighted start and end, as shown in this next figure:

Figure 2.16 – Select all the button code

Figure 2.16 – Select all the button code

Press theCtrl + C keyboard combination to copy the highlighted text. Place the keyboard cursor below the existing button code and tap theEnter key a few times to leave some spare empty lines.

Press theCtrl + V keyboard combination to paste the button code. At this point, we have two buttons. There are a couple of problems, however:

Figure 2.17 – Additional error

Figure 2.17 – Additional error

We have anadditional error in both blocks of code that represent our buttons. Theid attribute (in both blocks) is underlined in red. The reason for this error is that both buttons have anid attribute that is the same. Theid attribute is supposed to distinguish a UI element from all other UI elements. Let's fix that.

Giving the buttons unique id attributes

We could solve the problem by calling the second button,button2, but it would be more meaningful to change them both. Edit the code in the first button to give it an ID ofbuttonTop. To do so, find the following line of code (in the first button):

android:id="@+id/button"

Change it to this:

android:id="@+id/buttonTop"

Note

Notice the lowercaseb inbutton and the uppercaseT inTop.

Now identify this line of code in the second button:

android:id="@+id/button"

Change it to this:

android:id="@+id/buttonBottom"

The errors on theid attribute lines are gone. At this point, you might think we can move on to solve our missing method problem.

However, if you run the app and take a quick glance at it, you will see we only appear to have one button. Not only that but (as alluded to previously) the buttons are not in the place we expected them to be either:

Figure 2.18 – Single button

Figure 2.18 – Single button

The reason for this is we haven't explicitly positioned them, so they have defaulted to the top left. Theposition we see on theDesign tab is just a design-time position. Let's change that now.

Positioning the two buttons in the layout

The reason we can only seeone button is that both buttons are in the same position. The second button is exactly covering the first button. And even in theDesign tab (feel free to have a look), the buttons are still sat on top of each other, although they are in the middle of the screen.

Note

You might be wondering why the UI layout tool was designed in this apparently counterintuitive way. The reason is flexibility. As we will see in the next two chapters, not only is it possible to position UI elements differently at design time to when the app is running but there is also a whole bunch of different layout schemes that the app designer (that's you) can choose from to suit their plans. This flexibility results in a little awkwardness while learning about Android but great design power once you have got past the awkwardness. Don't worry: we will move a step at a time until you have this thing beaten.

We will make Android Studio solve the problem for us automatically by first adding to our code and then using the UI designer. First, let's get the design-time layout right. In the code for the second button, find this line of code:

tools:layout_editor_absoluteY="30dp" />

Edit it to be the same as this:

tools:layout_editor_absoluteY="100dp" />

This subtle change will move the second button down a little, but only for design time. If you look in theDesign tab, the button is positioned neatly underneath the first button, but if you run the app on the emulator, they are both still in the top-left corner and on top of one another.

Note

It is possible, even likely, that the exactdp measurements in your layout will be slightly different to those shown in the book. As long as the second button'slayout_editor_absoluteY attribute is about70dp greater than the first, then all will be neat and tidy. Feel free to play with this attribute on both buttons while switching between theCode andDesign tabs until the buttons are positioned to your liking.

When you are satisfied with the position of the buttons, switch to theDesign tab and find theInfer constraints button, shown next:

Figure 2.19 – Infer constraints button

Figure 2.19 – Infer constraints button

Click theInfer constraints button. Android Studio will edit the XML. Let's take a brief look at what has happened behind the scenes. From the end of both of the buttons, the following lines of code were removed.

If the constraints aren't being applied, hit theClear All Constraints button, which is to the left ofInfer constraints; sometimes Android Studio can get confused and needs to reset the existing constraints before inferring the rest:

tools:layout_editor_absoluteX="147dp"tools:layout_editor_absoluteY="30dp" />

These two lines of code were what positioned the buttons horizontally (…absoluteX) and vertically (…absoluteY).

Android Studio also added four lines of code to the first button and three to the second. Here is the code added near the start of the first button:

android:layout_marginTop="30dp"

This code causes the button to have a margin of 30 at the top. But at the top relative to what exactly? Look at these three lines of code that were added at the end of the first button:

app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" />

Notice the new attributes oflayout_constraintEnd_toEndOf,layout_constraintStart_toStartOf, andlayout_constraintTop_toTopOf. The value assigned to each of these attributes is"parent". This causes the first button to be positioned relative to theparent UI element. The parent is the containing layout: theConstraintLayout element.

Now look at the three linesof code added to the second (bottom) button.

Near the start of the code, we see this:

android:layout_marginTop="22dp"

And at the end of the code for the second button, we see these two extra lines:

app:layout_constraintStart_toStartOf="@+id/buttonTop"app:layout_constraintTop_toBottomOf="@+id/buttonTop" />

This means that the second button is positioned with a margin of 22 relative to thebuttonTop widget.

Note

Thedp code is a unit of measurement/distance and will be discussed in more depth inChapter 5, Beautiful Layouts with CardView and ScrollView. The precise values for thedp measurement will likely vary slightly on your layout.

Now run the app and you will see we have two distinct buttons. One has anid attribute ofbuttonTop and it is above the other button, with anid attribute ofbuttonBottom:

Figure 2.20 – Two button options

Figure 2.20 – Two button options

Clearly, there ismore to layouts than I have alluded to so far, but you have had your first glance at one of the options we have to design the UI of our apps. We will be taking a closer look at theConstraintLayout layout element as well as exploring more layout options inChapter 4, Getting Started with Layouts and Material Design.

We want to make one more change in our XML code.

Making the buttons call different methods

Switch back to theCode tab and identify this next line of code in the second (buttonBottom) button:

android:onClick="topClick"

Edit the code to this:

android:onClick="bottomClick"

Now we have two buttons, one above the other. The top one has anid attribute ofbuttonTop and anonClick attribute with a value oftopClick. The other has anid attribute ofbuttonBottom and anonClick attribute with a value ofbottomClick.

These last XML code changes now mean we need to code two methods (topClick andbottomClick) in our Java code.

Note

It is OK for two buttons to call the same method when they are clicked; it is not a syntax error. However, most buttons do have distinct purposes, so this exercise will be more meaningful if our buttons do different things.

We will do that soon, but before we do, let's learn a little bit more about Java comments and look at some Java code we can write to send messages. We will learn to send messages to the user to keep them informed and to ourselves for debugging purposes.

Leaving comments in our Java code

In programming, it is always a clever idea to write notes, known as code comments, and sprinkle them liberally throughout your code. This is to remind us what we were thinking at the time we wrote the code. To do this, you simply append a double forward slash and then type your comment, as follows:

// This is a comment and it could be useful

In addition, we can use comments tocomment out a line of code. Suppose we have a line of code that we temporarily want to disable. We can do so by adding the two forward slashes, like this:

// The code below used to send a message// Log.i("info","our message here");// But now it doesn't do anything// And I am getting ahead of where I should be

Note

Using comments to comment out code should only be a temporary measure. Once you have found the correct code to use, commented-out code should be deleted to keep the code file clean and organized.

Let's look at twoseparate ways to send messages in Android, and then we can write some methods that will send messages when our new UI buttons are pressed.

Coding messages to the user and the developer

In the introduction to this chapterand in the previous chapter, we talked a bit about using other people's code, specifically via the classes and their methods, of the Android API. We saw that we could do some quite complex things with insignificant amounts of code (such as talk to satellites).

To get us started, we are going to use two different classes from the Android API that allow us to output messages. The first class,Log, allows us to output messages to the Logcat window. The second class,Toast, is not a tasty breakfast treat, but it will produce a toast-shaped pop-up message for our app's user to see.

Here is the code we need to write to send a message to the Logcat window:

Log.i("info","our message here");

Exactly why this works will become clearer inChapter 10, Object-Oriented Programming, but for now, we just need to know that whatever we put between the two sets of quote marks will be output to the Logcat window. We will see where to write this type of code shortly.

Here is the code we need to write to send a message to the user's screen:

Toast.makeText(this, "our message",      Toast.LENGTH_SHORT).show();

This is a very convoluted-looking line of code, and exactly how it works, again, will not become clear untilChapter 10, Object-Oriented Programming. The important thing here is that whatever we put between the quote marks will appear in a pop-up message to our users.

Let's put some code, much like we have just seen, into our app for real.

Writing our first Java code

So, we now know the code that will output to Logcat or the user's screen. Butwhere do we write the code? To answer this question, we need to understand that theonCreate method inMainActivity.java executes as the app is preparing to be shown to the user. So, if we put our code at the end of this method, it will execute just as the user sees the app. Sounds good.

Note

We know that to execute the code in a method, we need to call it. We have wired our buttons up to call a couple of methods:topClick andbottomClick. Soon we will write these methods. But who or what is callingonCreate!? The answer to this mystery is that the Android operating system itself callsonCreate. It does so when the user clicks the app icon to run the app. InChapter 6, The Android Lifecycle, we will look deeper at this phenomenon, and it will be clear exactly what code executes and when. You don't need to completely comprehend this now. I just wanted to give you an overview of what was going on.

Let's quickly try this out. Switch to theMainActivity.java tab in Android Studio.

We know that theonCreate method is called just before the app starts. Let's copy and paste some code into theonCreate method of our app and see what happens when we run it.

Adding message code to the onCreate method

Find the closing curly brace} of theonCreate method and add the highlighted code shown next. In the code, I haven't shown the complete contents of theonCreate method but have used to indicate some lines of code not being shown. The important thing is to place the new code (shown in full) right at the end but before that closing curly brace,}:

@Overrideprotected void onCreate(Bundle savedInstanceState) {………// Your code goes here     Toast.makeText(this, "Can you see me?",                    Toast.LENGTH_SHORT).show();             Log.i("info", "Done creating the app");}

Notice that the twoinstances of the wordToast and the wordLog are highlighted in red inAndroid Studio. They are errors. We know thatToast andLog are classes and that classes are containers for code.

The problem is that Android Studio doesn't know about them until we tell it about them. We must add animport for each class. Fortunately, this is semi-automatic.

Left-click on the redToast code in theonCreate method. Now hold theAlt key and then tapEnter. When prompted, chooseImportclass. Now repeat this process forLog. Android Studio adds the import directives at the top of the code with our other imports and the errors are gone.

Note

Alt + Enter is just one of many useful keyboard shortcuts. The following link is to a keyboard shortcut reference for Android Studio. More specifically, it is for the IntelliJ Idea IDE, upon which Android Studio is based. Look at and bookmark this web page; it will be invaluable over the course of this book:http://www.jetbrains.com/idea/docs/IntelliJIDEA_ReferenceCard.pdf.

Scroll to the top ofMainActivity.java and look at the addedimport directives. Here they are for your convenience:

import android.util.Log;import android.widget.Toast;

Run the app in the usual way and look at the output in theLogcat window.

Examining the output

The next figureshows a screenshot of the output in theLogcat window:

Figure 2.21 – Output in the Logcat window

Figure 2.21 – Output in the Logcat window

Look at theLogcat window, you can see our messageDone creating the appwas output, although it is mixed up among other system messages that we are currently not interested in. If you watch the emulator when the app first starts, you will also see the neat pop-up message that the user will see:

Figure 2.22 – Pop-up message

Figure 2.22 – Pop-up message

It is possible that you might be wondering why the messages were output at the time they were. The answer is that theonCreate method is called justbefore the app starts to respond to the user. It is for this reason, it's common practice among Android developers to put code in this method to get their apps set up and ready for the user.

Now we will go a step further and write our own methods that will be called by our two buttons in the UI. We will place similarLog andToast messages inside these new methods.

Writing our own Java methods

Let's get straight on with writing our first Java methods with some moreLog andToast messages inside them.

Note

Now would be a good time, if you haven't already, to get the download bundle that contains all the code files. You can view the completed code for each chapter. For example, the completed code for this chapter can be found in theChapter 2 folder. I have further subdivided theChapter 2 folder intojava andres folders (for Java and resource files). In chapters with more than one project, I will divide the folders further to include the project name. You should view these files in a text editor. My favorite is Notepad++, a free download fromhttps://notepad-plus-plus.org/download/. The code viewed in a text editor is easier to read than from the book directly, especially the paperback version, and even more so where the lines of code are long. The text editor is also a great way to select sections of the code to copy and paste into Android Studio. You could open the code in Android Studio, but then you'd risk mixing up my code with the auto-generated code of Android Studio.

Identify the closing curly brace,}, of theMainActivity class.

Note

You are looking for the end of the entire class, not the end of theonCreate method as in the previous section. Take your time to identify the new code and where it goes among the existing code.

Inside that curly brace, enterthe following code that is highlighted.

@Overrideprotected void onCreate(Bundle savedInstanceState) {…………}………public void topClick(View v){             Toast.makeText(this, "Top button clicked",                            Toast.LENGTH_SHORT).show();             Log.i("info","The user clicked the top                   button");}public void bottomClick(View v){             Toast.makeText(this, "Bottom button clicked",                            Toast.LENGTH_SHORT).show();             Log.i("info","The user clicked the bottom                   button");}} // This is the end of the class

Notice that the two instances of the wordView might be red, indicating an error. Simply use theAlt + Enter keyboard combination to import theView class and remove the errors.

Note

The reason I said there "might" be an error is because it depends on how you entered the code. If you copied and pasted the code, then Android Studio may automatically add theView class import code. If you typed the new code, then the error will appear, and you will need to use theAlt + Enter key solution. This is just a quirk of Android Studio.

Deploy the app to a real device or emulator in the usual way and start tapping the buttons so we can observe the output.

Examining the output

At last, our app does something we told it to do when we told it to do it. We can see that the method names we defined in the buttononClick attribute are indeed called when the buttons are clicked, and the appropriate messages are added to theLogcat window and the appropriateToast messages are shown to the user.

Admittedly, we still don't understand why or how theToast andLog classes really work, neither do we fully comprehend thepublic void and(View v) parts of our method's syntax (or much of the rest of the auto-generated code). This will become clearer as we progress. As said previously, inChapter 10, Object-Oriented Programming, we will take a deep dive into the world of classes, and inChapter 9, Learning Java Methods, we will master the rest of the syntax associated with methods.

Check theLogcat window output. You can see that a log entry was made from theonCreate method just as before, as well as from the two methods that we wrote ourselves, each time you clicked one of the buttons. In the following figure, you can see I clicked each button three times:

Figure 2.23 – Logcat window output

Figure 2.23 – Logcat window output

As you are now familiar with where to find theLogcat window, in future I will presentLogcat output as trimmed text as follows, as it is easier to read:

The user clicked the top buttonThe user clicked the top buttonThe user clicked the top buttonThe user clicked the bottom buttonThe user clicked the bottom buttonThe user clicked the bottom button

And in the next figure, you can see that the top button has been clicked and thetopClick method was called, triggering the pop-upToast message highlighted here:

Figure 2.24 – Pop-up Toast message

Figure 2.24 – Pop-up Toast message

Throughout this book, wewill regularly output to theLogcat window, so we can see what is going on behind the UI of our apps. Toast messages are more for notifying the user that something has occurred. This might be a download that has completed, a new email that has arrived, or some other occurrence that the user might want to be informed about.

Frequently asked questions

  1. Can you remind me what methods are?

    Methods are containers for our code that can be executed (called) from other parts of our code. Methods are contained within a class.

  2. Like the first, I found this chapter tough going. Do I need to reread it?

    No; if you managed to build the app, you have made enough progress to handle the next chapter. All the blanks in your knowledge will be steadily filled in and replaced with glorious moments of realization as the book progresses.

Summary

We have achieved a lot in this chapter. It is true that much of the XML code is still generally incomprehensible. That's OK, because in the next two chapters we will be really getting to grips with the visual designer and learning more about XML, although ultimately our aim is to use XML as little as possible.

We have seen how, when we drag a button onto our design, the XML code is generated for us. Also, if we change an attribute in theAttributes window, then, again, the XML code is edited for us. Furthermore, we saw that we can type (or, in our case, copy and paste) XML code directly in theCode tab to create new buttons on our UI or edit existing ones.

We have seen as well as written our first Java, including comments that help us document our code, and we have even added our own methods to output debugging messages to the Logcat window and pop-upToast messages to the user.

In the next chapter, we will take a full guided tour of Android Studio to see exactly where different things get done at the same time as understanding how our project's assets, such as files and folders, are structured and how we can manage them. This will prepare us to go for a more in-depth look at UI design inChapter 4, Getting Started with Layouts and Material Design, andChapter 5, Beautiful Layouts with CardView and ScrollView, when we will build some significant real-world layouts for our apps.

Left arrow icon

Page1 of 10

Right arrow icon
Download code iconDownload Code

Key benefits

  • Kick-start your Android programming career or just have fun publishing apps to the Google Play marketplace
  • Get a first principles introduction to using Java and Android and prepare to start building your own apps from scratch
  • Learn by example by building four real-world apps and dozens of mini apps

Description

Do you want to make a career in programming but don’t know where to start? Do you have a great idea for an app but don't know how to make it a reality? Or are you worried that you’ll have to learn Java programming to become an Android developer? Look no further! This new and expanded third edition of Android Programming for Beginners will be your guide to creating Android applications from scratch.The book starts by introducing you to all the fundamental concepts of programming in an Android context, from the basics of Java to working with the Android API. You’ll learn with the help of examples that use up-to-date API classes and are created within Android Studio, the official Android development environment that helps supercharge your mobile application development process. After a crash course on the key programming concepts, you’ll explore Android programming and get to grips with creating applications with a professional-standard UI using fragments and storing user data with SQLite. This Android Java book also shows you how you can make your apps multilingual, draw on the screen with a finger, and work with graphics, sound, and animations.By the end of this Android programming book, you'll be ready to start building your own custom applications in Android and Java.

Who is this book for?

This Android book is for you if you are completely new to Java, Android, or programming and want to get started with Android app development. If you have experience of using Java on Android, this book will serve as a refresher to help you advance your knowledge and make progress through the early projects covered in the book.

What you will learn

  • Understand the fundamentals of coding in Java for Android
  • Install and set up your Android development environment
  • Build functional user interfaces with the Android Studio visual designer
  • Add user interaction, data captures, sound, and animation to your apps
  • Manage your apps data using the built-in Android SQLite database
  • Explore the design patterns used by professionals to build top-grade applications
  • Build real-world Android applications that you can deploy to the Google Play marketplace

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date :Apr 09, 2021
Length:742 pages
Edition :3rd
Language :English
ISBN-13 :9781800566446
Category :
Languages :
Tools :

What do you get with eBook?

Product feature iconInstant access to your Digital eBook purchase
Product feature icon Download this book inEPUB andPDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature iconDRM FREE - Read whenever, wherever and however you want
Product feature iconAI Assistant (beta) to help accelerate your learning
OR

Contact Details

Modal Close icon
Payment Processing...
tickCompleted

Billing Address

Product Details

Publication date :Apr 09, 2021
Length:742 pages
Edition :3rd
Language :English
ISBN-13 :9781800566446
Category :
Languages :
Concepts :
Tools :

Packt Subscriptions

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

Frequently bought together


Learning Java by Building Android Games
Learning Java by Building Android Games
Read more
Mar 2021686 pages
Full star icon4 (13)
eBook
eBook
€36.99€41.99
€52.99
Android Programming for Beginners
Android Programming for Beginners
Read more
Apr 2021742 pages
Full star icon4.2 (12)
eBook
eBook
€23.99€26.99
€32.99
How to Build Android Apps with Kotlin
How to Build Android Apps with Kotlin
Read more
Feb 2021794 pages
Full star icon4.6 (32)
eBook
eBook
€36.99€41.99
€52.99
Stars icon
Total138.97
Learning Java by Building Android Games
€52.99
Android Programming for Beginners
€32.99
How to Build Android Apps with Kotlin
€52.99
Total138.97Stars icon

Table of Contents

29 Chapters
Chapter 1: Beginning Android and JavaChevron down iconChevron up icon
Chapter 1: Beginning Android and Java
Technical requirements
What's new in the third edition?
Why Java and Android?
The beginner's first stumbling block
How Java and Android work together
Setting up Android Studio
What makes an Android app?
The structure of Android's Java code
Building our first Android app
Deploying the app so far
Frequently asked questions
Summary
Chapter 2: First Contact: Java, XML, and the UI DesignerChevron down iconChevron up icon
Chapter 2: First Contact: Java, XML, and the UI Designer
Technical requirements
Examining the logcat output
Exploring the project Java and the main layout XML
Adding buttons to the main layout file
Leaving comments in our Java code
Coding messages to the user and the developer
Writing our first Java code
Frequently asked questions
Summary
Chapter 3: Exploring Android Studio and the Project StructureChevron down iconChevron up icon
Chapter 3: Exploring Android Studio and the Project Structure
Technical requirements
Project Explorer and project anatomy
Exploring the file and folder structure of the Empty Activity project template
Exploring the file and folder structure of the Basic Activity project template
Exploring a Basic Activity project
Exploring the Android emulator
Summary
Chapter 4: Getting Started with Layouts and Material DesignChevron down iconChevron up icon
Chapter 4: Getting Started with Layouts and Material Design
Technical requirements
Finding out about Material Design
Exploring Android UI design
Introducing layouts
Building a precise UI with ConstraintLayout
Laying out data with TableLayout
Linking back to the main menu
Summary
Chapter 5: Beautiful Layouts with CardView and ScrollViewChevron down iconChevron up icon
Chapter 5: Beautiful Layouts with CardView and ScrollView
Technical requirements
Attributes quick summary
Building a UI with CardView and ScrollView
Creating a tablet emulator
Frequently asked question
Summary
Chapter 6: The Android LifecycleChevron down iconChevron up icon
Chapter 6: The Android Lifecycle
Technical requirements
The life and times of an Android app
How Android interacts with our apps
A simplified explanation of the Android lifecycle
How we handle the lifecycle phases
Lifecycle demo app
The structure of Java code – revisited
Introducing fragments and the lifecycle
Summary
Chapter 7: Java Variables, Operators, and ExpressionsChevron down iconChevron up icon
Chapter 7: Java Variables, Operators, and Expressions
Technical requirements
Java is everywhere
Understanding Java syntax and jargon
Storing and using data with variables
Using variables
Changing values in variables with operators
Trying out expressions
Summary
Chapter 8: Java Decisions and LoopsChevron down iconChevron up icon
Chapter 8: Java Decisions and Loops
Technical requirements
Making decisions in Java
Switching to make decisions
Repeating code with loops
for loops
Loops demo app
Summary
Chapter 9: Learning Java MethodsChevron down iconChevron up icon
Chapter 9: Learning Java Methods
Technical requirements
Methods revisited
Method structure
Using method demo apps
Exploring method overloading
Scope and variables revisited
Method recursion
Questions
Summary
Further reading
Chapter 10: Object-Oriented ProgrammingChevron down iconChevron up icon
Chapter 10: Object-Oriented Programming
Technical requirements
Important memory management warning
Object-oriented programming
Looking at the code for a class
Basic classes app
Frequently asked questions
Summary
Chapter 11: More Object-Oriented ProgrammingChevron down iconChevron up icon
Chapter 11: More Object-Oriented Programming
Technical requirements
Remember that encapsulation thing?
Static methods
Encapsulation and static methods mini-app
OOP and inheritance
Inheritance example app
Polymorphism
Frequently asked questions
Summary
Chapter 12: The Stack, the Heap, and the Garbage CollectorChevron down iconChevron up icon
Chapter 12: The Stack, the Heap, and the Garbage Collector
Technical requirements
All the Android UI elements are classes too
Inner and anonymous classes
Frequently asked questions
Summary
Chapter 13: Anonymous Classes – Bringing Android Widgets to LifeChevron down iconChevron up icon
Chapter 13: Anonymous Classes – Bringing Android Widgets to Life
Technical requirements
Declaring and initializing the objects
Creating UI widgets from pure Java without XML
Exploring the palette – part 1
Anonymous classes
Exploring the palette and more anonymous classes – part 2
The Widget Exploration app
Using WebView
Running the Widget Exploration app
Summary
Chapter 14: Android Dialog WindowsChevron down iconChevron up icon
Chapter 14: Android Dialog Windows
Technical requirements
Dialog windows
The Note to Self app
Summary
Chapter 15: Arrays, Maps, and Random NumbersChevron down iconChevron up icon
Chapter 15: Arrays, Maps, and Random Numbers
Technical requirements
A random diversion
Handling large amounts of data with arrays
Simple array example mini-app
Getting dynamic with arrays
Entering the nth dimension with arrays
ArrayList
Arrays and ArrayList instances are polymorphic
More Java collections – meet the Java HashMap
The Note to Self app
Frequently asked questions
Summary
Chapter 16: Adapters and RecyclersChevron down iconChevron up icon
Chapter 16: Adapters and Recyclers
Technical requirements
RecyclerView and RecyclerAdapter
Adding RecyclerView, RecyclerAdapter, and ArrayList to the Note to Self project
Running the app
Frequently asked questions
Summary
Chapter 17: Data Persistence and SharingChevron down iconChevron up icon
Chapter 17: Data Persistence and Sharing
Technical requirements
Android intents
Adding a settings page to Note to Self
Persisting data with SharedPreferences
Reloading data with SharedPreferences
Making the Note to Self settings persist
More advanced persistence
Backing up user data in Note to Self
Frequently asked questions
Summary
Chapter 18: LocalizationChevron down iconChevron up icon
Chapter 18: Localization
Technical requirements
Making the Note to Self app accessible for Spanish and German speakers
Running Note to Self in German or Spanish
Summary
Chapter 19: Animations and InterpolationsChevron down iconChevron up icon
Chapter 19: Animations and Interpolations
Technical requirements
Animations in Android
Animation demo app – introducing SeekBar
Frequently asked questions
Summary
Chapter 20: Drawing GraphicsChevron down iconChevron up icon
Chapter 20: Drawing Graphics
Technical requirements
Understanding the Canvas class
Using the Canvas class
Canvas demo app
The Android coordinate system
Creating bitmaps
Manipulating bitmaps
Bitmap manipulation demo app
Frequently asked question
Summary
Chapter 21: Threads and Starting the Live Drawing AppChevron down iconChevron up icon
Chapter 21: Threads and Starting the Live Drawing App
Technical requirements
Creating the Live Drawing project
The game loop
Threads
Implementing the game loop with a thread
Running the app
Summary
Chapter 22: Particle Systems and Handling Screen TouchesChevron down iconChevron up icon
Chapter 22: Particle Systems and Handling Screen Touches
Technical requirements
Adding custom buttons to the screen
Implementing a particle system effect
Handling touches
Running the app
The Android Studio Profiler tool
Summary
Chapter 23: Supporting Different Versions of Android, Sound Effects, and Spinner WidgetChevron down iconChevron up icon
Chapter 23: Supporting Different Versions of Android, Sound Effects, and Spinner Widget
Handling different versions of Android
The SoundPool class
Sound demo app introducing the Spinner widget
Summary
Chapter 24: Design Patterns, Multiple Layouts, and FragmentsChevron down iconChevron up icon
Chapter 24: Design Patterns, Multiple Layouts, and Fragments
Technical requirements
Introducing the model-view-controller pattern
Android design guidelines
Real-world apps
Device detection mini-app
Configuration qualifiers
Fragments
Our first Fragment app
Fragment reality check
Frequently asked question
Summary
Chapter 25: Building a Simple Image Gallery AppChevron down iconChevron up icon
Chapter 25: Building a Simple Image Gallery App
Technical requirements
Angry Birds classic swipe menu
Building an image gallery/slider app
Summary
Chapter 26: Advanced UI with Navigation Drawer and FragmentChevron down iconChevron up icon
Chapter 26: Advanced UI with Navigation Drawer and Fragment
Technical requirements
Introducing NavigationView
Examining the Simple Database app
Starting the Simple Database project
Exploring the autogenerated code and assets
Coding the Fragment classes and their layouts
Using the Fragment classes and their layouts
Summary
Chapter 27: Android DatabasesChevron down iconChevron up icon
Chapter 27: Android Databases
Technical requirements
Databases 101
SQL syntax primer
Android SQLite API
Coding the database class
Coding the Fragment classes to use the DataManager class
Running the Age Database app
Summary
Chapter 28: A Quick Chat before You GoChevron down iconChevron up icon
Chapter 28: A Quick Chat before You Go
Publishing
Make an app!
Keep learning
My other channels
Goodbye and thank you
Why subscribe?
Other Books You May EnjoyChevron down iconChevron up icon
Other Books You May Enjoy
Packt is searching for authors like you
Leave a review - let other readers know what you think

Recommendations for you

Left arrow icon
.NET MAUI Cookbook
.NET MAUI Cookbook
Read more
Dec 2024384 pages
Full star icon4.3 (3)
eBook
eBook
€23.99€26.99
€33.99
iOS 18 Programming for Beginners
iOS 18 Programming for Beginners
Read more
Dec 2024584 pages
Full star icon4.4 (7)
eBook
eBook
€23.99€26.99
€33.99
Apps and Services with .NET 8
Apps and Services with .NET 8
Read more
Dec 2023798 pages
Full star icon4.8 (24)
eBook
eBook
€26.98€29.99
€37.99
Flutter Design Patterns and Best Practices
Flutter Design Patterns and Best Practices
Read more
Sep 2024362 pages
Full star icon4.3 (6)
eBook
eBook
€23.99€26.99
€33.99
Mastering iOS 18 Development
Mastering iOS 18 Development
Read more
Nov 2024418 pages
Full star icon5 (3)
eBook
eBook
€22.99€25.99
€31.99
Mastering Kotlin for Android 14
Mastering Kotlin for Android 14
Read more
Apr 2024370 pages
Full star icon4.8 (10)
eBook
eBook
€20.99€23.99
€29.99
Swift Cookbook
Swift Cookbook
Read more
Jun 2024422 pages
Full star icon5 (3)
eBook
eBook
€20.99€23.99
€29.99
Android Programming for Beginners
Android Programming for Beginners
Read more
Apr 2021742 pages
Full star icon4.2 (12)
eBook
eBook
€23.99€26.99
€32.99
How to Build Android Apps with Kotlin
How to Build Android Apps with Kotlin
Read more
May 2023704 pages
Full star icon4.4 (14)
eBook
eBook
€55.99€62.99
€77.99
Right arrow icon

Customer reviews

Top Reviews
Rating distribution
Full star iconFull star iconFull star iconFull star iconHalf star icon4.2
(12 Ratings)
5 star50%
4 star25%
3 star16.7%
2 star8.3%
1 star0%
Filter icon Filter
Top Reviews

Filter reviews by




Samuel StegallSep 13, 2021
Full star iconFull star iconFull star iconFull star iconFull star icon5
Android development is something that I have wanted to learn more about recently. I have experience with web development with React, and have dabbled with React Native for mobile. As a result, I began searching for more resources on mobile development, and I found this book. Android Programming for Beginners does two things very well: It goes through the several different topics required to know for creating native Android apps, and it teaches Java while doing so. As someone with a fair amount of previous experience with Java, most of the coding-side of this book was more a refresher for me, but I imagine that this would be very useful for new developers as well.The book goes over some important aspects of Android development in detail, including the Android Lifecycle, Design Patterns, Mutlithreading and Databases in Android. This all is on top of the many different ways that developers can compose UI and manage resources when developing an app. Overall, if you go through this book and work through the samples cover-to-cover, you would gain enough knowledge and experience to develop most of the common types of apps being popularly-used today.I am hoping to start working in the professional mobile app development space moving forward, and this book has been incredibly helpful for me to become comfortable with taking that next step. If you're a new developer, or an experienced one just looking to try something new, this book is worth serious consideration.
Amazon Verified reviewAmazon
Jarvis HillMay 18, 2021
Full star iconFull star iconFull star iconFull star iconFull star icon5
I really enjoyed this book. It’s a great way to learn android programming. The book walks you through how to set up Android Studio and how to set up the android emulator so that you can have a testing environment. It also walks you through how to add and manipulate the UI. This book takes you from A to Z and teaches you everything you would need to know to start coding and Android applications so you won’t be left in the dark. This book is great for beginners to cut their teeth on. It visits everything from variables and operators to loops and methods. Later on, you’ll learn about arrays so that you can handle large amounts of data. The Object-Oriented Programming section does a great job of showing you how to encapsulate your classes and variables to prevent them from being access unintentionally. You’ll also be walked through inheritance and Polymorphism and shown how to implement abstract classes and interfaces. Later in the book, you learn how to code necessary to draw on the canvas, save and store data, and much more. You will pretty much learn everything you need in this book to get started with android programming. This is a must-have for anyone looking to get started.
Amazon Verified reviewAmazon
Nathaniel RichardsMay 13, 2021
Full star iconFull star iconFull star iconFull star iconFull star icon5
I’ve been wanting to get more into Android development and this book was a good introduction for me. I think even for new programmers it should be a fairly easy read so that’s great as well if you’re just getting into it!
Amazon Verified reviewAmazon
Justin HornerMay 12, 2021
Full star iconFull star iconFull star iconFull star iconFull star icon5
Many Android books targeting beginners assume too much knowledge of Java, and this book addresses that concern from the very beginning. The author teaches all the Java topics a beginner would need in four multi-chapter apps and more than a dozen quick mini-apps!This book will teach you practically everything you need to know to begin your journey as an Android programmer. Here's a shortlist of the topics covered.- Layouts, Fragments- Common Views- Java Fundamentals (OOP, Variables, Operators, Expressions, etc.)- Memory Usage (Stack, Heap, Garbage Collection)- Localization- DatabasesIf you're interested in learning Android development with Java, definitely check this one out!
Amazon Verified reviewAmazon
JollyNov 12, 2021
Full star iconFull star iconFull star iconFull star iconFull star icon5
Das Buch ist immer bemüht, den Leser Schritt für Schritt mitzunehmen.
Amazon Verified reviewAmazon
  • Arrow left icon Previous
  • 1
  • 2
  • 3
  • Arrow right icon Next

People who bought this also bought

Left arrow icon
Apps and Services with .NET 7
Apps and Services with .NET 7
Read more
Nov 2022814 pages
Full star icon4.8 (27)
eBook
eBook
€26.98€29.99
€37.99
Apps and Services with .NET 8
Apps and Services with .NET 8
Read more
Dec 2023798 pages
Full star icon4.8 (24)
eBook
eBook
€26.98€29.99
€37.99
Machine Learning Techniques for Text
Machine Learning Techniques for Text
Read more
Oct 2022448 pages
Full star icon4.8 (6)
eBook
eBook
€25.99€28.99
€35.99
iOS 16 Programming for Beginners
iOS 16 Programming for Beginners
Read more
Nov 2022686 pages
Full star icon4.7 (50)
eBook
eBook
€23.99€26.99
€33.99
Test-Driven iOS Development with Swift
Test-Driven iOS Development with Swift
Read more
Apr 2022280 pages
Full star icon5 (3)
eBook
eBook
€17.99€20.99
€25.99
Right arrow icon

About the author

Profile icon John Horton
John Horton
John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more
See other products by John Horton
Getfree access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook?Chevron down iconChevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website?Chevron down iconChevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook?Chevron down iconChevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support?Chevron down iconChevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks?Chevron down iconChevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook?Chevron down iconChevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.


[8]ページ先頭

©2009-2025 Movatter.jp