ch10.pdf
Document Details
Uploaded by ProdigiousQuantum
null
2021
Tags
Full Transcript
JTO Phase-II IT Android App Development 10 ANDROID APP DEVELOPMENT 10.1 INTRODUCTION Android tutorial or Android Studio tutorial covers basic and advanced concepts of android technology. Our Android development tutorial is deve...
JTO Phase-II IT Android App Development 10 ANDROID APP DEVELOPMENT 10.1 INTRODUCTION Android tutorial or Android Studio tutorial covers basic and advanced concepts of android technology. Our Android development tutorial is developed for beginners and professionals. Android is a complete set of software for mobile devices such as tablet computers, notebooks, smartphones, electronic book readers, set-top boxes etc. It contains a linux-based Operating System, middleware and key mobile applications. It can be thought of as a mobile operating system. But it is not limited to mobile only. It is currently used in various devices such as mobiles, tablets, televisions etc. 10.2 WHAT IS ANDROID Before learning all topics of android, it is required to know what is android. Android is a software package and linux based operating system for mobile devices such as tablet computers and smartphones. It is developed by Google and later the OHA (Open Handset Alliance). Java language is mainly used to write the android code even though other languages can be used. The goal of android project is to create a successful real-world product that improves the mobile experience for end users. 10.3 HISTORY OF ANDROID The history and versions of android are interesting to know. The code names of android ranges from A to J currently, such as Aestro, Blender, Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb, Ice Cream Sandwitch, Jelly Bean, KitKat and Lollipop. Let's understand the android history in a sequence. 1) Initially, Andy Rubin founded Android Incorporation in Palo Alto, California, United States in October, 2003. 2) In 17th August 2005, Google acquired android Incorporation. Since then, it is in the subsidiary of Google Incorporation. 3) The key employees of Android Incorporation are Andy Rubin, Rich Miner, Chris White and Nick SearsDifference between JDK, JRE, and JVM 4) Originally intended for camera but shifted to smart phones later because of low market for camera only. 5) Android is the nick name of Andy Rubin given by coworkers because of his love to robots. 6) In 2007, Google announces the development of android OS. 7) In 2008, HTC launched the first android mobile. JTO Phase –II DNIT Version 1.0 Sep 2021 Page 144 of 167 For Restricted Circulation JTO Phase-II IT Android App Development 10.4 ANDROID ARCHITECTURE AND SOFTWARE STACK android architecture or Android software stack is categorized into five parts: linux kernel native libraries (middleware), Android Runtime Application Framework Applications Figure 56: 1) LINUX KERNEL It is the heart of android architecture that exists at the root of android architecture. Linux kernel is responsible for device drivers, power management, memory management, device management and resource access. 2) NATIVE LIBRARIES On the top of linux kernel, their are Native libraries such as WebKit, OpenGL, FreeType, SQLite, Media, C runtime library (libc) etc. The WebKit library is responsible for browser support, SQLite is for database, FreeType for font support, Media for playing and recording audio and video formats. 3) ANDROID RUNTIME In android runtime, there are core libraries and DVM (Dalvik Virtual Machine) which is responsible to run android application. DVM is like JVM but it is optimized for mobile devices. It consumes less memory and provides fast performance. JTO Phase –II DNIT Version 1.0 Sep 2021 Page 145 of 167 For Restricted Circulation JTO Phase-II IT Android App Development 4) ANDROID FRAMEWORK On the top of Native libraries and android runtime, there is android framework. Android framework includes Android API's such as UI (User Interface), telephony, resources, locations, Content Providers (data) and package managers. It provides a lot of classes and interfaces for android application development. 5) APPLICATIONS On the top of android framework, there are applications. All applications such as home, contact, settings, games, browsers are using android framework that uses android runtime and libraries. Android runtime and native libraries are using linux kernal. 10.5 ANDROID CORE BUILDING BLOCKS Figure 57: An android component is simply a piece of code that has a well defined life cycle e.g. Activity, Receiver, Service etc. The core building blocks or fundamental components of android are activities, views, intents, services, content providers, fragments and AndroidManifest.xml. ACTIVITY An activity is a class that represents a single screen. It is like a Frame in AWT. VIEW A view is the UI element such as button, label, text field etc. Anything that you see is a view. INTENT Intent is used to invoke components. It is mainly used to: o Start the service o Launch an activity JTO Phase –II DNIT Version 1.0 Sep 2021 Page 146 of 167 For Restricted Circulation JTO Phase-II IT Android App Development o Display a web page o Display a list of contacts o Broadcast a message o Dial a phone call etc. SERVICE Service is a background process that can run for a long time. There are two types of services local and remote. Local service is accessed from within the application whereas remote service is accessed remotely from other applications running on the same device. CONTENT PROVIDER Content Providers are used to share data between the applications. FRAGMENT Fragments are like parts of activity. An activity can display one or more fragments on the screen at the same time. ANDROIDMANIFEST.XML It contains informations about activities, content providers, permissions etc. It is like the web.xml file in Java EE. ANDROID VIRTUAL DEVICE (AVD) It is used to test the android application without the need for mobile or tablet etc. It can be created in different configurations to emulate different types of real devices. ANDROID EMULATOR The Android emulator is an Android Virtual Device (AVD), which represents a specific Android device. We can use the Android emulator as a target device to execute and test our Android application on our PC. The Android emulator provides almost all the functionality of a real device. We can get the incoming phone calls and text messages. It also gives the location of the device and simulates different network speeds. Android emulator simulates rotation and other hardware sensors. It accesses the Google Play store, and much more JTO Phase –II DNIT Version 1.0 Sep 2021 Page 147 of 167 For Restricted Circulation JTO Phase-II IT Android App Development Figure 58: Testing Android applications on emulator are sometimes faster and easier than doing on a real device. For example, we can transfer data faster to the emulator than to a real device connected through USB. The Android emulator comes with predefined configurations for several Android phones, Wear OS, tablet, Android TV devices. 10.6 ANDROID PROGRAM COMPILATION (DALVIK VIRTUAL MACHINE | DVM) The Dalvik Virtual Machine (DVM) is an android virtual machine optimized for mobile devices. It optimizes the virtual machine for memory, battery life and performance. JTO Phase –II DNIT Version 1.0 Sep 2021 Page 148 of 167 For Restricted Circulation JTO Phase-II IT Android App Development Figure 59: 10.7 INSTALLATION OF ANDROID STUDIO Android Studio is the official IDE (Integrated Development Environment) for Android app development and it is based on JetBrains‟ IntelliJ IDEA software. Android Studio provides many excellent features that enhance productivity when building Android apps, such as: A blended environment where one can develop for all Android devices Apply Changes to push code and resource changes to the running app without restarting the app A flexible Gradle-based build system A fast and feature-rich emulator GitHub and Code template integration to assist you to develop common app features and import sample code Extensive testing tools and frameworks C++ and NDK support Built-in support for Google Cloud Platform, making it easy to integrate Google Cloud Messaging and App Engine, and many more. SYSTEM REQUIREMENTS Microsoft Windows 7/8/10 (32-bit or 64-bit) 4 GB RAM minimum, 8 GB RAM recommended (plus 1 GB for the Android Emulator) 2 GB of available disk space minimum, 4 GB recommended (500 MB for IDE plus 1.5 GB for Android SDK and emulator system image) 1280 x 800 minimum screen resolution JTO Phase –II DNIT Version 1.0 Sep 2021 Page 149 of 167 For Restricted Circulation JTO Phase-II IT Android App Development INSTALLATION GUIDE Step 1: Head over to this link to get the Android Studio executable or zip file. Step 2: Click on the Download Android Studio Button. Figure 60: Click on the “I have read and agree with the above terms and conditions” checkbox followed by the download button. Figure 61: JTO Phase –II DNIT Version 1.0 Sep 2021 Page 150 of 167 For Restricted Circulation JTO Phase-II IT Android App Development Click on the Save file button in the appeared prompt box and the file will start downloading. Step 3: After the downloading has finished, open the file from downloads and run it. It will prompt the following dialog box. Figure 62: Click on next. In the next prompt, it‟ll ask for a path for installation. Choose a path and hit next. Step 4: It will start the installation, and once it is completed, it will be like the image shown below. Figure 63: Click on next. JTO Phase –II DNIT Version 1.0 Sep 2021 Page 151 of 167 For Restricted Circulation JTO Phase-II IT Android App Development Figure 64: Step 5: Once “Finish” is clicked, it will ask whether the previous settings need to be imported [if the android studio had been installed earlier], or not. It is better to choose the „Don‟t import Settings option‟. Figure 65: Click the OK button. Step 6: This will start the Android Studio. JTO Phase –II DNIT Version 1.0 Sep 2021 Page 152 of 167 For Restricted Circulation JTO Phase-II IT Android App Development Figure 66: Meanwhile, it will be finding the available SDK components. Figure 67: Step 7: After it has found the SDK components, it will redirect to the Welcome dialog box. Figure 68: Click on Next. JTO Phase –II DNIT Version 1.0 Sep 2021 Page 153 of 167 For Restricted Circulation JTO Phase-II IT Android App Development Figure 69: Choose Standard and click on Next. Now choose the theme, whether the Light theme or the Dark one. The light one is called the IntelliJ theme whereas the dark theme is called Darcula. Choose as required. JTO Phase –II DNIT Version 1.0 Sep 2021 Page 154 of 167 For Restricted Circulation JTO Phase-II IT Android App Development Figure 70: Click on the Next button. Step 8: Now it is time to download the SDK components. Figure 71: Click on Finish. Components begin to download let it complete. JTO Phase –II DNIT Version 1.0 Sep 2021 Page 155 of 167 For Restricted Circulation JTO Phase-II IT Android App Development Figure 72: The Android Studio has been successfully configured. Now it‟s time to launch and build apps. Click on the Finish button to launch it. Step 9: Click on Start a new Android Studio project to build a new app. Figure 73: ANDROID PROJECT STRUCTURE AND DEBUGGING Figure 74: JTO Phase –II DNIT Version 1.0 Sep 2021 Page 156 of 167 For Restricted Circulation JTO Phase-II IT Android App Development Figure 75: Figure 76: JTO Phase –II DNIT Version 1.0 Sep 2021 Page 157 of 167 For Restricted Circulation JTO Phase-II IT Android App Development Figure 77: 10.8 BUILDING AND TESTING THE APP Build the project Go to Project folder->app->build->output->apk Copy the.apk file to your phone Allow install app from unknown source option Install and run the app. Figure 78: JTO Phase –II DNIT Version 1.0 Sep 2021 Page 158 of 167 For Restricted Circulation JTO Phase-II IT Android App Development 10.9 CREATING THE APP ICON AND CHANGE THE THEME WITH NO ACTION BAR Go to Project folder->app->source>main->res Navigate to mip-map folder Copy your icon Go to menifest file and specify name of youricon file 10.10 CREATING THE APP ICON AND CHANGE THE THEME WITH NO ACTION BAR 14.1 Figure 14.1 14.2 10.11 CREATE ACTIVITY AND FORMAT THE TEXTVIEW Add the TextView in constraint layout and set the margin to removeconstraint error Do following formatting by accessing the properties of label widget. JTO Phase –II DNIT Version 1.0 Sep 2021 Page 159 of 167 For Restricted Circulation JTO Phase-II IT Android App Development – ID – Text – Text appearance – Text color – Text Size – Font Family – Text Style – Layout height and Width as match parent with zero margin – Gravity to set alignment – Padding – Background – Alpha to set transparency 10.12 SHOW TOAST MESSAGE public class MainActivity extends AppCompatActivity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast.makeText(this,"This is Test Msg",Toast.LENGTH_SHORT).show(); } } 10.13 HANDLE THE CLICK EVENT OF BUTTON Draw the button in XML file and set ID of button Now set Text of Button and set any event name like “Clickme” in Onclick property option then write javacode public class MainActivity extends AppCompatActivity { @Override JTO Phase –II DNIT Version 1.0 Sep 2021 Page 160 of 167 For Restricted Circulation JTO Phase-II IT Android App Development protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void clickme(View view) { EditText name=(EditText)findViewById(R.id.et_name); String name1=name.getText().toString(); Toast.makeText(this,"Your Name is : "+name1,Toast.LENGTH_SHORT).show(); } } 10.14 CREATE ALERT DIALOG BOX ( JAVA CODE ) public class MainActivity extends AppCompatActivity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Are you sure!?").setMessage("Do you definitely want to do this?").setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this,"It's done!",Toast.LENGTH_SHORT).show(); } }).setNegativeButton("No",null).show(); }} JTO Phase –II DNIT Version 1.0 Sep 2021 Page 161 of 167 For Restricted Circulation JTO Phase-II IT Android App Development 10.15 OPEN SECOND ACTIVITY WITH POST VALUES public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void clickme(View view) { EditText name=(EditText)findViewById( R.id.editText); EditText pass=(EditText)findViewById( R.id.editText); String namestr=name.getText().toStri ng(); if(namestr.equals("vikas")) { Intent i=new Intent(getApplicationContext(),DisplayInfo.class); i.putExtra("value1",namestr); startActivity(i); }} } OPEN SECOND ACTIVITY WITH POST VALUES CONTINUED…. public class MainActivity extends AppCompatActivity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceStat e); setContentView(R.layout.displayinfo); Bundle extras=getIntent().getExtras(); String val1=extras.getString("value1"); Toast.makeText(getApplicationContext(),"First value"+ val1,Toast.LENGTH_SHORT).show(); TextView showuser=(TextView)findViewById(R.id.textview); JTO Phase –II DNIT Version 1.0 Sep 2021 Page 162 of 167 For Restricted Circulation JTO Phase-II IT Android App Development showuser.setText(val1); } } 10.16 SETTING UP THE LAUNCHER ACTIVITY To make any activity as a launcher activity just cut this code shown in blue and paste in your activity opening and closing TAG in Manifest file.