Sie sind auf Seite 1von 15

Using Toast

• Android Toast Example


• Android Toast can be used to display
information for the short period of time. A
toast contains message to be displayed quickly
and disappears after sometime.
• The android.widget.Toast class is the subclass
of java.lang.Object class.

1
Toast class
• Toast class is used to show notification for a
particular interval of time. After sometime it
disappears. It doesn't block the user
interaction.
• Constants of Toast class
• There are only 2 constants of Toast class which
are given below.

2
• Methods of Toast class
• The widely used methods of Toast class are
given below.

3
Android Toast Example
Toast toast=Toast.makeText(getApplicationContext(),
"Hello Javatpoint",Toast.LENGTH_SHORT);
toast.setMargin(50,50);
toast.show();
OR
Toast.makeText(getApplicationContext(),
"Hello Javatpoint",Toast.LENGTH_SHORT).show();

4
Working with Intents

5
Intent
(a simple message objects which is used to communicate from one activity to
another)
An intent is an abstract description of an operation to be performed

Intents define intention of an Application . They are also used to transfer


data between activities.
Intent is an important way to interact between components in an Android
application.
It can not only specify the actions that the current component wants to
perform, but also transfer data between different components.
An Intent can generally be used to launch activity, service and send
broadcast.

An Android Intent can be used to perform following 3 tasks :


 Open another Activity or Service from the current Activity
 Pass data between Activities and Services
 Delegate responsibility to another application. For example, you
can use Intents to open the browser application to display a URL.
6
• Intent can be broadly classified into 2 categories
 Explicit Intent
 Implicit Intent

• Intent Classification.
• Explicit intent : This kind of intent will define the
target component (Activity, Service etc) directly.
So only the specified target component will be
invoked.
• Implicit Intent : This kind of Intent will not specify
the component ( Activity, Service etc ) directly. It
only specify the action and category information.
Then android OS will filter out and start the
component which can response to the action and
category.
7
• Explicit Intent Example.
• There are two Activity in below example. When
you click the button in the first activity, it will use
an explicit Intent to tell android OS to start the
second Activity immediately. And transfer some
string data to the second activity also.
• When you click the bottom back menu while the
second activity is active, the second activity will
be closed and the first activity will be shown
again.
8
Explicit Intent
(the Intent in which you explicitly define the component that needs to be called by
Android System)
An explicit intent is one that you use to launch a specific app component, such as a particular
activity or service in your app.
Syntax :
Intent openNewActivity = new Intent (getApplicationContext(), SecondActivity.class);

9
Implicit Intent
(the intent where instead of defining the exact components, you define the action
you want to perform)
•An implicit intent specifies an action that can invoke any app on the device able to
perform the action.
•Using an implicit intent is useful when your app cannot perform the action, but
other apps probably can and you'd like the user to pick which app to use.

Syntax :

Intent sendIntent = new Intent();


sendIntent.setAction(Intent.ACTION_SEND);

10
Built-in Standard Intent Actions

List of standard actions that Intents can use for launching activities (usually
through startActivity(Intent).

ACTION_MAIN ACTION_SENDTO
ACTION_VIEW ACTION_ANSWER
ACTION_ATTACH_DATA ACTION_INSERT
ACTION_EDIT ACTION_DELETE
ACTION_PICK ACTION_RUN
ACTION_CHOOSER ACTION_SYNC
ACTION_GET_CONTENT ACTION_PICK_ACTIVITY
ACTION_DIAL ACTION_SEARCH
ACTION_CALL ACTION_WEB_SEARCH
ACTION_SEND ACTION_FACTORY_TEST

11
12
13
Data Passing

There are multiple ways to store data in an Intent.


 You can use an explicit bundle and set bundle using putExtra, or
 you can directly use putExtra

Use an explicit bundle

Bundle object is used to pass data between activities. You can still use
putExtra method to associate a bundle with an Intent.

Lets see the example code


Bundle dataBundle = new Bundle();
dataBundle.putString( "BundleUserName", "Pranay");
openNewActivity.putExtra("data",dataBundle);

14
Directly using putExtra

putExtra is the method which is used to store data in an Intent object.

This data can be of different types : string, char, Boolean, Bundle etc.
This data gets set as key value pair which you can retrieve in the called
activity.

Let's see this example:


openNewActivity.putExtra("UserName", "Pranay");
openNewActivity.putExtra("isRegistered", true);

The code below describes as to how we collect data into the Intent object
Intent intentObject = getIntent();
String userName = intentObject.getStringExtra ("UserName","");
boolean isRegistered = intentObject.getBooleanExtra ("isRegistered", true);

15

Das könnte Ihnen auch gefallen