Building Interactive Apps
Building Interactive Apps
Building Interactive Apps
We will explore two different types of Android layouts in this course - Linear
layouts and relative layouts.
An app developer is someone who can write code that an Android device
understands.
A GOOD app developer is someone who can write code that a human
understands.
There are a few concepts that you need to be familiar with before we start
building the application.
These include: Views are the basic building blocks for the elements of the User
Interface.
A View fills a rectangular area on the screen and is responsible for drawing and
event handling.
All Views inherit from View class (android.view.View)
Listed as follows are some of the common View subclasses which will be used in
Android applications:
TextView
EditText
Button
CheckBox
RadioButton
ImageButton
Progress Bar
Spinner
Android Layouts
Android Views
Android ViewGroups
Android Layouts
Layouts are typically defined in XML and can include GUI components such as text fields,
buttons, and labels.
Android Views
Views are the basic building blocks for the elements of the User Interface.
A View fills a rectangular area on the screen and is responsible for drawing and
event handling.
All Views inherit from View class (android.view.View)
Listed as follows are some of the common View subclasses which will be used in
Android applications:
TextView
EditText
Button
CheckBox
RadioButton
ImageButton
Progress Bar
Spinner
Linear Layout
Relative Layout
Table Layout
Frame Layout
Web View
List View
Grid View
Both View and ViewGroup subclasses together will play a key role in creating
layouts in Android applications.
Get familiarized with the layout editor by referring to the image and the numbered
steps in the upcoming cards.
If it is vertical, they are displayed in a single column, and if it is horizontal, they are
displayed in a single row.
Any change made to a layout's XML is reflected in the Android Studio's design
editor, which can be visualized by clicking the Design tab.
A button in Androidology is a pushbutton that the user can press to trigger an action.
Buttons and text views are subclasses of the same Android View class.
Few Scenarios
Hardcoding makes localization hard
Suppose you have created an app that is a huge success on your local Google
Play Store.
You now want to make it available internationally in different languages.
But you have hardcoded the text in your layout files!
Making the app international would be extremely difficult and would require
extensive reprogramming.
Hardcoding makes making global changes to the text harder
Suppose your company's name has been changed, and your boss has asked you
to change the wordings in your app.
If you have hardcoded the entire text, you will need to edit a whole host of files to
change the text.
The Solution
It also enhances the process of making global changes to the text across the
application much efficient as you only need to edit a single file - strings.xml.
Android is available in around 46 languages, thus enabling the development of
applications in various languages to reach a wider audience.
We are going to create 4 string resources, one for the text that appears at the
button, and three for the text that appears in the text view.
To do this, use Android Studio's explorer to find the file strings.xml in the app >
src > main > res > values folder.
<resources>
<string name="app_name">WTWW</string>
</resources>
android:text="@string/app_label"
1. We've created a layout that defines what the app looks like.
o It includes 3 text views and a button.
2. The file strings.xml includes the string resources needed by the
application.
o We've added a label for the button and default text for the text views
Quiz 1
The Activity Code: A Sneak Peak
When you create a new project, the wizard creates an empty activity
called MainActivity. The code for this activity is held in a file
called MainActivity.java.
Open this file from app > src > main > java folder.
You can see that Android Studio has generated Java code as follows.
MainActivity.java Deciphered
All activities (not just this one) must extend the Activity class or one of its
subclasses.
The Activity class consists of a bunch of methods that enhance your Java class
from a plain old Java class into a full-fledged, card-carrying Android activity.
All activities should also implement the onCreate() method.
This method gets called when an object of the activity is created. It is used to
perform basic setup, such as what layout the activity is associated with.
setContentView() - tells the activity which layout file to use for the screen. It
parses the layout and creates a view hierarchy.
R.layout.activity_main - is the reference ID of the layout
resource activitymain.xml.
You never change any of the code within this file, but it's useful to know it's there.
myTextView.setText("HelloWorld");
The Button
Typically, whenever a layout includes a button, something is expected to happen
when a user clicks the button.
Buttons carry out the expected tasks by calling a corresponding method in the
activity.
In our app, to get the button to call a method in the activity when it is clicked, we
need to make changes to two files:
1. Change the layout file activity_main.xml.
o Specify the method in the activity that will be called when the button is clicked.
2. Change the activity file MainActivity.java.
o Define the method that gets called.
Update Update!
Go to the layout file activity_main.xml, and add a new line of XML to the
<button> element to say that the method displayWeirdWord should be called
when the button is clicked.
Now that the layout knows which method to call in the activity, we need to define
the method.
Watch it!
To get a method to respond to a button click, it has to be made public, have
a void return type, and take a single View parameter.
The View parameter in the method may seem unusual at first, but there is a good
reason for it being there.
This parameter refers to the GUI component that triggers the method (in this case,
the button).
Note: As mentioned earlier, GUI components such as buttons and text views are all
types of View.
Rest assured that the core functionality is working, let us remove the hard-coded
infestations from our app and organize the list of weird words in a deck.
Add a new java class - weirdWordDeck.java (in the same package as
MainActivity.java)
To add the new class, navigate to WTWW > app > src > main > java >
com.fresco.play.wtww
Right-click the package name, select New > Java class
In the window that opens, name the new class as weirdWordDeck and
click ok.
Task #1 - Hints
TextView1 - WordLabelView
The view should be aligned at the top with respect to the parent view
TextView2 - wordNameView
The view should be aligned in the center vertically, with respect to the
parent view.
Assign paddings at the left, start, and top as necessary.
Button - showWordButton
The view should be aligned at the bottom with respect to the parent view
Kudos mate, you are now all set to build interactive apps!
Let us look back on all that we have learned in this course:
Got a gist of Android layouts, views, and view groups
Mastered the Android Layout Editor
Laid out the UI Components in both Linear and Relative layouts
Learned to add and edit the attributes of Text views and buttons in the
layout through the layout XML as well as the design editor.
Understood the significance of avoiding hard-coding strings and the use
of strings.xml.
Created references for UI Components
Set properties in the GUI component using the methods exposed by the
Java class
Points to Remember
The Button element is used to add a button.
The TextView element is used to add a text view.
All GUI components are types of views. They inherit from the
Android View class.
Strings can be referenced in the layout using "@string/string_name"
A button can be made to call a method when clicked by adding the following
XML to the layout: android:onClick="clickMethod"
o There needs to be a corresponding method in the activity public void
clickMethod(View view) { }
Final Quiz