100% found this document useful (9 votes)
105 views

Android Programming in Kotlin Starting With An App 1st Edition James all chapter instant download

Programming

Uploaded by

safirkahenrm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (9 votes)
105 views

Android Programming in Kotlin Starting With An App 1st Edition James all chapter instant download

Programming

Uploaded by

safirkahenrm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Download the full version of the ebook at ebookname.

com

Android Programming in Kotlin Starting With An App


1st Edition James

https://ebookname.com/product/android-programming-in-kotlin-
starting-with-an-app-1st-edition-james/

OR CLICK BUTTON

DOWNLOAD EBOOK

Download more ebook instantly today at https://ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Android Programming In Java Starting with an App 3rd


Edition James

https://ebookname.com/product/android-programming-in-java-starting-
with-an-app-3rd-edition-james/

ebookname.com

Android Development with Kotlin 1st Edition Marcin Moskala

https://ebookname.com/product/android-development-with-kotlin-1st-
edition-marcin-moskala/

ebookname.com

Android Game Programming For Dummies 1st Edition Derek


James

https://ebookname.com/product/android-game-programming-for-
dummies-1st-edition-derek-james/

ebookname.com

Pediatric Fitness Secular Trends and Geographic


Variability Medicine and Sport Science 1st Edition G. R.
Tomkinson And T. S. Olds (Editors)
https://ebookname.com/product/pediatric-fitness-secular-trends-and-
geographic-variability-medicine-and-sport-science-1st-edition-g-r-
tomkinson-and-t-s-olds-editors/
ebookname.com
The Value of BCG and TNF in Autoimmunity 1st Edition
Denise Faustman (Auth.)

https://ebookname.com/product/the-value-of-bcg-and-tnf-in-
autoimmunity-1st-edition-denise-faustman-auth/

ebookname.com

Hair Care An Illustrated Dermatologic Handbook 1st Edition


Zoe Diana Draelos

https://ebookname.com/product/hair-care-an-illustrated-dermatologic-
handbook-1st-edition-zoe-diana-draelos/

ebookname.com

Where We Come From The Origin of the Lithuanian People 2nd


Edition Zigmas Zinkevi■ius

https://ebookname.com/product/where-we-come-from-the-origin-of-the-
lithuanian-people-2nd-edition-zigmas-zinkevicius/

ebookname.com

Learning Node 1st Edition Shelley Powers

https://ebookname.com/product/learning-node-1st-edition-shelley-
powers/

ebookname.com

New Models for Ecosystem Dynamics and Restoration 1st


Edition Richard J. Hobbs

https://ebookname.com/product/new-models-for-ecosystem-dynamics-and-
restoration-1st-edition-richard-j-hobbs/

ebookname.com
The Social World of Batavia Europeans and Eurasians in
Colonial Indonesia New Perspectives in Se Asian Studies
2nd Edition Jean Gelman Taylor
https://ebookname.com/product/the-social-world-of-batavia-europeans-
and-eurasians-in-colonial-indonesia-new-perspectives-in-se-asian-
studies-2nd-edition-jean-gelman-taylor/
ebookname.com
Android Programming In Kotlin:
Starting with an App

Using Android Studio 3

First Edition

Mike James

I/O Press
I Programmer Library
Copyright © 2017 IO Press

All rights reserved. This book or any portion thereof may not be reproduced
or used in any manner whatsoever without the express written permission of
the publisher except for the use of brief quotations in a book review.

Mike James Android Programming In Kotlin: Starting with an App


1st Edition
ISBN Paperback: 978-1871962543
First Printing, 2017
Revision 0

Published by IO Press www.iopress.info


In association with I Programmer www.i-programmer.info

The publisher recognizes and respects all marks used by companies and
manufacturers as a means to distinguish their products. All brand names and
product names mentioned in this book are trade marks or service marks of
their respective companies and our omission of trade marks is not an attempt
to infringe on the property of others.
In particular we acknowledge that Android is a trademark of Google.

2
Preface
Android programming is an attractive proposition. There are more Android
users than any other smartphone or tablet and this makes it a potentially huge
market. Android devices are relatively cheap and this makes it suitable for
implementing smaller scale projects. Finally the tools that you need are free
to download and use and you don’t need anyone's permission to get started.
The only difficulty is that the Android is a tough system to master. It is easy
enough to get started as Android Studio will build you a Hello World app in a
few minutes, but what you do next is more difficult. The good news is that
Kotlin is a language that makes working with Java and Android much easier.
You cannot get away from Java in Android as it is the language used for the
libraries that your apps are built on, but Kotlin makes writing code so much
easier and it works with Java code perfectly. As a programmer who has
worked in both Java and Kotlin, I can vouch for the fact that Kotlin is a
pleasure to work with and an excellent choice.
You can get a surprisingly long way in Android programming by simply
copying and pasting code from other programs that roughly do what you
want, but the results are usually unreliable and disappointing. To create a
good app there is no substitute for understanding how the system works and
appreciating its strong points and its limitations.
This book aims not only to show you how common tasks are done in
Android, but to make sure that you understand the reasons they are done in a
particular way. This means covering not just the what but also the why. The
good news is that this isn’t as difficult as you might expect because Android
does have repeating patterns and ways of doing things and once you have
seen something in action you find it generalizes to components you haven’t
encountered.
This isn’t a book full of complete examples and case studies. In fact the
examples are stripped down to their bare minimum to avoid having to present
lines of irrelevant and repetitious code and to let you see what is essential. It
also isn’t a complete treatment of everything Android. A single book that
covered every aspect of the Android system would be too large to pick up and
carry. Instead it focuses on the things you need to know to write a simple app.
It focuses on creating the user interface (UI) because this is what you spend
most of your time working on even if the app in question is sophisticated. At
least 90% of the effort in creating any app goes into building and perfecting
the UI and this makes it the key place to start. In this book you will find out
how to build an app with a single Activity and a UI. If you master this level of
the art then you will find it much easier to push on into unknown territory.

