• No results found

Android Programming painless

N/A
N/A
Protected

Academic year: 2021

Share "Android Programming painless"

Copied!
10
0
0

Loading.... (view fulltext now)

Full text

(1)

Android

Programming Painless

Tutorial Book

1

st Edition for Android 4.1

(2)

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 websitehttp://developer.android.com/index.html is essential for developing android applications.

(3)

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.

(4)

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.

(5)

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 requestthe user's response.

o A Dialog Notification, for Activity-related notifications.

(6)

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

int getDuration(): Return the duration.

int getGravity(): Get the location at which the notification should appear on the screen. static Toast makeText(Context context, int resId, int duration)

Make a standard toast that just contains a text view with the text from a resource. static Toast makeText(Context context, CharSequence text, int duration)

Make a standard toast that just contains a text view.

void setText(int resId)

Update the text in a Toast that was previously created using one of the makeText() methods.

void setText(CharSequence s)

Update the text in a Toast that was previously created using one of the makeText() methods.

void show()

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

(7)

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);

(8)

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:

(9)

Result should look like this:

(10)

Download the Pdf version of this book:

References

Related documents

BESPOKE PROTECTION: CATLOC 1005... BESPOKE PROTECTION:

Additional requirement in present document The requirements in the original table for Band VII apply to Bands XV and XVI as well... 8.2.6.2 UE Rx-Tx time difference

than three-quarters of the cases handled by the Capital Trial Division (77%) have been resolved prior to trial either through a plea agreement to a sentence of

Fitat intake in the study was obtained based on the results of interviews used SQFFQ form, then the list of food interview results processed used FP2 program package, the results

What I love doing is teaching others Psychic ESP skills, teaching them the REAL Huna, teaching them hypnosis and NLP…And interacting with others in my fields that are also world

This study is concerned with understanding the impact of students’ prior expectations and learning conceptions on their SWE transition and subsequent employability

When I calibrate the model to the corporate bonds market and run a horse race between relationship stability, matching efficiency and trading speed limit, a friction similar to those

Moreover our inference procedures do not reply on conditions ensuring point identi…cation of o and thus allow for the presence of general covariates in the marginal model for the