ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar,...

16

Transcript of ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar,...

Page 1: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT
Page 2: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT

ANDROID

Page 3: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT
Page 4: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT

ANDROID

Prasanna Kumar DixitDirector, Interface Software, Bhubaneswar, Odisha, India.

VIKAS® PUBLISHING HOUSE PVT LTD

Page 5: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT

iv

VIKAS® PUBLISHING HOUSE PVT LTDE-28, Sector-8, Noida-201301 (UP) IndiaPhone: +91-120-4078900 • Fax: +91-120-4078999Registered Office: 576, Masjid Road, Jangpura, New Delhi-110 014, India

E-mail: [email protected] • Website: ww.vikaspublishing.com • Ahmedabad: 305, Grand Monarch, 100ft Shyamal Road, Near Seema Hall, Ahmedabad-380 051 • Ph. +91-79-65254204, +91-9898294208 • Bengaluru : First Floor, N.S. Bhawan, 4th Cross, 4th Main, Gandhi Nagar, Bengaluru-560 009 • Ph. +91-80-22281254, 22204639 • Chennai : E-12, Nelson Chambers, 115, Nelson Manickam Road, Aminjikarai Chennai-600 029 • Ph. +91-44-23744547, 23746090 • Hyderabad : Aashray Mansion, Flat-G (G.F.), 3-6-361/8, Street No. 20, Himayath Nagar, Hyderabad-560 029 • Ph. +91-40-23269992, Fax +91-23269993 • Kolkata : 82, Park Street, Kolkata-700 017 • Ph. 91-33-22837880 • Mumbai : 67/68, 3rd Floor, Aditya Industrial Estate, Chincholi Bunder, Behind Balaji International School & Evershine Mall, Malad (West), Mumbai-400 064 • Ph. +91-22-28772545, 28768301 • Patna : Flat No. 101, Sri Ram Tower, Beside Chiraiyatand Over Bridge, Kankarbagh Main Road, Kankarbagh, Patna-800 020 • Ph. +91-612-2351147

Android

ISBN: 9789325977884

First Edition 2014

Vikas® is the registered trademark of Vikas Publishing House Pvt Ltd.Copyright Author

All rights reserved. No part of this publication which is material protected by this copyright notice may be reproduced or transmitted or utilized or stored in any form or by any means now known or hereinafter invented, electronic, digital or mechanical, including photocopying, scanning, recording or by any information storage or retrieval system, without prior written permission from the publisher.

Information contained in this book has been published by VIKAS® Publishing House Pvt Ltd and has been obtained by its Authors from sources believed to be reliable and are correct to the best of their knowledge. However, the Publisher and its Authors shall in no event be liable for any errors, omissions or damages arising out of use of this information and specifically disclaim any implied warranties or merchantability or fitness for any particular use. Disputes if any are subject to Delhi Jurisdiction only.

Printed in India

Page 6: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT

DEDICATED TO

BIBLE

RAMAYANA

HOLY KORAN

MAHABHARATA

GURU GRANTH SAHIB

AND ALL THE OTHER HOLY BOOKS

AS NO BOOK CAN BE BETTER THAN THESE BOOKS.

Page 7: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT
Page 8: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT

vii

One is greater than zero: this is the motto behind this book. If some knowledge is gained after reading this book then it is more than zero, so my mission will be successful. Android is the in-thing in the market for everyone-young to old from; top to bottom; in India and the world. Developing the android application has become a; modern day past time. The purpose here is to say how simple the Android programming which can easily be developed. Now-a-days every web application is brought to the mobiles so that the information will on the finger tips. If we look at the market we find that the scope of Android is so huge that anyone who knows android applications can easily get a job. Due to all the above factors, Android is now a hot technology among the programmers. Companies like Samsung, LG, Sony, HTC etc are using Android extensively and are growing manifold. In the near future the market of Android will be much more that now.