3
It is assumed that you can program, but not necessarily in Kotlin or Java – any
general object-oriented language will do as a starting point. As long as you are
happy with the idea of a loop, conditional, function and what objects are,
then you should have no problem working with Kotlin and Android.
Finally the development tool used is the latest version of Android Studio
because it doesn’t make sense not to use it or to use anything else.
This is the place to start.
Mike James
December 2017

This book is a revised and updated version of the series of Android


Adventures With Android Studio on the I Programmer website:
www.i-programmer.info

There are additional articles on I Programmer that form the basis of the more
advanced books that are still in preparation. After updating and revision,
Android Adventures – Mastering Fragments will be published in print as
Android Programming: Mastering Fragments & Dialogs. The first draft of
Android Programming: Structuring a Complex App, which goes further into
threading, concurrency, life cycle and other topics crucial to a real world app,
is currently work in progress on the website.

To keep informed about forthcoming titles in the Android Programming


series visit the publisher’s website:
www.iopress.info
This is also where you will also find errata, update information to keep up
with changes in Android Studio and Android itself and, most importantly,
the code from the books. You can also provide feedback to help improve
future editions of Android Programming.

4
Table of Contents
Chapter 1
Getting Started With Android Studio 11
The Language Choice........................................................................12
What You Need to Know..................................................................13
Making a Start...................................................................................13
Your First Program............................................................................15
First Look...........................................................................................18
The IDE..............................................................................................19
Basic Project Structure......................................................................19
Anatomy of an Activity.....................................................................20
Hello Layout Editor...........................................................................21
Inspecting the XML...........................................................................26
The Kotlin..........................................................................................27
Getting Started with the Emulator ...................................................28
Summary...........................................................................................32
Chapter 2
Activity and User Interface 33
The MainActivity..............................................................................34
Inside the Activity.............................................................................35
View and ViewGroup........................................................................36
Creating Our First UI.........................................................................38
Properties & Attributes......................................................................42
Events................................................................................................43
Connecting the Activity to the UI.....................................................44
Finding View Objects........................................................................48
Summary...........................................................................................51
Chapter 3
Building a Simple UI 53
What's in the Palette..........................................................................54
The Button an Example....................................................................56
Positioning – the ConstraintLayout..................................................56
Sizing.................................................................................................64
The Component Tree........................................................................65
A Simple Button Example – Baseline Alignment............................66
Orientation and Resolution..............................................................67
A First App – Simple Calculator.......................................................73
Summary...........................................................................................81

5
Chapter 4
Android Events 83
How Java Passes Functions...............................................................83
Android Events..................................................................................84
The Kotlin Object..............................................................................85
Passing Functions In Kotlin..............................................................87
Function References..........................................................................87
Anonymous Functions......................................................................89
The Lambda.......................................................................................89
Events Using Lambdas......................................................................90
Closure...............................................................................................91
Using Breakpoints.............................................................................93
Modern Java Event Handling............................................................94
Summary...........................................................................................96
Chapter 5
Basic Controls 99
Basic Input Controls..........................................................................99
Button Styles and Properties............................................................99
All Attributes...................................................................................104
Text Fields.......................................................................................105
The onEditorAction Event..............................................................108
CheckBoxes.....................................................................................109
Switches and Toggle buttons..........................................................110
Radio Buttons .................................................................................111
Summary.........................................................................................113
Chapter 6
Working With Layouts 115
Understanding Layouts...................................................................115
Layout Properties............................................................................116
Width and Height............................................................................118
Units................................................................................................118
A Control is Just a Box....................................................................120
Gravity.............................................................................................121
The FrameLayout............................................................................122
LinearLayout....................................................................................124
RelativeLayout.................................................................................128
Summary.........................................................................................133

6
Chapter 7
The ConstraintLayout 135
Automatic Constraints....................................................................136
Manual Constraints.........................................................................141
Bias Constraints...............................................................................143
Chains..............................................................................................144
A Chained Keypad..........................................................................147
Guidelines........................................................................................151
Groups.............................................................................................152
Sizing...............................................................................................153
Barriers............................................................................................158
Troubleshooting..............................................................................161
Summary.........................................................................................163
Chapter 8
Programming The UI 165
A UI Library.....................................................................................165
The View.........................................................................................165
Using setContentView.....................................................................166
The ViewGroup...............................................................................168
Programming Layout Properties.....................................................169
The View Hierarchy........................................................................171
XML Layout.....................................................................................171
Inflation Theory..............................................................................172
Finding View objects.......................................................................173
How to Build a UI?..........................................................................174
Summary.........................................................................................176
Chapter 9
Menus – Toolbar 177
Creating a Menu Resource..............................................................177
The Menu Tree................................................................................178
Displaying a Menu..........................................................................180
Using the Toolbar............................................................................181
Creating the App Bar ......................................................................185
Where's My Toolbar?.......................................................................186
Responding to Menu Events...........................................................186
Changing Menus in Code................................................................189
Controlling the Toolbar...................................................................192
Summary.........................................................................................193

