what is android? workshop... · 2017. 4. 9. · what is android? android is an open source and...
TRANSCRIPT
What is Android?
Android is an open source and Linux-based Operating System for mobile devices such as
smartphones and tablet computers. Android was developed by the Open Handset Alliance, led by
Google, and other companies.
Android offers a unified approach to application development for mobile devices which means
developers need only develop for Android, and their applications should be able to run on
different devices powered by Android.
The first beta version of the Android Software Development Kit (SDK) was released by Google
in 2007 where as the first commercial version, Android 1.0, was released in September 2008.
Android Applications
Android applications are usually developed in the Java language using the Android Software
Development Kit.
Once developed, Android applications can be packaged easily and sold out either through a
store such as Google Play, SlideME, Opera Mobile Store, Mobango, F-droid and
the Amazon Appstore.
Categories of Android applications
There are many android applications in the market. The top categories are listed below :
History of Android
The code names of android ranges from A to N currently, such as Aestro, Blender, Cupcake,
Donut, Eclair, Froyo, Gingerbread, Honeycomb, Ice Cream Sandwitch, Jelly Bean, KitKat,
Lollipop and Marshmallow. Let's understand the android history in a sequence.
The latest version is Android 7 Nougat
Step 1 - System Requirements :
You will be delighted, to know that you can start your Android application development on
either of the following operating systems −
Microsoft® Windows® 10/8/7/Vista/2003 (32 or 64-bit)
Mac® OS X® 10.8.5 or higher, up to 10.9 (Mavericks)
GNOME or KDE desktop
Second point is that all the required tools to develop Android applications are open source and
can be downloaded from the Web. Following is the list of software's you will need before you
start your Android application programming.
Java JDK5 or later version
Java Runtime Environment (JRE) 6
Android Studio
Step 2 - Setup Android Studio
Android Studio is the official IDE for android application development. It works based
on IntelliJ IDEA, You can download the latest version of android studio from Android Studio
2.2 Download, If you are new to installing Android Studio on windows, you will find a file,
which is named as android-studio-bundle-143.3101438-windows.exe.So just download and run
on windows machine according to android studio wizard guideline.
If you are installing Android Studio on Mac or Linux, You can download the latest version
from Android Studio Mac Download,or Android Studio Linux Download, check the
instructions provided along with the downloaded file for Mac OS and Linux. This tutorial will
consider that you are going to setup your environment on Windows machine having Windows
8.1 operating system.
Installation
So let's launch Android Studio.exe, Make sure before launch Android Studio, Our Machine
should require installed Java JDK. To install Java JDK, take a reference of Android
environment setup
Once you launched Android Studio, it’s time to mention JDK7 path or later version in android
studio installer.
Below the image initiating JDK to android SDK
Need to check the components, which are required to create applications, below the image has
selected Android Studio, Android SDK, Android Virtual Machine and performance (Intel
chip).
Need to specify the location of local machine path for Android studio and Android SDK, below
the image has taken default location of windows 8.1 x64 bit architecture.
Need to specify the ram space for Android emulator by default it would take 512MB of local
machine RAM.
At final stage, it would extract SDK packages into our local machine, it would take a while time
to finish the task and would take 2626MB of Hard disk space.
After done all above steps perfectly, you must get finish button and it open android studio
project with Welcome to android studio message as shown below
You can start your application development by calling start a new android studio project. in a
new installation frame should ask Application name, package information and location of the
project.
After entered application name, it going to be called select the form factors your application
runs on, here need to specify Minimum SDK, in our tutorial, I have declared as API23: Android
6.0(Mashmallow)
The next level of installation should contain selecting the activity to mobile, it specifies the
default layout for Applications
At the final stage it going to be open development tool to write the application code.
Step 3 - Create Android Virtual Device
To test your Android applications, you will need a virtual Android device. So before we start
writing our code, let us create an Android virtual device. Launch Android AVD Manager
Clicking AVD_Manager icon as shown below
After Click on a virtual device icon, it going to be shown by default virtual devices which are
present on your SDK, or else need to create a virtual device by clicking Create new Virtual
device button
If your AVD is created successfully it means your environment is ready for Android application
development. If you like, you can close this window using top-right cross button. Better you re-
start your machine and once you are done with this last step, you are ready to proceed for your
first Android example but before that we will see few more important concepts related to
Android Application Development.
Anatomy of Android Application
Before you run your app, you should be aware of a few directories and files in the Android
project −
Sr.No. Folder, File & Description
1
Java
This contains the .java source files for your project. By default, it includes
an MainActivity.java source file having an activity class that runs when your
app is launched using the app icon.
2
res/drawable-hdpi
This is a directory for drawable objects that are designed for high-density
screens.
3
res/layout
This is a directory for files that define your app's user interface.
4
res/values
This is a directory for other various XML files that contain a collection of
resources, such as strings and colours definitions.
5
AndroidManifest.xml
This is the manifest file which describes the fundamental characteristics of the
app and defines each of its components.
6
Build.gradle
This is an auto generated file which contains compileSdkVersion,
buildToolsVersion, applicationId, minSdkVersion, targetSdkVersion,
versionCode and versionName
Following section will give a brief overview few of the important application files.
The Main Activity File
The main activity code is a Java file MainActivity.java. This is the actual application file
which ultimately gets converted to a Dalvik executable and runs your application. Following is
the default code generated by the application wizard for Hello World! application −
package com.example.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Here, R.layout.activity_main refers to the activity_main.xml file located in the res/layout folder.
The onCreate() method is one of many methods that are figured when an activity is loaded.
The Manifest File
Whatever component you develop as a part of your application, you must declare all its
components in a manifest.xml which resides at the root of the application project directory. This
file works as an interface between Android OS and your application, so if you do not declare
your component in this file, then it will not be considered by the OS. For example, a default
manifest file will look like as following file −
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tutorialspoint7.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here <application>...</application> tags enclosed the components related to the application.
Attribute android:icon will point to the application icon available under res/drawable-hdpi. The
application uses the image named ic_launcher.png located in the drawable folders
The <activity> tag is used to specify an activity and android:name attribute specifies the fully
qualified class name of the Activity subclass and the android:label attributes specifies a string to
use as the label for the activity. You can specify multiple activities using <activity> tags.
The action for the intent filter is named android.intent.action.MAIN to indicate that this activity
serves as the entry point for the application. The category for the intent-filter is
named android.intent.category.LAUNCHER to indicate that the application can be launched
from the device's launcher icon.
The @string refers to the strings.xml file explained below. Hence, @string/app_name refers to
the app_name string defined in the strings.xml file, which is "HelloWorld". Similar way, other
strings get populated in the application.
Following is the list of tags which you will use in your manifest file to specify different Android
application components −
<activity>elements for activities
<service> elements for services
<receiver> elements for broadcast receivers
<provider> elements for content providers
The Strings File
The strings.xml file is located in the res/values folder and it contains all the text that your
application uses. For example, the names of buttons, labels, default text, and similar types of
strings go into this file. This file is responsible for their textual content. For example, a default
strings file will look like as following file −
<resources>
<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
The Layout File
The activity_main.xml is a layout file available in res/layout directory, that is referenced by
your application when building its interface. You will modify this file very frequently to change
the layout of your application. For your "Hello World!" application, this file will have following
content related to default layout −
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
This is an example of simple RelativeLayout which we will study in a separate chapter.
The TextView is an Android control used to build the GUI and it have various attributes
like android:layout_width, android:layout_height etc which are being used to set its width and
height etc.. The @string refers to the strings.xml file located in the res/values folder. Hence,
@string/hello_world refers to the hello string defined in the strings.xml file, which is "Hello
World!".
Running the Application
Let's try to run our Hello World! application we just created. I assume you had created
your AVD while doing environment set-up. To run the app from Android studio, open one of
your project's activity files and click Run icon from the tool bar. Android studio installs the
app on your AVD and starts it and if everything is fine with your set-up and application, it will
display following Emulator window −
Congratulations!!! you have developed your first Android Application and now just keep
following rest of the tutorial step by step to become a great Android Developer. All the very
best.
Sample Example with code :
1. Addition of two numbers and Navigating to next screen.
Java file
package com.example.babu.myapp1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=(TextView)findViewById(R.id.t1);
TextView t2=(TextView)findViewById(R.id.t2);
final TextView t3=(TextView)findViewById(R.id.t3);
final EditText e1=(EditText)findViewById(R.id.e1);
final EditText e2=(EditText)findViewById(R.id.e2);
Button b1=(Button)findViewById(R.id.b1);
Button b2=(Button)findViewById(R.id.b2);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this, MyActivity2.class );
startActivity(i);
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a=Integer.parseInt(e1.getText().toString());
int b=Integer.parseInt(e2.getText().toString());
int c =a+b;
t3.setText(Integer.toString(c));
}
});
}
}
Xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.babu.myapp1.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="welcome to Android Class" />
<TextView
android:id="@+id/t1"
android:text="Enter First number"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/t2"
android:text="Enter second number"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/e2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/b1"
android:text="add"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="click next screen"
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2.Styles
Xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Workshop Styles "
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="@+id/imageButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/ic_launcher"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small font"
android:id="@+id/button"
android:layout_below="@+id/imageButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Font"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_alignRight="@+id/button"
android:layout_alignEnd="@+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:textSize="25dp" />
</RelativeLayout>
Java file
package com.example.babu.workshop2;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private ProgressDialog progress;
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress = new ProgressDialog(this);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView txtView = (TextView) findViewById(R.id.textView);
txtView.setTextSize(10);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView txtView = (TextView) findViewById(R.id.textView);
txtView.setTextSize(55);
}
});
}
}
3.
Registration and Login application
Xml files
1. <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.babu.worshopregistration2.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HOME PAGE" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REGISTRATION"
android:id="@+id/b1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOGIN"
android:id="@+id/b2"/>
</LinearLayout>
2. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.babu.worshopregistration2.Registration"
tools:showIn="@layout/activity_registration">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THIS IS REGISTRATION PAGE"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NAME "/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/e1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASSWORD "/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/e2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EMAIL "/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/e3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PHONE_NO "/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/e4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT"
android:id="@+id/b1" />
</LinearLayout>
3.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.babu.worshopregistration2.Login"
tools:showIn="@layout/activity_login">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NAME"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/e1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASSWORD"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/e2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/b1"/>
</LinearLayout>
Java files
1.registration
package com.example.babu.worshopregistration2;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Registration extends AppCompatActivity {
SQLiteDatabase sd=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
final EditText e1=(EditText)findViewById(R.id.e1);
final EditText e2=(EditText)findViewById(R.id.e2);
final EditText e3=(EditText)findViewById(R.id.e3);
final EditText e4=(EditText)findViewById(R.id.e4);
Button b1=(Button)findViewById(R.id.b1);
sd=openOrCreateDatabase("user",MODE_PRIVATE,null);
sd.execSQL("create table if not exists login(name varchar, password varchar,email
varchar, phone varchar)");
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name=e1.getText().toString();
String password=e2.getText().toString();
String email=e3.getText().toString();
String phone=e4.getText().toString();
sd.execSQL("insert into login
values('"+name+"','"+password+"','"+email+"','"+phone+"')");
Intent i=new Intent(Registration.this,welcome.class);
startActivity(i);
sd.close();
finish();
}
});
}
}
2. login
package com.example.babu.worshopregistration2;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends AppCompatActivity {
SQLiteDatabase sd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
final EditText e1=(EditText)findViewById(R.id.e1);
final EditText e2=(EditText)findViewById(R.id.e2);
Button b1=(Button)findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name=e1.getText().toString();
String password=e2.getText().toString();
sd=openOrCreateDatabase("user",MODE_PRIVATE,null);
Cursor c=sd.rawQuery("select * from login where name='"+name+"' and
password='"+password+"'",null) ;
c.moveToNext();
if(c.getCount()>0)
{
Intent i=new Intent(Login.this,welcome.class);
startActivity(i);
sd.close();
finish();
}
else
Toast.makeText(Login.this, "Wrong user name and wrong
password",Toast.LENGTH_SHORT).show();
}
});
}
}