Android Programming painless

September 5, 2017 | Author: Camilus Raynaldo | Category: Android (Operating System), Programmer, Java (Programming Language), Websites, Search Engine Optimization
Share Embed Donate


Short Description

This book is intended to any person wanting to learn Android programming while giving to the reader the necessary founda...

Description

1st

Tutorial Book

Edition for Android 4.1

Android Programming Painless by Camilus Raynaldo

Android Programming Painless by Camilus Raynaldo

Copyright © 2012 Camilus Raynaldo. All rights reserved.

Portions of the book are exact reproductions or modifications of work created and shared by Google and are used according to terms described in the Apache 2.0 license.

Some Java examples used in chapter 1 are from http://www.tutorialspoint.com and creative common work

This book is totally free and is open to anyone wishing to make their first steps in Android programming. You can use this book for educational purposes; it is prohibited to make any commercial use.

Note: The Android developer website http://developer.android.com/index.html is essential for developing android applications.

Cover Image by Acuna Cesar ([email protected])

About the Author

Camilus Raynaldo is a software developer with more than seven years of experience in .Net and java development.

Other fields of interest are: Web development (HTML/CSS, XML) and oracle database. He graduated from the “INUQUA” in 2004 with a Bachelor of Science degree in computer science He writes android applications for personal use and works as IT Engineering at ONI (Citizen Identification System in Haiti) since 2007.

Preface

This book is intended to any person wanting to learn Android programming while giving to the reader the necessary foundation in Java and familiar with the java keywords used in Android. This book was designed to provide the foundation for a novice user without any prerequisites through detailed examples using the new Android SDK (Jelly Bean), and as a guide to set a standard for all future android programmers.

Chapter 4: Notifications

Several types of situations may arise that require you to notify the user about an event that occurs in your application. Some events require the user to respond and others do not. For example: o When an event such as saving a file is complete, a small message should appear to confirm that the save was successful. o If the application is running in the background and needs the user's attention, the application should create a notification that allows the user to respond at his or her convenience. o If the application is performing work that the user must wait for (such as loading a file), the application should show a hovering progress wheel or bar. Each of these notification tasks can be achieved using a different technique: o A Toast Notification, for brief messages that come from the background. o A Status Notification, for persistent reminders that come from the background and request the user's response. o A Dialog Notification, for Activity-related notifications.

Note: In this book you will use the toast notification

Toast Notifications A toast notification is a message that pops up on the surface of the window. It only fills the amount of space required for the message and the user's current activity remains visible and interactive. The notification automatically fades in and out, and does not accept interaction events. The class toast is in the package android.widget Tables below present methods and constants of the class toast.

Class Toast Constants int

LENGTH_LONG

Show the view or text notification for a long period of time.

int

LENGTH_SHORT

Show the view or text notification for a short period of time.

Public Methods getDuration(): Return the duration. int getGravity(): Get the location at which the notification should appear on the screen. int makeText(Context context, int resId, int duration) static Toast Make a standard toast that just contains a text view with the text from a resource. makeText(Context context, CharSequence text, int duration) static Toast Make a standard toast that just contains a text view. setText(int resId) void Update the text in a Toast that was previously created using one of the makeText() methods. setText(CharSequence s) void Update the text in a Toast that was previously created using one of the makeText() methods. show() void Show the view for the specified duration.

Create your first Toast 1- Create a new project and name it First Toast 2- Select the src folder, expand it, next expand the package and double click the MainActivity.java

3- Here is how to make a toast a) First, instantiate a Toast object with the makeText() method makeText(Context context, CharSequence text, int duration) This method takes three parameters: the application Context, the text message, and the duration for the toast. It returns a properly initialized Toast object b) Display the toast notification with the show() method Example 6 Toast toast = Toast.makeText(this, "First toast !", Toast.LENGTH_LONG); Toast. show() ;

You can also chain your methods and avoid holding on to the Toast object, like this: Toast toast = Toast.makeText(this, "First toast!",Toast.LENGTH_LONG).show();

Activity which hosts the toast is the Main Activity hence the this, our CharSequence text is "First toast!", and the duration of the toast Toast.LENGTH_LONG. 4- Add the toast in your FirstToast project MainActivity.java should look like this: package com.example.firsttoast; import android.os.Bundle; import android.app.Activity; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

Toast toast = Toast.makeText(this, "First Toast", Toast.LENGTH_LONG); toast.show(); } }

Note: Toast.LENGTH_LONG : Display the toast for 5 seconds Toast. LENGTH_SHORT : Display the toast for 3 seconds

5- Run the application Result should look something like this:

If you decide that the toast should appear at the top-left corner, you can set the gravity like this: toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 50);

Result should look like this:

You can change the toast position as you wish with the setGravity(int, int, int) method.

Download the Pdf version of this book: http://keroob.com/index.php/andro

View more...

Comments

Copyright © 2017 KUPDF Inc.
SUPPORT KUPDF