7
Chapter 10
Menus – Context & Popup 195
The Context Menu..........................................................................195
Contextual Action Bar.....................................................................199
The Popup Menu.............................................................................202
Summary.........................................................................................204
Chapter 11
Resources 205
Why Use Resources?.......................................................................205
What are Resources?.......................................................................207
Drawables........................................................................................209
Values..............................................................................................211
IDs....................................................................................................212
Accessing Resources in Code – The R Object................................213
Conditional Resources....................................................................214
A Simple Localization.....................................................................218
Android Studio Translation Tools.................................................219
Summary.........................................................................................221
Chapter 12
Bitmap Graphics 223
Android Graphics............................................................................223
The Bitmap......................................................................................223
The ImageView Control..................................................................224
Canvas..............................................................................................225
A First Graphic................................................................................227
Transformations..............................................................................229
A Logical Approach to Transforms................................................232
Setting Your Own Coordinates.......................................................233
Simple Animation...........................................................................234
Timer and Threads..........................................................................236
Listing..............................................................................................239
Summary.........................................................................................242

8
Chapter 13
Life Cycle Of An Activity 243
Lifetime and State...........................................................................243
The Life Cycle of an App................................................................244
The Simple Approach.....................................................................245
Lifecycle Explorer...........................................................................246
Trying It Out....................................................................................247
Retaining State – the Bundle..........................................................248
Saving Additional UI Data..............................................................250
Complex UI Elements.....................................................................251
Advanced State Management.........................................................252
Summary.........................................................................................253
Chapter 14
Spinners 255
The Spinner and the Layout Editor................................................255
Introducing the ArrayAdapter........................................................257
Handling the Selection...................................................................259
Creating an ArrayAdapter from a Resource...................................262
Changing The List...........................................................................262
Summary.........................................................................................264
Chapter 15
Pickers 265
Working with Pickers......................................................................265
TimePicker.......................................................................................266
TimePicker in Code.........................................................................267
Updating the Time..........................................................................268
DatePicker........................................................................................269
Number Picker.................................................................................272
Multi-Digit Input.............................................................................276
Summary.........................................................................................279
Chapter 16
ListView 281
Understanding the Adapter............................................................281
Extending the ListAdapter Class....................................................282
Using the ArrayAdapter..................................................................283
Working with the Data....................................................................285
A Custom Layout.............................................................................288
A Custom ArrayAdapter.................................................................291
Reuse, Caching and General Layouts.............................................295
Custom Adapter..............................................................................298
Summary.........................................................................................299

9
Chapter 17
Android The Kotlin Way 301
What You No Longer Have To Type...............................................301
var & val...........................................................................................302
No More get & set............................................................................302
View Objects As Properties.............................................................304
Event Handlers................................................................................304
Data..................................................................................................306
Null Safety.......................................................................................307
Java Types and Null........................................................................308
Kotlin Aims to Help........................................................................310
Summary.........................................................................................311

10
Chapter 1

Getting Started With Android Studio

Android represents a big potential market. It is also the most open of the "big"
phone and tablet platforms. You can write a program for an Android and let
your friends have a copy, keep it to yourself or put it on sale in an app store.
Android phones and tablets are comparatively cheap and this makes it easier
to get started. What is even better, all the tools you need to create an Android
app are free. You don't need to pay anything to create, or distribute, your
Android apps. If you want to sell them using a well known marketplace there
may something to pay – there is a one-time fee of $25 to register for Google
Play, but you don't have to use any particular distribution method.
All that stands between you and your Android app is your imagination and
programming ability. I can't do much to improve your imagination, but I can
help with the programming side of things. If you are new to Android
programming this is the place to start.
In this book I will show you the fundamentals of Android programming. Not
the tips and tricks, but how to think about what is going on. You'll be
introduced to the general principles that will make it possible for you to
master anything that you encounter that is new in the future. It isn’t possible
to cover all of Android in one book as the subject is very large. Instead we
focus on the basics of creating a User Interface (UI) as all apps have to have
some way of interacting with a user.
There are many ways to create an Android app but Google's Android Studio is
an easy to use Android IDE – Integrated Development Environment – and it is
now the recommended way of doing the job.
Before Android Studio you had to use the Eclipse IDE and set up the SDK and
other pieces of software needed. This wasn't difficult, but Android Studio
eliminates extra steps and it makes programming Android easy. Put simply, it
is the way of the future and so worth your investment in learning it.
With the release of Android Studio Google stopped work on the Eclipse
plugin and this means that Android Studio really is the only way to develop
apps from now on.

11
The Language Choice
With the release of Android Studio 3 you now have a choice of programming
in Java or Kotlin. The advantage of Java is that it is a well known and well
supported language. If you already program in Java or want to acquire the
skill then you might well be better off starting with Android Programming In
Java: Starting with an App ISBN: 978-1871962550
Kotlin may be a be a new language but it is already well supported for the
simple reason that it is 100% compatible with Java. The Android libraries are
all written in Java, but Kotlin can make use of them with no problems. It is
this that makes Kotlin Android development possible. What is more, you
aren’t restricted to Kotlin in a project. You can add Java code to your new
Kotlin project and you can add Kotlin code to an existing Java project.
Put simply, there is very little risk involved in moving to Kotlin and there is a
lot to be gained. Kotlin is a much simpler and cleaner language than Java. It
has had the benefit of seeing how Java evolved and avoiding those mistakes.
Kotlin attempts to get straight to the point. In Java you tend to write some
code over and over and it can be time consuming and hides the simplicity of
what you are trying to do. Whenever this happens Kotlin modifies the
language so that you can express what you are doing succinctly. Programmers
moving from Java to Kotlin generally find that they like it because they get
more done with less typing. Programmers who only know Kotlin don’t know
how lucky they are!
Kotlin does things differently from Java and while you can pick up the
language as you go you might like to read Programmer's Guide To Kotlin
ISBN:978-1871962536. It isn’t necessary, as long as you know Java or another
object-oriented language, you can pick up Kotlin as you develop your
Android apps but I recommend learning the finer points of the language
sooner or later. It pays off to know your language.
The way Kotlin is used and the ways it changes Android programming in
particular are introduced as we go along. However, the final chapter is a brief
look at the major influences of Kotlin on Android programming. If you want a
quick overview before you start then read the final chapter, but in many ways
it makes more sense to read it as a summary after you have encountered the
ideas in context.
The bottom line is that, unless you have a commitment to Java, you probably
should start new projects in Kotlin and convert existing projects to Kotlin a
bit at a time.

12
What You Need to Know
You need to be able to program in a modern object-oriented language. Java
would be best as it is closest to Kotlin, but C++, C#, Visual Basic or anything
similar are close enough in spirit to Java for you to be able to cope. You might
well need to look things up about the specifics of particular features of Kotlin,
but most of the time it should be obvious, or obvious with the help of a few
comments.
It isn't necessary to be an expert programmer because for a lot of Android
programming you are simply using the features and facilities provided. That
is, a lot of Android programming is just a matter of following the rules.
However, if you hope to produce something unique and useful you will at
some point have to add something of your own – and here creativity and skill
are required. So you might not need to be an expert programmer to get
started, but you need to become one by the time you create your amazing app.
Fortunately practice is a good teacher and so learning to make the most of
Android Studio will actually help you learn to code.

Making a Start
I'm not going to spend a lot of time explaining how to install Android Studio
in a step-by-step way as the Android website does a good job and it is more
likely to be up-to-date. It is worth, however, going over the basic principles.
https://developer.android.com/studio/
The installer will download everything you need including the JDK.
Windows:
1. Launch the downloaded EXE file,
android-studio-bundle-<version>.exe.
2. Follow the setup wizard to install Android Studio.
Mac OS X:
1. Open the downloaded DMG file,
android-studio-bundle-<version>.dmg
2. Drag and drop Android Studio into the Applications folder.

13
Linux:
1. Unpack the downloaded ZIP file,
android-studio-bundle-<version>.tgz,
into an appropriate location for your applications.
2. To launch Android Studio, navigate to the
android-studio/bin/
directory in a terminal and execute studio.sh. You may want to add
android-studio/bin/
to your PATH environmental variable so that you can start Android
Studio from any directory.
Accept any defaults that the setup program offers you – unless you have a
good reason not to. It installs not only Android Studio, but the SDK and the
virtual device system that lets you test your application.

In most cases Android Studio just installs with no problem.


Now you should be able to run Android Studio. If not the most likely cause of
the problem is the JDK and so re-installation is a best first option.

14
Your First Program
You can opt to start Android Studio after the installation. You will probably
not get straight to Android Studio the first time it starts as it downloads
updates to itself and to the Android SDK. You just have to be patient.
When it finally gets going you will see the Android Studio welcome screen:

If you have already created some programs you might well see them listed in
Recent projects.
Assuming this is your first project select the option:
Start a new Android Studio project

15
You can ignore the details of the new project for the moment. All you have to
do is supply a name for your application – HelloWorld in this case. Also make
sure you have Include Kotlin support ticked – this is what makes the project
use Kotlin rather than Java. Accept the other defaults that Android Studio
has filled in for you.

When you click Next you are given the chance to pick what devices you are
targeting. Again simply accept the defaults:

Most of the time you will want to create apps that run on a version of Android
that captures the biggest market but if this isn't a concern then it can be better
to select a more recent Android version.

16
The next page lets you select a template for your project. In this case change
the selection to Basic Activity. This gives you some additional generated code
which makes the app easier to create an app that looks right. Every Android
application consists of at least one Activity and this template generates a
project with a single Activity ready for you to customize:

On the next page you can assign custom names for the various components of
your project that the template generates. For a real project you would assign
names that were meaningful but in this case you can accept the defaults:

Finally you can click the Finish button and wait as Android Studio creates all
the files you need. Even a simple Android project has lots of files so again it
all takes time.

17
First Look
When everything is ready you will see Android Studio for the first time.
As long as everything has worked you should eventually, it takes about three
minutes or more, be presented with a view of your new project starting off in
the Layout Editor:

Problems?
If you get any error messages then the chances are that your project hasn't
finished being processed. Wait a little while longer for the activity to stop. If
you look at the status line at the bottom of the window you will see a message
saying “Gradle Build Finished” when Android Studio has finished with your
new project.
If you still have problems it is worth trying the File,Invalidate Caches/Restart
command. This usually works for "Missing styles" and similar errors.

18
The IDE
Although there looks like a lot to master in Android Studio's user interface,
most of it you will only visit occasionally. The key things to notice are that
moving from left to right you have:
● The Project window
● The tool Palette and the Component Tree window
● The Layout Editor
● The Attributes window
Most of the time you will be using the Project window and the Attributes
window. You will also see different editors depending on what sort of file
you have selected. In this case you have by default a layout file,
content_main.xml, selected and hence you have a layout editor in the middle
of the screen.
Before we go into layout, which is one of the main topics of this book, it is
important that you know a little about the file structure of a project so that
you can navigate to its different parts.

Basic Project Structure


When the project has finished building all of the files created can be viewed
by opening the Projects tab. The main thing to notice is that there are a great
many folders and files:

It seems almost unbelievable that the simplest Android app you can create
involves so many files.

