[Android] Beginner Tutorials – Lesson 26. Intent Filter – practice

Translated by Taras Leskiv (http://android-by-example.blogspot.com/)

Last lessons were overloaded with theory. This theory must be understood well for you not to have problems in practice. These topics are the basis – Task, Lifecycle, Intent. If you don’t understand something completely, you can always open the materials and read it again. Further we will implement examples that will confirm this theory and everything will become more clear.

In previous lessons we have learnt to invoke an Activity using Intent and explicitly defining the class. We are also aware that there is another way of Activity invocation – implicit. It is based on the point that Activity is invoked not by its name but by its functionality. That is, when we want to complete specific actions, we create and configure the corresponding Intent and sent it to look for those Activities that can solve our problem.

Let’s have a look how is it implemented in practice. We will create the application that will display the current time and date. We will implement it using three Activities:
– the first one will contain two buttons: Show time and Show date
– the second will display the time
– the third will display the date

Clicking the Show time button will invoke the second Activity and pressing Show date – the third Activity. But we will implement this not by explicitly defining Activity classes in the Intent but using the Intent Filter.

Let’s create a project:

Project name: P0261_IntentFilter
Build Target: Android 2.3.3
Application name: IntentFilter
Package name: ru.startandroid.develop.p0261intentfilter
Create Activity: MainActivity

Open main.xml and draw two buttons:

MainActivity.java implementation:


In the code we have defined the buttons and set them Activity as a click listener. Inside onClick method we define which button has been clicked and create an Intent.

For creating an intent we use a constructor: Intent(String action). That is when creating an Intent we specify its attribute which is called action. It is a usual String constant. Action usually specifies an action that we want to perform. For example, there such system action-constants: ACTION_VIEW – view, ACTION_EDIT – edit, ACTION_PICK – selection from the list, ACTION_DIAL – make a call.

If the action is performed on some data, then one more Intent-attribute is specified – data. Inside it we can specify any object we need: user in the address book, map coordinates, phone number etc. That is action specifies what to do and datawith what to do it.

We will talk about data in the next lessons, we will use only action for now. I’ve already enumerated some system action-constants, but we can use our own action.

As you can see from the code, I came up with these actions:

The first one means that I want to invoke an Activity that will show me the current time. Second one – Activity with the current date.

You have to clearly understand the following thing: action is just a text. I could come up with action like abcdefg123456. But showtime text represents what I want to accomplish, it is more obvious and understandable. And I am using ru.startandroid.intent.action prefix to avoid collisions. There may be an application installed that already uses showtime action – I don’t want to intercept with it. That’s why my action is ru.startandroid.intent.action.showtime.

So we have created an Intent with action and sent it to the system to look for Activities. For Activity to fit for our intent it must contain an action attribute with the same value that action in the Intent. It means we need to create two Activities, configure their Intent Filters and implement displaying date and time.

Activity is created as usual – create ActivityTime class which is the subclass of android.app.Activity and register it in manifest as Activity. After it is registered in the manifest, we have to create an Intent Filter in the same place. To do so, select ActivityTime, click Add, choose Intent Filter and click OK.


After this analogically create Action inside Intent Filter and write ru.startandroid.intent.action.showtime inside the Name field.


We also have to create Category inside the Intent Filter and in the name field choose android.intent.category.DEFAULT from the list. I will not explain now why is it needed. But without this startActivity(Intent) invocation will not find an Activity.


Create the layout for new Activity and name it time.xml:

Write code inside ActivityTime.java:

Everything is simple here – calculate the current time and display it in the TextView.

Save everything and run the application.


Press Show time button:


time has been displayed. That is Intent with action = ru.startandroid.intent.action.showtime found and displayed Activity, which has action which is equal to ru.startandroid.intent.action.showtime inside its Intent Filter.

Let’s get back (Back button) and now press Show date button. The application will generate an error as it couldn’t find an Activity which corresponds to Intent with action = ru.startandroid.intent.action.showdate (we have created only for showtime).

Let’s create such an Activity and name it ActivityDate. Actions are all the same as when creating ActivityTime:
– class creation
– creating Activity in the manifest and creating an Intent Filter for it (with action = ru.startandroid.intent.action.showdate and category = android.intent.category.DEFAULT)


Name layout-file date.xml:

ActivityDate.java code:


Save everything and run the application, press Show date and you will see date displayed. That means that Intent with action = ru.startandroid.intent.action.showdate found and displayed ActivityDate which fits it by Intent Filter.

To make knowledge solid, let’s do something else. If you remember, in the Lesson №22 about Intent Filter, I wrote that one Intent can find several appropriate Activities. In this case the user will be given a choice which Activity to use. Let’s implement this case. We will make one more Activity, which will react on Intent with action = ru.startandroid.intent.action.showdate. And it will display the current date analogically to ActivityDate. But the display format will be slightly different.

Let’s create such an Activity and name it ActivityDateEx. Actions are the same like when creating ActivityDate:
– class creation
– Activity creation in manifest and creating the Intent Filter for it (with action = ru.startandroid.intent.action.showdate and category = android.intent.category.DEFAULT)

We will not create a new layout-file and use already existing date.xml. Actually all three Activities could have used the same layout as they are absolutely identical – one TextView.

ActivityDateEx.java code:

As you can see, the difference from ActivityDate is only date format. Save everything and run the application. Press Show date and you will see the following choice:


Intent found two Activities, but showed the name of the parent application and package for each of them. In our case – both Activities are from our application, that’s why the text is the same and we can’t distinguish which is which. Let’s fix it by providing proper names.

Press Back to close the selection dialog. Go to manifest and change the label for Activity:


Date basic for ActivityDate
Date extended for ActivityDateEx

Save everything and run the application. Press Show date and you will see the following choice:


This is significantly better. Press Date extended and see the date in the extended format in ActivityDateEx.

So we sent Intent with action. This Intent found Activity with proper Intent Filter and displayed it. If it found several Activities – it gave a choice. These examples are a great explanation of the mechanism.

If you got lost what to create and where, here is the project structure and manifest contents.


manifest contents (AndroidManifest.xml tab):




Share this Post :

No comments yet.

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *