Calling Built in
Calling Built in
Prerequisite
In this android intent example you need the Android Studio or Eclipse with ADT plugin and
Android SDK tool. It is also necessary that you must know about some fundamentals of Java and
XML as well as about the Android device. We also assume here that you know hello world
program for developing android apps. This is a simple beginners tutorial, which hardly take you
half an hour to understand, and doing yourself. I am doing this in Android Studio but you may
choose Eclipse (remember google officially going to end support for ADT at the end of 2015).
Again click next, select the Blank Activity, Click next. Android will ask to name the activity, keep
the default names, click on finish. Android Studio will create the the new Android Project with
all the necessary files.
Second one
CALLING BUILD IN APPLICATION USING INTENTS
Calling Build in Application using Intents :
"Until this point, you have seen how to call activities
within your own application. One of the keyaspects of Android programming is using the intent
to call activities from other applications. In particular,your application can call the many builtin applications that are included with an Androiddevice. For example, if your application needs
to enable a user to call a particular person saved inthe Contacts application, you can simply use
an Intent object to bring up the Contacts application,from which the user can select the person
to call. This enables your application to present a consistentuser experience, and enables you to
avoid building another application to retrieve all the contacts in the Contacts application."
The following demonstrates how to call some of the built-in applications commonly
found on an Android device.
STEPS:
Uri.parse(geo:37.827500,-122.481670));
startActivity(i);
}
});
//Choose Contact button
b4 = (Button) findViewById(R.id.btn_chooseContact);
b4.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0){
Intent i = new
Intent(android.content.Intent.ACTION_PICK);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(i,request_Code);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code)
{
if (resultCode == RESULT_OK)
{
Toast.makeText(this,data.getData().toString(),
Toast.LENGTH_SHORT).show();
Intent i = new Intent(
android.content.Intent.ACTION_VIEW,
Uri.parse(data.getData().toString()));
startActivity(i);
}
}
}
}
Subscribe to our newsletter and download the Android UI Design mini-book right
now!
With this book, you will delve into the fundamentals of Android UI design. You will
understand user input, views and layouts, as well as adapters and fragments.
Furthermore, you will learn how to add multimedia to an app and also leverage
themes and styles!
Email address:
Open Eclipse IDE and go to File New Project Android Application Project.
Specify the name of the application, the project and the package. Then, choose in the Minimum
Required Sdk the API 11 or larger, because Android introduced fragments in Android 3.0
Honeycomb (API level 11). Finally, click Next.
In the next window, check the Create Activity option. The new activity will be the
main activity of your project. Then press Next button.
In Configure Launcher Icon window you should choose the icon you want to have
in your app. In our occasion, we will use the default icon of android, so just press
Next.
In the next window, select the Blank Activity option and press Next.
Specify a name for the new Activity and a name for the layout description of your
app. The .xml file for the layout will automatically be created in
the res/layout folder. Then press Finish
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fragment No.1"
android:onClick="selectFrag" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.2" />
<fragment
android:name="com.javacodegeeks.android.fragmentstest.FragmentOne"
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Please, notice the highlighted lines. When we add a <fragment> element in a layout, we have to
contain the android:name and android:id attributes. The android:name defines an object of a
Fragment Class, that we want to be instantiated and the android:id specifies the unique id of
that fragment.
We will use the FragmentOne as default.
07
08
09
10
11
12
13
14
15
16
17
18
19
20
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.fragment_one, container, false);
}
}
As you can see in the code above, we use the onCreateView override method. It is
called by the Android system so that the fragment creates its user interface. Then,
it returns a View component which is placed in the <fragment> element of the layout.
Thats the reason we use the inflate method, in order to inflate a layout of an xml
file and to return its view.
4. Create the layouts of the Fragments
We are going to make a simple UI for each fragment layout, so we are going to create two
different xml files.
For FragmentOne we will create the fragment_one layout and for FragmentTwo the
fragment_two respectively.
Right click on res/layout folder New Android XML File and name the xml file. Choose
the LinearLayout as root element, as shown in the picture below, and then press Finish.
Email address:
1. Create a New Android Application Project
Open Eclipse IDE and go to File New Project Android Application Project.
Specify the name of the application, the project and the package. Then, choose in the Minimum
Required Sdk the API 11 or larger, because Android introduced fragments in Android 3.0
Honeycomb (API level 11). Finally, click Next.
In the next window, check the Create Activity option. The new activity will be the main
activity of your project. Then press Next button.
In Configure Launcher Icon window you should choose the icon you want to have in your app.
In our occasion, we will use the default icon of android, so just press Next.
In the next window, select the Blank Activity option and press Next.
Specify a name for the new Activity and a name for the layout description of your app. The .xml
file for the layout will automatically be created in the res/layout folder. Then press Finish.
In the image below you can see the final structure of the created project.
For this example, we need two different Buttons. When a button is pressed, a fragment appears
below them. So we will create two different buttons within the main LinearLayout and a
fragment by using the <fragment> tag.
Open res/layout/activity_main.xml and go to the activity_main.xml tab. Then paste the
following code.
activity_main.xml:
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03
android:layout_width="match_parent"
04
android:layout_height="match_parent"
05
06
android:orientation="vertical" >
<Button
07
android:id="@+id/button1"
08
android:layout_width="fill_parent"
09
android:layout_height="wrap_content"
10
android:text="Fragment No.1"
11
android:onClick="selectFrag" />
12
13
14
<Button
android:id="@+id/button2"
15
android:layout_width="fill_parent"
16
android:layout_height="wrap_content"
17
android:onClick="selectFrag"
18
19
20
<fragment
21
android:name="com.javacodegeeks.android.fragmentstest.FragmentOne"
22
android:id="@+id/fragment_place"
23
android:layout_width="match_parent"
24
android:layout_height="match_parent" />
25
26
</LinearLayout>
Please, notice the highlighted lines. When we add a <fragment> element in a layout, we have to
contain the android:name and android:id attributes. The android:name defines an object of a
Fragment Class, that we want to be instantiated and the android:id specifies the unique id of
that fragment.
We will use the FragmentOne as default.
3. Create the Fragments
We are going to make two different classes to define the fragments, that extends the Fragment
Class.
Right click on com.javacodegeeks.android.filtertstest package New Class.
To the opened window, specify the name for the new Class. We will name it FragmentOne and
we will put it in the same package as the MainActivity.java file.
package com.javacodegeeks.android.fragmentstest;
02
03
import android.app.Fragment;
04
import android.os.Build;
05
import android.os.Bundle;
06
import android.view.LayoutInflater;
07
import android.view.View;
08
import android.view.ViewGroup;
09
10
11
@Override
12
13
14
15
16
17
return inflater.inflate(
18
19
20
package com.javacodegeeks.android.fragmentstest;
02
03
import android.app.Fragment;
04
import android.os.Build;
05
import android.os.Bundle;
06
import android.view.LayoutInflater;
07
import android.view.View;
08
import android.view.ViewGroup;
09
10
11
12
13
@Override
public View onCreateView(LayoutInflater inflater,
14
15
16
17
18
return inflater.inflate(
19
20
21
As you can see in the code above, we use the onCreateView override method. It is called by the
Android system so that the fragment creates its user interface. Then, it returns a View component
which is placed in the <fragment> element of the layout. Thats the reason we use the inflate
method, in order to inflate a layout of an xml file and to return its view.
4. Create the layouts of the Fragments
We are going to make a simple UI for each fragment layout, so we are going to create two
different xml files.
For FragmentOne we will create the fragment_one layout and for FragmentTwo the
fragment_two respectively.
Right click on res/layout folder New Android XML File and name the xml file. Choose
the LinearLayout as root element, as shown in the picture below, and then press Finish.
Open the res/layout/frament_one.xml file on the fragment_one.xml tab and paste the
following code.
fragment_one.xml:
<?xml version="1.0" encoding="utf-8"?>
02
<LinearLayout
03
xmlns:android="http://schemas.android.com/apk/res/android"
04
android:layout_width="match_parent"
05
android:layout_height="match_parent"
06
android:orientation="vertical"
07
android:background="#00ffff">
08
09
<TextView
10
android:id="@+id/textView1"
11
12
13
14
15
16
17
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="This is fragment No.1"
android:textStyle="bold" />
</LinearLayout>
With the same way, we are going to make the layout for the second fragment.
Right click on res/layout folder New Android XML File and specify the name of the
xml file (fragment_two as we mentioned above). Choose the LinearLayout as root element and
then press Finish.
Open the res/layout/frament_two.xml file on the respective xml tab and paste the following
code.
fragment_two.xml:
<?xml version="1.0" encoding="utf-8"?>
02
<LinearLayout
03
xmlns:android="http://schemas.android.com/apk/res/android"
04
android:layout_width="match_parent"
05
android:layout_height="match_parent"
06
android:orientation="vertical"
07
android:background="#ffff00">
08
09
<TextView
10
android:id="@+id/textView2"
11
android:layout_width="match_parent"
12
android:layout_height="match_parent"
13
android:text="This is fragment No.2"
14
android:textStyle="bold" />
15
16
</LinearLayout>
Email address:
1. Create a New Android Application Project
Open Eclipse IDE and go to File New Project Android Application Project.
Specify the name of the application, the project and the package. Then, choose in the Minimum
Required Sdk the API 11 or larger, because Android introduced fragments in Android 3.0
Honeycomb (API level 11). Finally, click Next.
In the next window, check the Create Activity option. The new activity will be the main
activity of your project. Then press Next button.
In Configure Launcher Icon window you should choose the icon you want to have in your app.
In our occasion, we will use the default icon of android, so just press Next.
In the next window, select the Blank Activity option and press Next.
Specify a name for the new Activity and a name for the layout description of your app. The .xml
file for the layout will automatically be created in the res/layout folder. Then press Finish.
In the image below you can see the final structure of the created project.
For this example, we need two different Buttons. When a button is pressed, a fragment appears
below them. So we will create two different buttons within the main LinearLayout and a
fragment by using the <fragment> tag.
Open res/layout/activity_main.xml and go to the activity_main.xml tab. Then paste the
following code.
activity_main.xml:
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03
android:layout_width="match_parent"
04
android:layout_height="match_parent"
05
06
android:orientation="vertical" >
<Button
07
android:id="@+id/button1"
08
android:layout_width="fill_parent"
09
android:layout_height="wrap_content"
10
android:text="Fragment No.1"
11
android:onClick="selectFrag" />
12
13
14
<Button
android:id="@+id/button2"
15
android:layout_width="fill_parent"
16
android:layout_height="wrap_content"
17
android:onClick="selectFrag"
18
19
20
<fragment
21
android:name="com.javacodegeeks.android.fragmentstest.FragmentOne"
22
android:id="@+id/fragment_place"
23
android:layout_width="match_parent"
24
android:layout_height="match_parent" />
25
26
</LinearLayout>
Please, notice the highlighted lines. When we add a <fragment> element in a layout, we have to
contain the android:name and android:id attributes. The android:name defines an object of a
Fragment Class, that we want to be instantiated and the android:id specifies the unique id of
that fragment.
We will use the FragmentOne as default.
3. Create the Fragments
We are going to make two different classes to define the fragments, that extends the Fragment
Class.
Right click on com.javacodegeeks.android.filtertstest package New Class.
To the opened window, specify the name for the new Class. We will name it FragmentOne and
we will put it in the same package as the MainActivity.java file.
package com.javacodegeeks.android.fragmentstest;
02
03
import android.app.Fragment;
04
import android.os.Build;
05
import android.os.Bundle;
06
import android.view.LayoutInflater;
07
import android.view.View;
08
import android.view.ViewGroup;
09
10
11
@Override
12
13
14
15
16
17
return inflater.inflate(
18
19
20
package com.javacodegeeks.android.fragmentstest;
02
03
import android.app.Fragment;
04
import android.os.Build;
05
import android.os.Bundle;
06
import android.view.LayoutInflater;
07
import android.view.View;
08
import android.view.ViewGroup;
09
10
11
12
13
@Override
public View onCreateView(LayoutInflater inflater,
14
15
16
17
18
return inflater.inflate(
19
20
21
As you can see in the code above, we use the onCreateView override method. It is called by the
Android system so that the fragment creates its user interface. Then, it returns a View component
which is placed in the <fragment> element of the layout. Thats the reason we use the inflate
method, in order to inflate a layout of an xml file and to return its view.
4. Create the layouts of the Fragments
We are going to make a simple UI for each fragment layout, so we are going to create two
different xml files.
For FragmentOne we will create the fragment_one layout and for FragmentTwo the
fragment_two respectively.
Right click on res/layout folder New Android XML File and name the xml file. Choose
the LinearLayout as root element, as shown in the picture below, and then press Finish.
Open the res/layout/frament_one.xml file on the fragment_one.xml tab and paste the
following code.
fragment_one.xml:
01
02
<LinearLayout
03
xmlns:android="http://schemas.android.com/apk/res/android"
04
android:layout_width="match_parent"
05
android:layout_height="match_parent"
06
android:orientation="vertical"
07
android:background="#00ffff">
08
09
<TextView
10
android:id="@+id/textView1"
11
android:layout_width="match_parent"
12
android:layout_height="match_parent"
13
android:layout_weight="1"
14
15
android:textStyle="bold" />
16
17
</LinearLayout>
With the same way, we are going to make the layout for the second fragment.
Right click on res/layout folder New Android XML File and specify the name of the
xml file (fragment_two as we mentioned above). Choose the LinearLayout as root element and
then press Finish.
Open the res/layout/frament_two.xml file on the respective xml tab and paste the following
code.
fragment_two.xml:
01
02
<LinearLayout
03
xmlns:android="http://schemas.android.com/apk/res/android"
04
android:layout_width="match_parent"
05
android:layout_height="match_parent"
06
android:orientation="vertical"
07
android:background="#ffff00">
08
09
<TextView
10
android:id="@+id/textView2"
11
android:layout_width="match_parent"
12
android:layout_height="match_parent"
13
14
android:textStyle="bold" />
15
16
</LinearLayout>
In our example, we show the two buttons on the screen and when the appropriate button is
pressed, the respective fragment is displayed. By default, the layout of FragmentOne is shown, as
we declared it in the activity_main.xml file at the android:name attribute of the <fragment>
element.
Open src/com.javacodegeeks.android.filtertstest/MainActivity.java and paste the
following code.
MainActivity.java:
01
package com.javacodegeeks.android.fragmentstest;
import android.os.Bundle;
04
import android.view.View;
05
import android.app.Activity;
06
import android.app.Fragment;
07
import android.app.FragmentManager;
08
import android.app.FragmentTransaction;
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
if(view == findViewById(R.id.button2)) {
fr = new FragmentTwo();
}else {
fr = new FragmentOne();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}
Now we are ready to run our application. To do this, right click on our project Run as
Android Application. The AVD will appear with the app loaded.
As we can see from the image below, there are two Buttons and FragmentOne is displayed. As
we already mentioned, that is happening because we put the FragmentOne Class at the
android:name attribute in the activity_main.xml file.
If we press the button Fragment No.1, the FragmentOne will return a View which is
placed in the <fragment> element of the layout. So the appearing screen seems like
the first picture.
Display notifications
A notification is a message you can display to the user outside of your
application's normal UI. When you tell the system to issue a notification, it first
appears as an icon in the notification area. To see the details of the notification, the
user opens the notification drawer. Both the notification area and the notification
drawer are system-controlled areas that the user can view at any time.
Android Toast class provides a handy way to show users alerts but problem is that
these alerts are not persistent which means alert flashes on the screen for a few
seconds and then disappears
To see the details of the notification, you will have to select the icon which will
display notification drawer having detail about the notification. While working with
emulator with virtual device, you will have to click and drag down the status bar to
expand it which will give you detail as follows. This will be just 64 dp tall and called
normal view.
Above expanded form can have a Big View which will have additional detail about the
notification. You can add upto six additional lines in the notification. The following screen shot
shows such notification.
mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");
You have plenty of optional properties which you can set for your notification. To learn more
about them, see the reference documentation for NotificationCompat.Builder.
notification text in the notification drawer, you add the PendingIntent by calling
setContentIntent().
A PendingIntent object helps you to perform an action on your applications behalf, often at a
later time, without caring of whether or not your application is running.
We take help of stack builder object which will contain an artificial back stack for the started
Activity. This ensures that navigating backward from the Activity leads out of your application to
the Home screen.
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
1
2
Combine all of the options that have been set and return a new Notification object.
NotificationCompat.Builder setAutoCancel (boolean autoCancel)
Setting this flag will make it so the notification is automatically canceled when the user
Set the large icon that is shown in the ticker and notification.
NotificationCompat.Builder setNumber (int number)
10
11
12
13
Set the text that is displayed in the status bar when the notification first arrives.
NotificationCompat.Builder setVibrate (long[] pattern)
15
16
Set the time that the event occurred. Notifications in the panel are sorted by this time.
Example
Following example shows the functionality of a Android notification using a
NotificationCompat.Builder Class which has been introduced in Android 4.1.
Step
1
2
3
4
5
6
7
Description
You will use Android studio IDE to create an Android application and name it as
tutorialspoint under a package com.example.notificationdemo. While creating this project,
make sure you Target SDK and Compile With at the latest version of Android SDK to use
higher levels of APIs.
Modify src/MainActivity.java file and add the code to notify(""), if user click on the
button,it will call android notification service.
Create a new Java file src/NotificationView.java, which will be used to display new layout
as a part of new activity which will be started when user will click any of the notifications
Modify layout XML file res/layout/activity_main.xml to add Notification button in relative
layout.
Create a new layout XML file res/layout/notification.xml. This will be used as layout file
for new activity which will start when user will click any of the notifications.
No need to change default string constants. Android studio takes care of default string
constants
Run the application to launch Android emulator and verify the result of the changes done in
the application.
import
import
import
import
import
import
import
import
import
import
android.app.Notification;
android.app.NotificationManager;
android.app.PendingIntent;
android.content.Intent;
android.support.v7.app.ActionBarActivity;
android.os.Bundle;
android.view.Menu;
android.view.MenuItem;
android.view.View;
android.widget.Button;
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
android:layout_height="wrap_content"
android:text="Notification Example"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point "
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification"
android:id="@+id/button"
android:layout_marginTop="62dp"
android:layout_below="@+id/imageButton"
android:layout_centerHorizontal="true" />
</RelativeLayout>
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.notificationdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NotificationView"
android:label="Details of notification"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
</application>
</manifest>
Let's try to run your tutorialspoint application. I assume you had created your AVD while doing
environment set-up. To run the APP from Android Studio, open one of your project's activity
files and click Run
icon from the toolbar. Android Studio installs the app on your AVD
and starts it and if everything is fine with your setup and application, it will display following
Emulator window