19
Don't panic. Most of the files that have been created are auto-generated and
most of the time you don't need to know anything about them, let alone open
or edit them. In fact opening and editing auto-generated files really isn't a
good idea.
So let's focus on the files that matter to us.
For our simple program there are only two important files. One of them
determines the behavior of the Activity:
MainActivity.kt
The other determines the visual appearance, or View, of the app:
content_main.xml
You can set which Activity is the one that the system starts, but by default it
is the single activity that you created and named when you set up the project.
You can change the default names but for the moment leave them as they are.
Despite this being a Kotlin project, the java directory, from your point of view,
is where most of the construction of your app occurs, so make sure you know
where it is. The res directory is where you store all of the resources, layouts,
bitmaps, etc, that your app needs.
So while things look complicated at the moment the only two project files
that matter to you, and your project, are MainActivity.kt in the java folder
and content_main.xml in the res folder.
The two other folders in the java folder are concerned with creating tests for
your program. This is not something that we need to worry about when first
starting to write Android apps.

Anatomy of an Activity
An Android app is made up of one or more Activity classes.
You can think of an Activity as being something like a web page complete
with HTML to determine what displays and JavaScript to determine what it
does.
In the case of an Activity the layout is determined by the XML file in resource
(res) folder, this is often called the View, and the behavior is determined by
the Kotlin or Java code in the java folder.
The XML can be thought of as a markup language much like HTML or XAML.
It defines an initial layout for the screen when the app first runs. It is possible
to generate new layout components at runtime from the Java file. In fact, if
you really want to, you can dispense with the XML file and generate
everything from code, but as you will discover the XML markup approach is
much the best way to do the job because of the availability of the Layout
Editor.

20
So to be 100% clear in a Kotlin project:
● The kt file contains the code that makes your app behave in particular
ways.
● The .xml layout file contains a definition of the initial UI, the View, of
your app.

Hello Layout Editor


Let's take a look at the two files that have been generated for our initial Hello
World application beginning with the XML layout. Double click on
content_main.xml file in the Project tab and the file will open (if it isn't
already open). If it is already open you can also select its tab displayed just
above the editor area. You can select any file that is open for editing by
selecting its tab.
You can work with the XML directly to define where all the buttons and text
go, and later you will learn how to edit it when things go wrong or to fine
tune it. However, Android Studio provides you with a very nice interactive
editor – the Layout Editor and this is worth using.
As you become more experienced the idea of switching between a design
view and an XML view will become second nature. Think of the interactive
editor as a very easy way of generating the XML that otherwise would take
you ages to get right. If you look at the bottom left you will see two tabs –
Design and Text:

You can switch between editing the XML as text, and editing it in the drag-
and-drop Layout Editor simply by clicking on the tab. If you now click on the
tab the window will display the Layout Editor but be patient the first time
you do this it might take a few moments.

21
The Layout Editor looks a little too much to take in when you first see it but
you will quickly get used to it. On the left is a Palette of all of the components
or controls - buttons, text, checkboxes and so on - that you can place on the
design surface:

In the middle is the design surface and this defaults to the screen size and
appearance of the Nexus 5. You can select other devices to work with.

22
There are, in fact, two views of the layout that you can use, the design and the
blueprint. By default you are shown the design view but you can display
either view using the menu at the top left of the design area.

You can display both views together but in most cases available screen area is
the main issue and showing just one is the best option. The design view
shows you the layout as a close approximation to how it will appear on a real
device. The blueprint view doesn’t try to render the UI realistically but it does
provide you will more layout information to help you position and size
elements. Use whichever you are most happy with.

23
On the left, below the Palette, you have the Component Tree which shows
you the structure of your layout, that is how different UI components are
contained inside others. It shows you the structure of the XML file in an
easier to use form. You can use the Component Tree as an easy way of to
select individual UI components by clicking on their names. You can also
drag-and-drop UI components onto the Component Tree to position them
accurately within the hierarchy.
On the right you have the Attributes window that can be used to set the
attributes, such as width, height, color and so on of any component in the
layout. If you have used any drag-and-drop Layout Editor then this will seem
familiar and if you have struggled with detailed layout using a markup
language, be it HTML, XAML or XML, you will appreciate how easy the
Layout Editor makes building and testing a UI.
In the case of our sample program the only component is a single TextView
already containing the text "Hello World". A TextView is the standard
component to use when all we want to do is to display some static text.

24
You can modify the greeting text if you want to. Select the TextView
component either on the design or in the Component Tree and use the
Attributes window to find its Text attribute. Change this to read "Hello
Android World":

Use the text field without the spanner icon. The properties with the spanner
icon next to them are used to set values that only show in the Layout Editor.
In this case the text field without the spanner icon is the one that controls
what appears in your app at runtime.
You can use the Layout Editor to create any UI you care to and you really
don't have to get involved in the XML that corresponds to the layout – unless
things go wrong or you need to do something so sophisticated that the Layout
Editor doesn't support it.

25
Random documents with unrelated
content Scribd suggests to you:
[Contents]
VI. MENABOZHO SWALLOWED BY A LARGE FISH

(Chippewa, 1895)

One day Menabozho went fishing with hook and line in Gitchee Gumee, the Big
Sea Water. A large fish came along and swallowed the hook and line, swallowed
Menabozho and his canoe, swallowed everything, just like a big sea cave.

When Menabozho waked out of his sleep he saw a squirrel sitting on the canoe
beside him. The fish had swallowed him, too.

Menabozho said: “Brother Squirrel, where are we?”

The squirrel answered: “Menabozho, we are in a great fish.”

Menabozho found his bow and arrows in the canoe and shot an arrow upward. It
killed the great fish. The body of the fish began to rise to the top of the water.
Menabozho prayed to the Great Manitou that the wind might blow from the
south. The Great Spirit heard his prayer and sent the south wind. It blew the
great fish to the north shore of Gitchee Gumee, where Nokomis lived. [240]

