android app dev 2
Post on 25-Feb-2018
231 Views
Preview:
TRANSCRIPT
-
7/25/2019 Android App Dev 2
1/28
Learn How to Develop Android
Application
Shakeel Iqbal,31 Jul 2013CPOL
4.81 (88 votes)
Rate this:vote 1vote 2vote 3vote 4vote 5
This Article gives a basic concept for developing Android app.
Download HelloWorld.zip - 881.5 KB
Introduction
http://www.codeproject.com/script/Membership/View.aspx?mid=578505http://www.codeproject.com/script/Membership/View.aspx?mid=578505http://www.codeproject.com/info/cpol10.aspxhttp://www.codeproject.com/info/cpol10.aspxhttp://www.codeproject.com/info/cpol10.aspxhttp://www.codeproject.com/Articles/628894/Learn-How-to-Develop-Android-Applicationhttp://www.codeproject.com/Articles/628894/Learn-How-to-Develop-Android-Applicationhttp://www.codeproject.com/Articles/628894/Learn-How-to-Develop-Android-Applicationhttp://www.codeproject.com/KB/android/628894/HelloWorld.ziphttp://www.codeproject.com/KB/android/628894/HelloWorld.ziphttp://www.codeproject.com/KB/android/628894/HelloWorld.ziphttp://www.codeproject.com/Articles/628894/Learn-How-to-Develop-Android-Applicationhttp://www.codeproject.com/Articles/628894/Learn-How-to-Develop-Android-Applicationhttp://www.codeproject.com/Articles/628894/Learn-How-to-Develop-Android-Applicationhttp://www.codeproject.com/Articles/628894/Learn-How-to-Develop-Android-Applicationhttp://www.codeproject.com/Articles/628894/Learn-How-to-Develop-Android-Applicationhttp://www.codeproject.com/info/cpol10.aspxhttp://www.codeproject.com/script/Membership/View.aspx?mid=578505 -
7/25/2019 Android App Dev 2
2/28
These days the demand of smart phone is being increased and we have different types of
client e.g. Touch Phone, tables, Note etc. The demands of the applications for these smart
clients are constantly increasing and we have several operating systems for these smart
clients e.g. Android, IOS and Windows etc. This article will provide a guideline to .net
developer or beginners to develop Android Application.
Development Environment
This Article is not focusing on how to install/Configure development environment, you can
find different development environment on internet. In this article, I will use eclipse mobile
juno development environment.
Create New Project
For creating new project click on the File -> New -> Project.
It opens the new wizard window where you can select application template and set the
name of the project and package name.
-
7/25/2019 Android App Dev 2
3/28
On the click on Next Button, it moves to the next step. Here provide the Application
Name, Project Name and Package Name. Package Name is a same as Name Space
in .Net and it must be unique. The project name is only used by Eclipse and it must be
unique in the workspace.
-
7/25/2019 Android App Dev 2
4/28
You also need to select Minimum Required SDK from dropdown. It is the lowest version
of Android that your application will support. Then select Target SDK which is the HighestAPI that the application is known to work with. Then select the installed SDK for Compile
With option to compile your code for selected targeted SDK.
In the next steps of the wizard, you need to select icons, some other information and on
the last step provide the name of your activity and it's Navigation Type.
-
7/25/2019 Android App Dev 2
5/28
I will provide the detail of activity and layout later, for now you can say it is windows formas desktop application or as a Web page in web application. Provide the name of Activity
and Layout. Leave Navigation type None for now. Press finish button. Eclipse will
automatically generate start up activity for you.
-
7/25/2019 Android App Dev 2
6/28
Within the Eclipse, you can see your project in the Package Explorer pane of the left-hand
side of the screen. The HelloWorld application contains several auto generated folders and
file. Lets discuss it one by one.
/Src:It contains the all java source files those are associated to the project. For
example the MainActivity.java file generated by Eclipse is stored in this directory
under the package name com.MyFirst.helloworld you specified in the wizard. You
can add more packages for your application e.g. com.MyFirst.Fragments,
CommonClasses etc.
/gen:It contains the java source files and other code files generated by Eclipse. We
will use R.java file code later in our project. This file is generated to link your
-
7/25/2019 Android App Dev 2
7/28
resource files for use in your java files in /src folder.
Andriod Directories (/Android 4.2 and /Android Dependencies):The Android 4.2
directory is generated against your selected Android SDK and Dependencies folder
the support file is generated for the backward compatibility with previous versions of
Android SDK.
/assests:By default this is empty folder, but you can put your raw assets in it and
these files include in your projects as-is. For Example it is good location for textures
and game data.
/bin:It is the output directory for build. You can find the final .apk file and other
compiled resources in this folder.
/libs:It contains the private libraries (jar) those you might want to use in your
application.
/res:It contains all the resources files those are associated to your application. All
the graphical element, string and layout of application exists one of the sub folder of
the res. We will add some resource later in this project. The auto generated layout
file activity_main.xml by Eclipse exists in layout sub folder.
Project.properties:This file is generated by android project wizard and it is used for
Eclipse settings. It is very rare to change this file directly. Instead Right click on the
project in Eclipse and click on properties and make any change which is required
for your application.
Run Your Application in Emulator
First you need to configure emulator for testing your application. To configure your emulator
click on window -> Android Virtual Device Manager
-
7/25/2019 Android App Dev 2
8/28
It will open pop up where you can create new Virtual device, Edit exist and start any virtual
device. After create your new virtual device close this window. Now right click on theapplication in Package Explorer pane on the left-hand side of the screen and click on Run
As -> Android Application. It will compile your application and execute it in the emulator.
-
7/25/2019 Android App Dev 2
9/28
After a few minutes the emulator will be started and you will be able to see your
HelloWorld application in it.
On the screen you can see Status Bar, Action Bar and Navigation Bar. You have nothing
to do with the Status Bar and Navigation Bar, it is being managed by System. The Action
bar contains the logo and title of your application and you can also open menu by click
Menu button (three vertical dots) on it. Layout Area where we place all UI controls.
Design Layout
As it is mentioned earlier Eclipse generates by default one layout. In emulator the
application is showing only Hello World! text in the middle of the screen. Open the layout
from /res/layout/activity_main.xml and you can see the layout in designer in the middle area
Eclipse. You can change view from graphic or xml as per your wish. Eclipse provides you
both option in the bottom of the layout Graphical Layout or activity_main.xml.
-
7/25/2019 Android App Dev 2
10/28
In the designer you can see all available controls for UI design in different tabs e.g. Text
Fields, Layouts etc. I will create a UI given below in image for understanding the basic
component of Android application. Lets start playing with the layout.
-
7/25/2019 Android App Dev 2
11/28
This layout is designed using Linear Layout. LinearLayout is a view group that aligns
children in single direction, Horizontal or Vertical. The attribute android:orientation is used to
set the direction. E.g. android:orientation="vertical". Eclipse provides a Property Pan on the
right-hand side of the screen to set the properties from designer. TextView control is a
readonly text. You can only view it and cannot edit it. EditText control is use for getting
input from the user. Button is a standard button control and on the click event we perform
action accordingly.
Hide Shrink Copy Code
-
7/25/2019 Android App Dev 2
12/28
android:textStyle="bold" />
-
7/25/2019 Android App Dev 2
13/28
android:layout_height="wrap_content"
android:text="@string/open_activity_title" />
.
.
.
One thing you must notice in this UI, I havent registered any event for the buttons here in
layout. We can do here using onClick attribute, but it is not a good way to do here. Now itis time to understand the some properties/attributes of different UI elements.
Properties
layout_width & layout_height:These properties are used for setting the width and height of
the element. The possible values are fill_parent, match_parent, wrap_content and any
number (e.g. 100dp). The fill_parent and match_parent are same. fill_parent is renamed to
match_parent in API level 8 and higher, which means that the view want to as big as
parent (minus padding). wrap_content means set the width or height according to the
content or child.
android:background="@drawable/header_bg_royal": This property is similar to the other
programming languages to set background of the element. The interesting point here is to
set the background from the resources of the android application. The header_bg_royal is
the image which I pasted in the res/drawable-hdpi. You can see complete path in the image
below.
-
7/25/2019 Android App Dev 2
14/28
In layout xml file we can access resources using @ sign at the start of resource name.
@drawable means get the resource from the drawable folder. You can see more than one
drawable folders e.g. drawable-hdpi, drawable-ldpi etc. The reason for multiple folders is we
have large verity of client machine some is using tables, Note, mobiles etc. The screen size
and resolution varies client to client. You can create your icons, image for differentresolution and put it into the appropriate folder. Android OS will automatically pick best
suitable image for displaying. @drawable/header_bg_royal means get header_bg_royal
image/resource from drawable.
android:text="@string/intent_title": I hope you have understood what purpose of @ sign is
and what does string means after it and what intent_title is. Before explaining it again, let
me explain why I need to put all my strings in the resource files. If I hard code the text of
any control in xml file, the Eclipse gives warning to move it into the resource file. You can
see warning in the image given below, when I set the button text property is Button,Eclipse is giving warning message to move it into the string resource file.
The question is why it should be string resource. Although you can hardcode it, but it is not
a good programming practice. Suppose you have 10 occurrence of text in different activities
or layout. For changing that text you need to go into each activity/layout and change there.
If you put that text in the string resources then the change require only one place. You can
also create your application multilingual using these resource files.
@string/intent_title means get the value of intent_title from the string resource.
android:textColor="@color/white_color": For setting the fore color of the text, get color valuefrom resource file. In next section you will get to know how can we add value in resource
file.
android:id="@+id/btn_phone_call": If you want to access any control in code, assign it an
id. The + sign with @ means if the resource exist then use it otherwise create it.
@+id/btn_phone_call mean use the btn_phone_call in id resource and if it does exists then
create it.
Resource File
-
7/25/2019 Android App Dev 2
15/28
Eclipse provides you a provision to add resources using designer. You can also use xml
view to add resources. You can add different type of resources e.g. string, color, array etc.
I will not go in the detail of resources in this article. You can read more about it in differenttutorials.
Activity
It is a most important concept of Android development. An activity represents the screen in
an application. You can use it in different ways as floating window or embedded inside of
another activity etc. The class must be inherited from Activity class for your activity. extends
keyword is used for inheritance in java.
Hide Copy Code
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
-
7/25/2019 Android App Dev 2
16/28
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;}
}
onCreate method is used to initialize your activity. We usually call setContentView() method
to define the layout for the activity and also get UI controls using findViewByid() that are
required to interact programmatically.
super.onCreate() means call the onCreate method of base class. Super is used to access
the base class in java. The layout that is associated with this activity is in
activity_main.xml . You can get the id of that resource using R.layout.activity_main. R is the
auto generated class that maintains the resources and we use it to access to resources in
code. setContentView(R.layout.activity_main) sets the layout for activity.
onCreateOptionsMenu() method is used to handle the menu for that activity. I will explain it
later.
Access Control/View in Activity
After setting the content view of the activity, you can get control using findViewById(viewId)
method. It takes the view/control id as a parameter and returns the view that is found. View
is the base class for widgets, which are used to create interactive UI components (buttons,
text fields, etc.). We need to cast the view into the original widgets.
Hide Copy Code
Button btnPhoneCall = (Button)findViewById(R.id.btn_phone_call);
EditText txtName = (EditText)findViewById(R.id.txt_name);
In above sample code findViewById(R.id.btn_phone_call) finds the button having id passed
as parameter and finds the
-
7/25/2019 Android App Dev 2
17/28
@Override
public void onClick(View v) {
onPhoneCallClick();
}
});
There are three ways to implement listeners in java and they have their own advantages
and disadvantages. These three ways are
Inline implementation
Using the implements keyword
By using variables
You can study about these different ways on the internet. In the above example I register
listener for button using Inline implementation. Call setOnClickListener() method of button
and pass OnClickListener as an parameter. In OnClickListener handler override the onClick
event. When user will click on button, it will call onClick event. You can write your code
inside onClick.
Intent
Intent is the asynchronous message which allows android components to request
functionality from the other component of the Android System. It represents an apps Intent
to do something. We can use intents for different tasks e.g. making call, open map, open
activity etc. But most often they are used to start another activity.
How to Start another Activity
Till here, you have learnt about activity, layout resources and intent. Create another activity
name LifeCycleActivity and create its layout activity_life_cycle.xml in layout folder. It is
better to use Eclipse to add activity in your application that automatically generates layout
for that activity and automatically put necessary information of activity in
AndroidManifest.xml file. Otherwise you need to do it manually. Right click on your
application in Package Explorer pane in left-hand side and click on New > Other. It will
open a popup window where you need to select Android Activity and after completing the
wizard, it will create a Activity and default layout for you.
-
7/25/2019 Android App Dev 2
18/28
Create object of Intent class and pass it to the startActivity() method. Intent class has
different type of constructor, for starting activity pass context and activity class to the object.
Activity class is the subclass of context, so pass this as a first parameter and the pass the
class as to which the system should deliver the intent. Then pass this intent to the
startActivity() method and your second activity will be started.
Hide Copy Code
Intent intent = new Intent(this, LifeCycleActivity.class);
startActivity(intent);
There are some other methods for starting activity. You can study about them too.
startActivityForResult(intent, requestCode)
startActivityFromChild(child, intent, requestCode)
startActivityFromFragment(fragment, intent, requestCode)
Open Phone Dialer
Launching the Phone dialer is also quite easy in Android you can send message to Android
OS to run specific application using intent
Hide Copy Code
Uri uri = Uri.parse("tel:03361122334");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
-
7/25/2019 Android App Dev 2
19/28
-
7/25/2019 Android App Dev 2
20/28
Hide Shrink Copy Code
@Override
protected void onCreate(Bundle savedInstanceState) {
}
@Override
protected void onStart() {
}
@Override
protected void onRestart() {
}
@Override
protected void onResume() {
}
@Override
protected void onPause() {
}
@Override
protected void onStop() {
}
@Override
protected void onDestroy() {
}
onCreate() method called when activity is first created. onStart() called when activity is
visible to user and onResume() called when application starts interaction with the user. At
this point your activity comes at the top of activity stack.
-
7/25/2019 Android App Dev 2
21/28
onPause() method called when system is about to start resuming of previous activity. It is
typically used to save the unsaved data to persist it, stop animation or other work that can
consume CPU.
onStop() method called when the activity is no longer visible to the user, because anotheractivity has been resumed and is covering this one.
onRestart() called after your activity has been stopped. It called prior to being started again.
onDestroy() called before your activity is destroyed.
Create Menu
For creating menu, open the res/menu/activity_main.xml file and add your menu items in it.
Set id and title for each menu item and then you can handle this menu item by id in
activity class.
Hide Copy Code
-
7/25/2019 Android App Dev 2
22/28
android:id="@+id/menu_exit"
android:showAsAction="never"
android:title="@string/menu_exit"/>
For handling on click method of the menu item override theonOptionsItemSelected(MenuItem
item) method in your activity. This method fires whenever user click/select any menu item.
For handling different actions for each element get the id of the selected menu item using
getItemId() method and perform action for each menu item.
Hide Shrink Copy Code
@Overridepublic boolean onOptionsItemSelected(MenuItem item) {
boolean handled = false;
int id = item.getItemId();
switch(id)
{
case R.id.menu_settings:{
Toast.makeText(this, "Clicked on setting menu item.",
Toast.LENGTH_SHORT).show();
handled = true;
break;
}
case R.id.menu_other:
{
onOpenActivity();
handled = true;
break;
}
case R.id.menu_exit:
{
finish();
handled = true;
-
7/25/2019 Android App Dev 2
23/28
break;
}
default:
{
handled = super.onOptionsItemSelected(item);}
}
return handled;
}
Each activity can has its own menu. You can put different menu items for each activity andhandle it differently. Some activity cannot have any menu. For this dont override
onCreateOptionsMenu method in your activity. In this method we inflate menu items to
menuInflater.
License
This article, along with any associated source code and files, is licensed under The Code
Project Open License (CPOL)
Android Development For Beginners
Thank you for signing up for the course! We look forward to working with you and hearing
your feedback in our forums.
Need help getting started?
Find an answer to your question in theUdacity FAQ
Learn about different parts of the Classroom in Udacity Introduction
Contents
1Course Resourceso 1.1 Additional Documentation
http://www.codeproject.com/info/cpol10.aspxhttp://www.codeproject.com/info/cpol10.aspxhttps://www.udacity.com/faqhttps://www.udacity.com/faqhttps://www.udacity.com/wiki/udacityintroductionhttps://www.udacity.com/wiki/ud837#course-resourceshttps://www.udacity.com/wiki/ud837#course-resourceshttps://www.udacity.com/wiki/ud837#additional-documentationhttps://www.udacity.com/wiki/ud837#additional-documentationhttps://www.udacity.com/wiki/ud837#additional-documentationhttps://www.udacity.com/wiki/ud837#course-resourceshttps://www.udacity.com/wiki/udacityintroductionhttps://www.udacity.com/faqhttp://www.codeproject.com/info/cpol10.aspxhttp://www.codeproject.com/info/cpol10.aspx -
7/25/2019 Android App Dev 2
24/28
o 1.2 Downloadable Materials
2Course Syllabus
o 2.1 Lesson 1: Building Layouts
o 2.2 Lesson 2: Making an App Interactive
o
2.3 Lesson 3: Work with the Android Framework and Control Flow 3Acknowledgements
Course Resources
Additional Documentation
1. The Android XML Visualizer
2.
Common Android Views Cheat-sheet
3.
Java Data Types Cheat-Sheet
4. Layout Cheat-Sheet
5.
Vocabulary Words
6.
Android Developers Website
7. The Material Design Specification
Downloadable Materials
You can download Supplemental Materials, Lesson Videos and Transcripts from
Downloadables (bottom right corner of the Classroom) or from the Dashboard (first option
on the navigation bar on the left hand side).
The final code for the course is freely available in threeGithubrepositories. Github is a
website that facilitates sharing code online. Code on Github is organized via the Gitversion
control system. To learn more about Git and Github, consider taking ourversion control
course.
1.
Code forHappy Birthday
2. Code forJust Java
3.
Code forCourt Counter
Course Syllabus
Lesson 1: Building Layouts
https://www.udacity.com/wiki/ud837#downloadable-materialshttps://www.udacity.com/wiki/ud837#downloadable-materialshttps://www.udacity.com/wiki/ud837#course-syllabushttps://www.udacity.com/wiki/ud837#course-syllabushttps://www.udacity.com/wiki/ud837#course-syllabushttps://www.udacity.com/wiki/ud837#lesson-1-building-layoutshttps://www.udacity.com/wiki/ud837#lesson-1-building-layoutshttps://www.udacity.com/wiki/ud837#lesson-2-making-an-app-interactivehttps://www.udacity.com/wiki/ud837#lesson-3-work-with-the-android-framework-and-control-flowhttps://www.udacity.com/wiki/ud837#acknowledgementshttps://www.udacity.com/wiki/ud837#acknowledgementshttp://labs.udacity.com/android-visualizer/#/android/sandboxhttp://labs.udacity.com/android-visualizer/#/android/sandboxhttps://s3-us-west-1.amazonaws.com/udacity-content/PDFs/Common+Android+Views+Cheat+Sheet+%281%29.pdfhttps://s3-us-west-1.amazonaws.com/udacity-content/PDFs/Common+Android+Views+Cheat+Sheet+%281%29.pdfhttp://labs.udacity.com/images/Java-Data-Types-Cheat-Sheet.pdfhttp://labs.udacity.com/images/Java-Data-Types-Cheat-Sheet.pdfhttp://labs.udacity.com/images/Layout-Cheat-Sheet.pdfhttp://labs.udacity.com/images/Layout-Cheat-Sheet.pdfhttps://developers.google.com/android/for-all/vocab-words/https://developers.google.com/android/for-all/vocab-words/http://developer.android.com/index.htmlhttp://www.google.com/design/spec/material-design/introduction.htmlhttp://www.google.com/design/spec/material-design/introduction.htmlhttps://github.com/https://github.com/https://git-scm.com/https://git-scm.com/https://www.udacity.com/course/how-to-use-git-and-github--ud775https://www.udacity.com/course/how-to-use-git-and-github--ud775https://www.udacity.com/course/how-to-use-git-and-github--ud775https://www.udacity.com/course/how-to-use-git-and-github--ud775https://github.com/udacity/Happy-Birthdayhttps://github.com/udacity/Happy-Birthdayhttps://github.com/udacity/Happy-Birthdayhttps://github.com/udacity/Just-Javahttps://github.com/udacity/Just-Javahttps://github.com/udacity/Court-Counterhttps://github.com/udacity/Court-Counterhttps://github.com/udacity/Court-Counterhttps://github.com/udacity/Court-Counterhttps://github.com/udacity/Just-Javahttps://github.com/udacity/Happy-Birthdayhttps://www.udacity.com/course/how-to-use-git-and-github--ud775https://www.udacity.com/course/how-to-use-git-and-github--ud775https://git-scm.com/https://github.com/http://www.google.com/design/spec/material-design/introduction.htmlhttp://developer.android.com/index.htmlhttps://developers.google.com/android/for-all/vocab-words/http://labs.udacity.com/images/Layout-Cheat-Sheet.pdfhttp://labs.udacity.com/images/Java-Data-Types-Cheat-Sheet.pdfhttps://s3-us-west-1.amazonaws.com/udacity-content/PDFs/Common+Android+Views+Cheat+Sheet+%281%29.pdfhttp://labs.udacity.com/android-visualizer/#/android/sandboxhttps://www.udacity.com/wiki/ud837#acknowledgementshttps://www.udacity.com/wiki/ud837#lesson-3-work-with-the-android-framework-and-control-flowhttps://www.udacity.com/wiki/ud837#lesson-2-making-an-app-interactivehttps://www.udacity.com/wiki/ud837#lesson-1-building-layoutshttps://www.udacity.com/wiki/ud837#course-syllabushttps://www.udacity.com/wiki/ud837#downloadable-materials -
7/25/2019 Android App Dev 2
25/28
In this lesson you'll learn about various Views and ViewGroups used to make layouts for
your Android apps. Topics include:
Writing XML
TextViews and ImageViews Styling your views
Positioning your views with ViewGroups
LinearLayout weight
Using RelativeLayout to position views relative to each other
Following Lesson 1 is Practice Set 1, where you will:
DownloadAndroid Studio
Make a Hello World Application and run it on your phone
Create a Birthday Card Application
Create your own custom card and video for the Make Your Own Cardshowcase!
Lesson 2: Making an App Interactive
https://developer.android.com/sdk/index.htmlhttps://developer.android.com/sdk/index.htmlhttps://developer.android.com/sdk/index.htmlhttp://discussions.udacity.com/t/make-your-own-card/19643http://discussions.udacity.com/t/make-your-own-card/19643https://developer.android.com/sdk/index.html -
7/25/2019 Android App Dev 2
26/28
In this lesson you'll start learning Java by creating the JustJava coffee ordering app seen
below:
Topics include:
Making a button click execute code
Having the app evaluate math expressions
Declaring, initializing and using variables
Variable scope
Nested layouts
The String datatype
Combining Strings, ints and variables with concatenation
Following Lesson 2 is Practice Set 2, where you will:
Identify incorrect variable declarations and initializations
Determine program output
Determine whether variables should be local or global
Build the CourtCounter basketball scoring app seen below:
-
7/25/2019 Android App Dev 2
27/28
Lesson 3: Work with the Android Framework and Control Flow
In this lesson you'll finish the JustJava coffee ordering app by adding more coffee ordering
options and nuanced control flow. You'll learn about object oriented programming so that
you can harness several features of the Android Framework. Topics include:
Method declaration and calling
Parameters and return values
An introduction to Object Oriented Programming
Changing the attributes of your views dynamically
Creating conditional logic using if/else statements
Following Lesson 3 is the course's final project 5 Things. For the final project you will
create an educational app that teaches the user five things. During this project you will:
Plan out your app design prior to coding, based on user feedback and needs
Take an app layout from drawing to XML code
Create, position, and style views
-
7/25/2019 Android App Dev 2
28/28
Create interactivity through button clicks and Java code
Students enrolled in the class for pay can submit the final project for review. To learn more
about the final project, clickhere.
https://www.udacity.com/course/viewer#%21/c-ud837/l-4040108662/m-4606438835https://www.udacity.com/course/viewer#%21/c-ud837/l-4040108662/m-4606438835https://www.udacity.com/course/viewer#%21/c-ud837/l-4040108662/m-4606438835https://www.udacity.com/course/viewer#%21/c-ud837/l-4040108662/m-4606438835
top related