So Android is a movement that has moved the data from laptop to hand held devices like mobile. Competition for Android is there but is in the front runner in mobile technology by a long distance. Good knowledge in basic Java will help you to understand and develop the technology and Android apps. Many universities in India and across the world are now teaching Android in their syllabus which shows the importance of this subject. This book can be read by anyone who knows Java and XML concepts. This book is a step forward to do a plain Android example. This book is written with lot of diagrams with explanations which will help a student to understand fast. This book is also attached with a project, if any programmer tries to run that project will be greatly benifitted to learn Android professionally. It is always challenge to give a project in Android which I have tried to give. So it is a very useful book both for beginners and professionals.

I shall be glad to hear from the users of the book so that the way forward to design a new edition for me becomes easier. I can be reached at [email protected]

Prasanna Kumar Dixit

PREFACE

Page 9: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT

My acknowledgement goes to everyone who has come in my life: my Father-who taught me how to work hard, my Mother- who taught us love in different forms, my wife-who has taught me to be passionate and more romantic to the profession and given the internal energy and support, my daughters-so good with all day chatting and vibrancy which gives me the energy to do work, my brother-always helping me without any conditions, my sister-for giving and loving me with unconditional love, my neice and nephew-give me love like my daughters and sons.

I thank all my teachers of unit-IV of Bhubaneswar, B.S High School Sundargarn, SCS College Puri and Institute of Engineers (Chennai). I specially thank the lecturers of ITER Odisha from where I have completed my M.Tech. Their dedication is fantastic. I specially thank Mr. Hemant kumar Pradhan (M. Tech.) from whom I got support in Android, Ms. Bijaya Bhatt (M. Tech.) whose inspiration in the topic is brilliant. Then come many persons who have helped me from time to time, thanks to them. Just to mention few of them, Lalto sir, Sambit, Sabyasachi who are another few persons who helped me in Android. Then come Swati, Sruti, Nibedita, Mahaprasad , Abhipsa , Sidhartha , Rosalin, Archana Sahoo, Abinash, Manisha, J.M.Pany, Sunil, Amit, Abhishek, and Aryan all of whom have been fantastic in their work. Most of the credit for this book goes to my students whom I am training as their response has encouraged me to write the book. Manas Pradhan is another name who has also helped me a lot. Finally thanks to almighty to give me courage to write this book. At last I have to give thank to Mr. Santosh Verma, Mr. Sunil Thakur, Mr. N.Poddar, Mr. Suvendu and the whole editorial team of Vikas for giving such a beautiful look to this book.

Prasanna Kumar Dixit

ACKNOWLEDGEMENT

Page 10: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT

ix

1. INTRODUCTION TO ANDROID OPERATING SYSTEM

1.1 What is Android? 2 1.2 What is “Open Handset Alliance”? 2 1.3 Android Ecosystem 3 1.4 Why android??? 4 1.5 Android versions 5 1.6 Android Activity 6 1.7 Features of Android 7 1.8 Android Architecture: 8 1.9 Stack Linux Kernel 15

2. CONFIGURATION OF ANDROID ENVIRONMENT

2.1 Operating System 18 2.2 Java JDK 18 2.3 Android SDK 18 2.4 Android Development Tools (ADT) 20 2.5 Android Virtual Devices (AVDs) 21 2.6 Emulators 22 2.7 Dalvik Virtual Machine 23 2.8 Difference between Java Virtual Machine (JVM) and Dalvik Virtual Machine (DVM) 26 2.9 Steps To Install and Configure Eclipse and SDK 27

CONTENTS

Page 11: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT

x

3. CREATE THE FIRST ANDROID APPLICATION

3.1 Directory Structure 39

4. ANDROID USER INTERFACE

4.1 Understanding the components of a screen 42 4.1.1 Linear Layout 44 4.1.2 Absolute Layout: 56 4.1.3 Frame Layout: 59 4.1.4 Relative Layout 60 4.1.5 Table Layout 64

5. DESIGNING YOUR USER INTERFACE WITH VIEW

5.1 TextView 68 5.2 Button - A standard push button 69 5.3 ImageButton 71 5.4 EditText 73 5.5 CheckBox 74 5.6 ToggleButton 80 5.7 RadioButton and RadioGroup 83 5.8 Progress Bar 87 5.9 Autocomplete TextView 90 5.10 Spinner 92 5.11 ListView 94 5.12 GridView 97 5.13 ImageView 99 5.14 Scrollview 102 5.15 Custom Toast Alert 106 5.16 Time and Date Picker 109