The great fish floated on the water like a little sun-fish; when it touched the shore
the birds fed on its flesh, and Menabozho came out and went to his
grandmother, Nokomis.

After their greeting was over Menabozho went back and found the birds still
feeding on the fish.

“Go away, my little brothers,” he said.

Each bird took a piece of the fish and flew away, and Menabozho then cut up the
great creature and made much fish oil; he had a great plan in his mind and was
glad to have this oil.

A wicked manitou lived on an island in Gitchee Gumee. This island had miles of
blackest pitch on all of its shores; not even a water manitou could swim through
this pitch. Menabozho carried the fish oil over and poured it on the pitch;
wherever the oil touched the pitch it was never sticky again.
Menabozho found the wigwam of the wicked manitou. All day long he shot
arrows at this wigwam. The manitou came out and laughed at him.

A woodpecker called out, “Hit him in the back, Menabozho!”

The manitou just then turned to run, and Menabozho hit him and he fell. The
woodpecker flew down by Menabozho. His white feathers were stained by the
pitch, but Menabozho painted his head with war paint. He is one of Menabozho’s
brothers.

Ooranah. [241]

[Contents]
VII. THE THUNDER-BIRD OF THE DAKOTAS

(Sioux, 1895)

hat is thunder?” a white


man asked a Sioux or
Dakota Indian.

“Thunder is a big bird flying


in the air. It makes tracks
like fire. You can hear it clap
its wings. It is the young
thunder-birds that hurt the
Sioux. The old birds will not
touch us. They are our
friends.”

“Did you ever see a


thunder-bird?” was asked
by the white man.

“I never did, but my father’s


brother, Little Crow, saw
one fall dead out of the sky.
It had wings wide as a white
man’s house, and it had
lightning on its wings. It had a face like a man; its nose was like an eagle’s bill.”

“Who else ever saw one?” was the next question.

“One fall our tribe was out hunting, and a thunder-bird flew down on the ground
just a little way from them. It did not hurt them; they saw that it had on
[242]snowshoes. They found the track of the shoes when it flew away. Our tribe
had good hunting that winter. They killed many bears.”

“We have only one God; why do you have so many?” was next asked.
“The Great Spirit is the god of the Dakotas,” said the Indian. “He made
everything but wild rice and thunder. We must do as our fathers have done or
the spirits of the dead will punish us. It is not good to change. We believe what
they told us when we were children. We worship the Great Spirit.”

“Who rules the water?” asked the white man.

“Unktahe, the spirit of the water.”

“What do you call the thunder?”

“Wahkeon. He and Unktahe are always fighting. It is a great battle; when there is
a storm Unktahe sends the thunder-birds back to the sky, sometimes.”

“Tell me more about the thunder-birds,” said the white man.

“Wahkeontonka is the father of all the birds. He is Big Thunder. He lives on a


great mountain in the west. His wigwam has four doors. A caribou stands at the
north door. He is swift like the north wind. At the south door is a red deer; he is
very beautiful. His eyes are like the little lakes in summer.

“A butterfly watches at the east door of the wigwam. He is like the morning light.
A bear watches [243]the west door, and when the wind blows from the west even
the white people can hear the bear growling. This is true.”

“What has Wahkeontonka done for the Indian?”

“The thunder spirit gave the Indians thunder and wild rice. They eat rice and are
strong. He showed them how to use the bow and arrow.

“He dug iron from the ground and made tomahawks. He made spears. The
Indians know Wahkeontonka is wise.”

The Dakotas believe that their god of storm lives on Thunder Cap, a high
promontory in Minnesota overlooking Lake Superior; from here he sends the
rain, hail, or snow.

Thunder Bay lies at his feet; on its shore lies the great giant turned to stone,
who, ages ago, dared to defy Big Thunder. Wahkeontonka is the Dakotas’
Jupiter. [244]
[Contents]
VIII. HIAWATHA THE WISE
How he united the Five Nations

any, many moons ago three Indians


sat on the bank of the great river
with many islands. These three
Indians had come on a long trail
from their country, and it was a new
trail, for they had made it
themselves. Nobody had been on it
before they cut their way through
the thick forest.

The fathers of these Indians had


been told of this river in the north
which was filled with islands. The
three Indians had said to their
fathers that they would seek it; now
they sat on a little hill, and it was
before them.

The night sun had changed into a


shape like a canoe three times since
they had started on the long trail.
Their moccasins were torn, and their
feet were very tired; but the river
was very beautiful, and it made their
eyes glad to see it.

While the three Indians sat watching


the river, they saw a white canoe coming straight toward the little hill [245]where
they sat. It seemed to come from the place of the setting sun.

The three Indians saw a white-haired chief alone in the canoe, and he had no
paddle. The canoe came very fast, but it needed no help. The white-haired chief
told the canoe to stop by the little hill on the shore where sat the three Indians; it
came there and stopped.
The three Indians knew by the strange canoe that the Great Spirit had sent him,
and they were afraid.

The white-haired chief said: “I am Hiawatha. I will help you and your people. Tell
me what your nation can do. Tell me of your hunting.”

The three arose and told Hiawatha of their nation. They had thought their people
very strong; now they seemed like wild rabbits for weakness. They told him of
their hunting, but they were not proud, for Hiawatha was wiser than any chief,
and he knew what was in their hearts.

Hiawatha said: “Go back to your people. I shall come, and you will see me when
you have made my lodge ready. I knew you were coming, for I saw you in the
dark forests. I saw you on the great rocks in the forests. Go back and tell your
people I am coming. Tell them to make a wigwam for Hiawatha.”

