Android Programming
Android Programming
On
ANDROID PROGRAMING
Submitted by
VINITA SINHA
Registration No:11601947
(June-July, 2017)
DECLARATION
I hereby declare that I have completed my six weeks summer training at __TEACH TECH SERVICES_
from 5 JUNE 2018 to 13 JULY 2018 under the guidance of Mr Pulkeet Gupta. I have declare that I
have worked with full dedication during these six weeks of training and my learning outcomes fulfill
the requirements of training for the award of degree of BTech,CSE Lovely Professional University,
Phagwara.
(Signature of student)
Introduction:
As a beginner in Android Programming I have faced a steep learning curve.
Learning Android is like moving to a foreign city. Even if you speak the language,
it will not feel like home at first.
Android has a culture. That culture speaks Java, but knowing Java is not enough.
Getting your head around Android requires learning many new ideas and
techniques. It helps to have a guide through unfamiliar territory.
A simple application may need only one subclass; a complex application can have
many. GeoQuiz is a simple app, so it will have a single Activity subclass named
MainActivity. MainActivity will manage the user interface, or UI.
A layout defines a set of UI objects and their positions on the screen. A layout is
made up of definitions written in XML. Each definition is used to create an object
that appears on screen, like a button or some text.
The first step is to create an Android project. An Android project contains the files
that make up an application. To create a new project, first open Android Studio. If
this is your first time running Android Studio, you will see the Welcome dialog.
From the dialog, choose Start a new Android Studio project. If you do not see the
dialog, you may have created projects before. In this case,
Notice that the package name uses a “reverse DNS” convention: The domain name
of your organization is reversed and suffixed with further identifiers. This
convention keeps package names unique and distinguishes applications from each
other on a device and on Google Play. Click Next. The next screen allows you to
specify details about which devices you want to support. GeoQuiz will only
support phones, so just check Phone and Tablet. Select a minimum SDK version of
API 19: Android 4.4 (KitKat).
Then click Next…
Now you have to choose in which type of activity you want to work with.
There are many options like empty activity, location activity, scrolling activity,
Basically when you are a beginner, you have to choose an empty activity to avoid
complications.
In the final dialog of this wizard, name the activity subclass MainActivity.
Click Finish. Android Studio will create and open your new project.
The main view is the editor. To get you started, Android Studio has opened
MainActivity.java in the editor.
Widgets are the building blocks you use to compose a UI. A widget can show text
or graphics, interact with the user, or arrange other widgets on the screen. Buttons,
text input controls, and
checkboxes are all types of widgets. The Android SDK includes many widgets that
you can configure to get the appearance and behavior you want. Every widget is an
instance of the View class or one of its subclasses (such as TextView or Button).
Linear Layout: Linear Layout is a view group that aligns all children in a single
direction, vertically or horizontally.
Table Layout: Table Layout is a view that groups views into rows and columns.
Frame Layout: The Frame Layout is a placeholder on screen that you can use to
display a single view.
List View: List View is a view group that displays a list of scrollable items.
In this application I have made my layout in linear layout design and in linear
layout I have given orientation vertical and gravity center. The root element of this
layout’s view hierarchy is a Linear Layout. As the root element, the Linear Layout
must specify the Android resource XML namespace at
http://schemas.android.com/apk/res/android LinearLayout
wrap_content view will be as big as its contents require (You may see fill_parent
in some places. This deprecated value is equivalent to match_parent.)
For the root Linear Layout, the value of both the height and width attributes is
match_parent. The Linear Layout is the root element, but it still has a parent – the
view that Android provides for your app’s view hierarchy to live in. The other
widgets in your layout have their widths and heights set to wrap_content.
The Text View is slightly larger than the text it contains due to its
android:padding="24dp" attribute. This attribute tells the widget to add the
specified amount of space to its contents when determining its size. You are using
it to get a little breathing room between the question and the buttons. (Wondering
about the dp units? These are density independent pixels).
android:orientation
android:text
The Text View and Button widgets have android:text attributes. This attribute tells
the widget what text to display. Notice that the values of these attributes are not
literal strings. They are references to string resources. A string resource is a string
that lives in a separate XML file called a strings file. You can give a widget a
hardcoded string, like android:text="True", but it is usually not a good idea.
Placing strings into a separate file and then referencing them is better because it
makes localization easy.
Now we have to create string resource file, for creating we have to open
res/values/strings.xml. The template has already added one string resource for you.
Add the three new strings that your layout requires.
Now, whenever you refer to @string/false_button in any XML file in the GeoQuiz
project, you will get the literal string “False” at runtime and viva versa.
Now our layout design has been completed. It show now look like this on your
phone. We can easily built graddle in android studio and built apk file and can
transfer that application onto your phone. We can also run the application in
android studio by creating a virtual machine.
These buttons will not work because we just only created a layout. Now we want
to add functions to those buttons. For this when you created the GeoQuiz project, a
subclass of Activity named mainActivity was created for you. The class file for
mainActivity is in the app/java directory of your project. The java directory is
where the Java code for your project lives.
Open the QuizActivity.java file and take a look at its contents. The default java
class will look like this:
package com.big.android.geoquiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@Override
setContentView(R.layout.activity_quiz); }}
If you are not seeing all of the import statements, click the symbol to the left of the
first import statement to reveal the others. This file has one Activity method:
onCreate(Bundle).
This method inflates a layout and puts it on screen. When a layout is inflated, each
widget in the layout file is instantiated as defined by its attributes. You specify
which layout to inflate by passing in the layout’s resource ID.
layout is R.layout.activity_main.
“android:id="@+id/true_button"
Notice that there is a + sign in the values for android:id but not in the values for
android:text. This is because you are creating the IDs and only referencing the
strings.
First, notice the m prefix on the two member (instance) variable names. This prefix
is an Android naming convention. Now you can wire up your button widgets. This
is a two-step process:
In an activity, you can get a reference to an inflated widget by calling the following
Activity method:
The Android SDK comes with listener interfaces for various events, so you do not
have to write your own. In this case, the event you want to listen for is a button
being pressed (or “clicked”), so your listener will implement the
View.OnClickListener interface. Start with the TRUE button. In
MainActivity.java, we add the following code to onCreate(Bundle) just after the
variable assignment.
You set a listener to inform you when the Button known as mTrueButton has been
pressed. The setOnClickListener(OnClickListener) method takes a listener as its
argument. This listener is implemented as an anonymous inner class. The syntax is
a little tricky, but it helps to remember that everything within the outermost set of
parentheses is passed into setOnClickListener(OnClickListener). Set a similar
listener for the FALSE button.
Now to make the buttons fully armed and operational. You are going to have a
press of each button trigger a pop-up message called a toast. A toast is a short
message that informs the user of something but does not require any input or
action. You are going to make toasts that announce whether the user answered
correctly or incorrectly.
Like when I press true button then with the help of toast a small notification
displays on the screen, reading that the answer is correct but when we press false
button then incorrect answer displays.
Here Toast.LENGTH_SHORT.show() means the duration for which the text will
displays. If length is short then, it will display for short time period if length is long
then it will display for longer period of time.
Now our next target is to work on NEXT button so that next question will display
after the 1st question. To make this happen, we are going to add a class named
Question to the GeoQuiz project. An instance of this class will encapsulate a single
true-false question. Then, we will create an array of Question objects for
MainActivity to manage.
The mQuesId variable will hold the resource ID (always an int) of a string
resource for the question. These variables need getter and setter methods. Rather
than typing them in yourself, you can have Android Studio generate the
implementations for you.
Back in Question.java, right-click after the constructor and select Generate... and
then Getter and Setter. Select mQuesId and mAnswer and click OK to create a
getter and setter for each variable.
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
@Override
} }); }
Here I have added a variable and a question array. Here you call the Question
constructor several times and create an array of Question objects.
Now I am going to have QuizActivity create an array of Question objects. It will
then interact with the TextView and the three Buttons to display questions.
Now let’s see about the NEXT button. First, get a reference to the button. Then set
a View.OnClickListener on it. This listener will increment the index and update the
TextView’s text.
Now that you have the questions behaving appropriately, it is time to turn to the
answers. At the moment, GeoQuiz thinks that the answer to every question is
“true.” Let’s rectify that. Here again, you will implement a private method to
encapsulate code rather than writing similar code in two places. The method that
you are going to add to MainActivity is:
This method will accept a boolean variable that identifies whether the user pressed
TRUE or FALSE. Then, it will check the user’s answer against the answer in the
current Question object. Finally, after determining whether the user answered
correctly, it will make a Toast that displays the appropriate message to the user.
Now for adding the arrow sign beside the next button we again switch to
activity_main.xml file and write down the following code inside the next button.
android:drawableRight="@drawable/arrow_right"
android:drawablePadding="4dp”.