We create New Activity in Android Studio to create XML files for designing UI and java file coding. Below are the steps to create new Activity in Android Studio:
Step 1: Firstly, click on app > res > layout > Right Click on layout. After that Select New > Activity and choose your Activity as per requirement. Here we choose Blank Activity as shown in figure below.

Step 2: After that, customize the Activity in Android Studio. Enter the “Activity Name” and “Package name” in the Text box and Click on Finish button.

Step 3: After that your new Activity in Layout will be created. Your XML Code is in Text and your Design Output is in Design.

Intent
An Android Intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
For example, let’s assume that you have an Activity that needs to launch an email client and send an email using your Android device. For this purpose, your Activity would send an ACTION_SEND along with appropriate chooser, to the Android Intent Resolver. The specified chooser gives the proper interface for the user to pick how to send your email data.
Intent email = new Intent(Intent.ACTION_SEND, Uri.parse(“mailto:”));
email.putExtra(Intent.EXTRA_EMAIL, recipients);
email.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
email.putExtra(Intent.EXTRA_TEXT, body.getText().toString());
startActivity(Intent.createChooser(email, “Choose an email client from…”));
Above syntax is calling startActivity method to start an email activity and result should be as shown below −
There are separate mechanisms for delivering intents to each type of component − activities, services, and broadcast receivers.
Sr.No | Method & Description |
1 | Context.startActivity()The Intent object is passed to this method to launch a new activity or get an existing activity to do something new. |
2 | Context.startService()The Intent object is passed to this method to initiate a service or deliver new instructions to an ongoing service. |
3 | Context.sendBroadcast()The Intent object is passed to this method to deliver the message to all interested broadcast receivers. |
Intent Objects
An Intent object is a bundle of information which is used by the component that receives the intent as well as information used by the Android system.
An Intent object can contain the following components based on what it is communicating or going to perform −
Action
This is a mandatory part of the Intent object and is a string naming the action to be performed — or, in the case of broadcast intents, the action that took place and is being reported. The action largely determines how the rest of the intent object is structured . The Intent class defines a number of action constants corresponding to different intents.
The action in an Intent object can be set by the setAction() method and read by getAction().
Data
Adds a data specification to an intent filter. The specification can be just a data type (the mimeType attribute), just a URI, or both a data type and a URI. A URI is specified by separate attributes for each of its parts −
These attributes that specify the URL format are optional, but also mutually dependent −
- If a scheme is not specified for the intent filter, all the other URI attributes are ignored.
- If a host is not specified for the filter, the port attribute and all the path attributes are ignored.
The setData() method specifies data only as a URI, setType() specifies it only as a MIME type, and setDataAndType() specifies it as both a URI and a MIME type. The URI is read by getData() and the type by getType().
Category
The category is an optional part of Intent object and it’s a string containing additional information about the kind of component that should handle the intent.
The addCategory() method places a category in an Intent object, removeCategory() deletes a category previously added, and getCategories() gets the set of all categories currently in the object.
Extras
This will be in key-value pairs for additional information that should be delivered to the component handling the intent. The extras can be set and read using the putExtras() and getExtras() methods respectively.
Flags
These flags are optional part of Intent object and instruct the Android system how to launch an activity, and how to treat it after it’s launched etc.
Component Name
This optional field is an android ComponentName object representing either Activity, Service or BroadcastReceiver class. If it is set, the Intent object is delivered to an instance of the designated class otherwise Android uses other information in the Intent object to locate a suitable target.
The component name is set by setComponent(), setClass(), or setClassName() and read by getComponent().
Types of Intents
There are following two types of intents supported by Android

Explicit Intents
Explicit intent going to be connected internal world of application, suppose if you wants to connect one activity to another activity, we can do this quote by explicit intent, below image is connecting first activity to second activity by clicking button.

These intents designate the target component by its name and they are typically used for application-internal messages – such as an activity starting a subordinate service or launching a sister activity. For example −
// Explicit Intent by specifying its class name
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
// Starts TargetActivity
startActivity(i);
Implicit Intents
These intents do not name a target and the field for the component name is left blank. Implicit intents are often used to activate components in other applications. For example −
Intent read1=new Intent();
read1.setAction(android.content.Intent.ACTION_VIEW);
read1.setData(ContactsContract.Contacts.CONTENT_URI);
startActivity(read1);
Above code will give result as shown below