The three Indians could not talk to each other. Their hearts were full. They found
the trail they had made and followed it back to their own land; there [246]they told
their chiefs of the wise one in the white canoe. The chiefs made ready for his
coming.

“He will come in a white stone canoe,” said the chiefs.

The wigwam was built by a lake, and it was made of the finest skins of the deer.
It was a white wigwam, with the door left open. No one watched to see who
should shut the door.

One morning the door was shut, and a strange white canoe was in the water.
The people came out of their lodges, and soon the doorway of skins in the white
wigwam was opened. Hiawatha had come to the Onondaga nation. His wigwam
was on the shore of Tiota or Cross Lake, in the land of the Onondagas.

Heyanwatha means the Wise Man. Hiawatha the people call him now. He taught
the Onondagas many things, for he had lived with the Great Spirit. He was sent
to help the Indian tribes.

Hiawatha taught the people how to plant corn and beans. They learned much
about planting, and they learned how to store food for winter time.

While he was with the Onondagas the runners brought word that a great band of
warriors was coming to fight them. The young braves put on their war paint.
“Call a great council of all the tribes,” said the wise Hiawatha. “Let them meet on
the hill by the lake.” It was Onondaga Lake. [247]

Swift runners carried word to four tribes. Their chiefs and great braves met on
the hill by the lake, and their wives waited with them. All the people waited for
three days, but Hiawatha did not come to the council. The chiefs sent men to
Hiawatha on the morning of the fourth day to ask why he made them wait.

Hiawatha answered: “The Holder of the Heavens has shown me that if I go to


this council great sorrow will come to me. I was sent to teach you peace. I shall
show you how to make war. I will come.”

Then Hiawatha stepped into his white stone canoe, and it went to the place of
the great council, where the chiefs waited.

All the great chiefs and the people shouted when Hiawatha came. He stood still
in the council circle. His daughter stood beside him, but no one had seen her
before. When her father looked at her she went to her place among the women.

The first day of the council the chiefs told their plans, and Hiawatha listened.
The second day he arose in the council, and the people listened. Hiawatha said
wise words. All the chiefs remembered the words of Hiawatha. He made this
speech:

“My brothers: You are from many tribes. You have come here for one cause. It is
to live in safety. We must join ourselves together. The tribes that are [248]on the
warpath are strong. Not one tribe here is equal to that great people. Make
yourselves a band of brothers. Then you will be stronger than they.

“The Mohawks that sit in council by that great tree shall be the first nation. They
are the warlike people.

“The Oneidas who sit by the great stone that cannot be moved shall be the
second nation. They are a wise people.

“The Onondagas that live at the foot of the great hills shall be the third nation.
They are great in speech making.

“The Senecas who live in the forest, and whose trails are found all over the land,
shall be the fourth nation, for they have much wisdom in hunting.
“The Cayugas live in the open country. Their wigwams are the finest, and their
beans and corn grow like the grass on the plains. Their name is known for great
wisdom; they shall be the fifth nation.”

Hiawatha sat down in the council, and the third day the chiefs talked with one
another; then they all said: “We will do this thing. We will be one nation. We will
be called the Five Nations.”

The council was ended. Hiawatha went to his canoe and called softly to his
daughter.

As she left the women a great cloud came in the sky. It was a thunder-bird. The
great cloud took the daughter of Hiawatha, and she was gone. [249]

Pueblo Women grinding Corn

From a Photograph

[250]

The white stone canoe came to the landing place. There was music in the air
like the wind blowing through the pine trees. All the sky was filled with the sweet
music.
The people mourned for Hiawatha, for he was gone. His wigwam by the lake
Tiota was empty, and he was never seen again.

The Five Nations say that he went to the Islands of the Happy Ones. Owayneo,
the Great Spirit, called him. His daughter had gone before him.

The Five Nations were strong. They were a wise people. Many moons after the
white men came the Tuscaroras sat with them around the council-fire. Then they
were known as the Six Nations. The white people have often called them the
Mingos.

Arranged from Schoolcraft and Horatio Hale. [251]

[Contents]
IX. WAMPUM OR INDIAN MONEY

iawatha was on his way to


the country of the
Mohawks. He went from
one nation to another to
teach them the things they
needed to know. While on
his journey he came to the
borders of a lake. It was
too deep to wade across,
and he stood still thinking
whether he should call his
magic white canoe to help
him or go back. He had
come on a long trail and
was very tired.

While he was wondering


which way was best, the
sky was filled with wild
ducks. These birds flew
down upon the lake and
began to drink and to
swim. In a few minutes
they flew up into the air
again, in one great black
flock.

Behold! the lake was dry, and its bed was a mass of shells.

Hiawatha knew that the lake had been made dry ground for his sake. He
gathered some of the shells [252]and, striking them with sharp flints, strung the
pieces on strings of sinew. This was the first wampum.

He carried the strings of wampum to the Mohawks, and they believe their tribe to
have been the first to use it.
They showed the other tribes how to make the long strings of bead money, and
many bands went on strange trails to find the rivers or the lakes where the shells
were hidden under the water.

Mounds like small hills have been found by the white men near lakes and
streams. When these mounds were uncovered, it was known that they were only
heaps of broken shells. The flint stones were not good tools, and the Indians
would use only perfect beads. [253]

[Contents]
X. LEGEND OF THE ARBUTUS

(Chippewa, 1894)

n old teepee stood by a frozen river in the


forest where there are many pine trees.
The tops of the trees were white with
snow. The teepee was almost covered
with the snow. An old chief sat in this
teepee; his hair was like the icicles that
hang from dead pine-tree branches; he
was very old.

He was covered with furs. The floor of his


teepee was covered with the skins of the
bear and the elk. He had been a great
hunter. His name was Peboan. Peboan
was faint with hunger, and he was cold.
He had been hunting for three days. He
had killed nothing. All the moose, deer,
and bear had gone. They had left no trail.
Wabasso, the rabbit, had hidden in the
bushes. There was no food, no meat for
Peboan.

He called upon the great Menabozho for


help.

“Come, Menabozho, come help Peboan, the chief of the winter manitous. Come,
for Mukwa the bear has gone from me. Come, or Peboan must go to the far
[254]north to find Mahto the white bear. Peboan is old, and his feet are weary.”

Peboan crawled on his knees over the furs to the little fire in the middle of the
teepee. He blew on the coals with his faint breath, and the coals grew very red.
His breath was like a wind; the coals made the wind warm like a south wind. The
deerskins that covered the teepee trembled like leaves, for the warm wind blew
them.
Peboan sat on the furs on the floor of his teepee and waited. He knew
Menabozho would hear him.

Peboan heard no sound, but he looked toward the door of his teepee. It was
lifted back, and he saw a beautiful Indian maiden.

She carried a great bundle of willow buds in her arms. Her dress was of sweet
grass and early maple leaves. Her eyes were like a young deer. Her hair was
like the blackest feathers of a crow, and it was so long that it was like a blanket
over her shoulders. She was small; her feet were hidden in two moccasin
flowers.

“Menabozho heard Peboan, the winter manitou. He has sent me. I am Segun.”

“You are welcome, Segun. Sit by my fire; it is warm. I have no meat. Sit down
and tell me what you can do.”

“Peboan may tell first what he can do,” said Segun. [255]

Peboan said: “I am a winter manitou; I blow my breath, and the flowers die. The
waters stand still; the leaves fall and die.”

Segun said: “I am a summer manitou; I blow my breath, and the flowers open
their eyes. The waters follow me on my trail.”

Peboan said: “I shake my hair, and the snow falls on the mountains, like the
feathers of Waubese, the great white swan.”

Segun said: “I shake my hair, and warm rain falls from the clouds. I call, and the
birds answer me. The trees put on their leaves, and the grass grows thick like
the fur of the bear. The summer sky is my teepee. Menabozho has said that the
time has come for you to go.”

Peboan’s head bent over on his shoulder. The sun melted the snow on the pine
trees; it melted the snow on the teepee. Segun waved her hands over Peboan,
and a strange thing happened.

Peboan grew smaller and smaller. His deerskin clothes turned to leaves and
covered Peboan on the ground.
Segun looked, but Peboan was gone. She took some flowers from her hair and
hid them under the leaves on the ground. There was ice on the leaves, but it did
not hurt the pink flowers. Segun breathed on the flowers, and they became
sweet. [256]

She said: “I go, but the flowers shall stay to tell of Segun’s visit to Peboan. The
children shall find them and know that Segun has sent Peboan away. It shall be
so each time the snows melt and the rivers begin to run. This flower shall tell
that spring has come.”

Peboan’s teepee was sweet with the breath of the flowers, but Segun was gone.
[257]

[Contents]
XI. THE ONE WHO LOVED HIM MOST

he Great Spirit whispered to


the heart of a warrior that he
must go and seek the Happy
Hunting Grounds. His squaw
shed many tears when he
told her. His children wailed
loudly, for they knew no one
ever returned from that
journey.

“We will follow you on the


long trail,” said his squaw.

The warrior hid his arrows


and his bow, put on the new
moccasins that lay by his
wigwam door, and started.
Behind him followed in the
same trail his squaw, his
sons, and his dog. He sang the death chant, and their voices echoed the chant.
The dog knew its meaning and howled for sorrow.

After a time the younger boy grew weary and hid himself. They did not miss the
child, and he ran back to the wigwam, lay down, and fell asleep.

Soon the older son missed his brother. He said:

“I will go back and find Keweenaw. He is small; I will care for him.” [258]

The father and mother were not alone. The dog walked softly behind them. The
warrior did not look back, but his squaw called to him: “I am here. I am strong. I
will follow you to the gate of the Happy Hunting Grounds.”

Then the trail became rough and steep. The wild cactus tore their moccasins.
The thorn tree caught their blankets and tore their flesh.
The wife was weary and cried: “Let us rest. Let us wait.” She fell on her face in
the trail.

When she arose no one was before her. She said: “I will go and find our
children; then I will come back to the trail and find my husband.”

The warrior was now alone. No, not alone, for his dog, footsore and heated with
the long journey, kept at his heels. His master had not known that he had
followed. Now in the dark night his dog pressed his head against him to comfort
him.

The trail led westward, but the morning sun cast no long shadows of the wife
and children. They had not taken up the trail again.

And still forever toward the west went the two, the Indian and his dog. Moons
and suns rose and set. At last, across the wide plains, he saw two great rocks,
like tall trees. These marked the gateway of the Happy Hunting Grounds. Tired,
sick, and nearly fainting with weariness, the Indian kept on his way. [259]Tired,
hungry, and very weak, behind him came the faithful dog.

At the gateway the Indian raised his voice and cried: “I am here. The Great Spirit
called me; I have come.”

The watchman opened the gate and, taking the brave by the hand, led him past
the two great rocks that held the wonderful gate.

“Where are those who were with you at the first?” asked the watchman.

“The way was long. Their feet were weary,” was the answer.

“Who is this that stands watching you, with eyes that show the tears they cannot
shed?”

“He who loved me best,” said the Indian.

The watchman put his hand on the head of the hound. The dog gave a joyful
leap, and the gates of the Happy Hunting Grounds shut both the Indian and his
dog into the Land of Rest, from which there is no return. [260]
[Contents]

You might also like