Page 12: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT

xi

6. ACTIVITY

6.1 Introduction 119 6.2 Intent 119 6.3 Intent _ filter 127 6.4 Activity life cycle 127 6.5 Broadcast Life cycle 130 6.6 Service 130

7. MULTIMEDIA

7.1 Android System Architecture 142 7.2 Play Audio and Video 144 7.2.1 Play Audio 144 7.3 Text to Speech 156

8. SQLITE DATABASE IN ANDROID

8.1 SQLite Database 166 8.2 Why SQLite? 166

8.3 Creation and connection of the database 167 8.4 Extracting value from a Cursors 173 8.5 Transactions 175

9. TELEPHONING AND MESSAGING

9.1 SMS Telephony 184

10. TELEPHONING AND MESSAGING

10.1 Creating the Project 200 10.2 Getting the maps API key 201 10.3.1 Displaying the zoom control 205 10.3.2 Changing Views 209

Page 13: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT

xii

10.4 Navigating to a Specific Location 211 10.4.1 Adding Markers 214 10.4.2 Getting the Location which was touched 218 10.4.3 Geocoding and Reverse Geocoding 220 10.4.4 Getting Location Data 223 10.4.5 Monitoring a Location 227

11. JSON

11.1 What is JSON? 230 11.2 XML and JSON 230 11.3 Use of JSON 231 11.4 Syntax and Rule of JSON 232 11.5 JSON Name/Value Pairs 232 11.6 JSON Values 232 11.7 JSON objects 233 11.8 JSON Arrays 234 11.9 How to JSON uses JavaScript syntax 234 11.10 Parsing JSON and XML 235 11.10.1 Parsing JSON Http Response 236 11.10.2 Parsing XML Http Response 236

12. PROJECT ON TRAFFIC SURVEY

12.1 Description 240

Page 14: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT

1 Introduction to Android Operating System

Chapter Overview:In this chapter, a new learner will able to understand the history of Android, the architecture, framework and various tools which are used in Android framework. You will also learn about the Android system overview with topics like Preferences and various supported File System in Android.

Operating Systems have developed a lot in last 15 years. It started from black and white phones to smart phones or mini computers. Nowadays one of the most widely used mobile OS is ANDROID.

• What is Android?

• What is “Open Handset Alliance”?

•Android Ecosystem

•Why Android?

•Android Version

•Features of Android

•Architecture of Android

•Stack Overview

Page 15: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT

Android2

Android Inc was founded in Palo Alto of California, U.S. by Andy Rubin, Rich Miner, Nick Sears and Chris White during 2003. After that it was acquired by Google in 2005.

1.1 What is Android? Android is a stack of software for mobile devices that are an Operating System, Middleware

and Key Applications.

Figure 1.1: Android Operating System

Android is a Linux-based operating system which is designed for touchscreen mobile devices like smartphones and tablet computers.

It is an open source technology that allows the software to be freely modified and distributed by device manufacturers, wireless carriers and developers.

Android was unveiled during 2007 along with the founding of the Open Handset Alliance.

Figure 1.2: Android Logo

1.2 What is “Open Handset Alliance”?Open Handset Alliance (OHA) was formed in November 2007.The OHA is the group that is in charge of the Android smartphones operating system. It was created by Google. The Open Handset Alliance (OHA) is a business alliance that consists of 47 companies for developing open standard platform for

Page 16: ANDROID - KopyKitab€¦ · ANDROID Prasanna Kumar Dixit Director, Interface Software, Bhubaneswar, Odisha, India. VIKAS® PUBLISHING HOUSE PVT LTD. iv VIKAS® PUBLISHING HOUSE PVT

Android

Publisher : SChand Publications ISBN : 9789325977884Author : Prasanna KumarDixit

Type the URL : http://www.kopykitab.com/product/11721

Get this eBook

20%OFF