The target component which receives the intent can use the getExtras() method to get the extra data sent by the source component. For example −
// Get bundle object at appropriate place in your code
Bundle extras = getIntent().getExtras();
// Extract data using passed keys
String value1 = extras.getString(“Key1”);
String value2 = extras.getString(“Key2”);
Intent Filters
Android OS uses filters to pinpoint the set of Activities, Services, and Broadcast receivers that can handle the Intent with help of specified set of action, categories, data scheme associated with an Intent.
You will use the <intent-filter> element in the manifest file to list down actions, categories and data types associated with any activity, service, or broadcast receiver.
Following is an example of a part of AndroidManifest.xml file to specify an activity com.example.My Application.CustomActivity which can be invoked by either of the two mentioned actions, one category, and one data −
<activity android:name=”.CustomActivity”
android:label=”@string/app_name”>
<intent-filter>
<action android:name=”android.intent.action.VIEW” />
<action android:name=”com.example.My Application.LAUNCH” />
<category android:name=”android.intent.category.DEFAULT” />
<data android:scheme=”http” />
</intent-filter>
</activity>
Once this activity is defined along with above mentioned filters, other activities will be able to invoke this activity using either the android.intent.action.VIEW, or using the com.example.My Application.LAUNCH action provided their category is android.intent.category.DEFAULT.
The <data> element specifies the data type expected by the activity to be called and for above example our custom activity expects the data to start with the “http://”
There may be a situation that an intent can pass through the filters of more than one activity or service, the user may be asked which component to activate. An exception is raised if no target can be found.
There are following test Android checks before invoking an activity −
- A filter <intent-filter> may list more than one action as shown above but this list cannot be empty; a filter must contain at least one <action> element, otherwise it will block all intents. If more than one actions are mentioned then Android tries to match one of the mentioned actions before invoking the activity.
- A filter <intent-filter> may list zero, one or more than one categories. If there is no category mentioned then Android always passes this test but if more than one category is mentioned then for an intent to pass the category test, every category in the Intent object must match a category in the filter.
- Each <data> element can specify a URI and a data type (MIME media type). There are separate attributes like scheme, host, port, and path for each part of the URI. An Intent object that contains both a URI and a data type passes the data type part of the test only if its type matches a type listed in the filter.
Resources
Sr.No. | Directory & Resource Type |
1 | anim/XML files that define property animations. They are saved in res/anim/ folder and accessed from the R.anim class. |
2 | color/XML files that define a state list of colors. They are saved in res/color/ and accessed from the R.color class. |
3 | drawable/Image files like .png, .jpg, .gif or XML files that are compiled into bitmaps, state lists, shapes, animation drawables. They are saved in res/drawable/ and accessed from the R.drawable class. |
4 | layout/XML files that define a user interface layout. They are saved in res/layout/ and accessed from the R.layout class. |
5 | menu/XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. They are saved in res/menu/ and accessed from the R.menu class. |
6 | raw/Arbitrary files to save in their raw form. You need to call Resources.openRawResource() with the resource ID, which is R.raw.filename to open such raw files. |
7 | values/XML files that contain simple values, such as strings, integers, and colors. For example, here are some filename conventions for resources you can create in this directory −arrays.xml for resource arrays, and accessed from the R.array class.integers.xml for resource integers, and accessed from the R.integer class.bools.xml for resource boolean, and accessed from the R.bool class.colors.xml for color values, and accessed from the R.color class.dimens.xml for dimension values, and accessed from the R.dimen class.strings.xml for string values, and accessed from the R.string class.styles.xml for styles, and accessed from the R.style class. |
8 | xml/Arbitrary XML files that can be read at runtime by calling Resources.getXML(). You can save various configuration files here which will be used at run time. |
Alternative Resources
Your application should provide alternative resources to support specific device configurations. For example, you should include alternative drawable resources ( i.e.images ) for different screen resolution and alternative string resources for different languages.
At runtime, Android detects the current device configuration and loads the appropriate resources for your application.
To specify configuration-specific alternatives for a set of resources, follow the following steps −
- Create a new directory in res/ named in the form <resources_name>-<config_qualifier>. Here resources_name will be any of the resources mentioned in the above table, like layout, drawable etc. The qualifier will specify an individual configuration for which these resources are to be used. You can check official documentation for a complete list of qualifiers for different types of resources.
- Save the respective alternative resources in this new directory. The resource files must be named exactly the same as the default resource files as shown in the below example, but these files will have content specific to the alternative. For example, the image file name will be the same but for a high resolution screen, its resolution will be high.
Accessing Resources
During your application development you will need to access defined resources either in your code, or in your layout XML files. Following section explains how to access your resources in both the scenarios −
Accessing Resources in Code
When your Android application is compiled, a R class gets generated, which contains resource IDs for all the resources available in your res/ directory. You can use R class to access that resource using sub-directory and resource name or directly resource ID.
Example
To access res/drawable/myimage.png and set an ImageView you will use following code −
ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);
Accessing Resources in XML
Consider the following resource XML res/values/strings.xml file that includes a color resource and a string resource −
<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
<color name=”opaque_red”>#f00</color>
<string name=”hello”>Hello!</string>
</resources>
Now you can use these resources in the following layout file to set the text color and text string as follows −
<?xml version=”1.0″ encoding=”utf-8″?>
<EditText xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:textColor=”@color/opaque_red”
android:text=”@string/hello” />
Adding activity