The Wayback Machine - https://web.archive.org/web/20240913041424/https://www.geeksforgeeks.org/processing-and-parsing-xml-in-android/
Open In App

Processing and Parsing XML in Android

Last Updated : 19 Sep, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this blog, we will learn about one of Android’s most fascinating topics. Even most of us are unaware of it, despite the fact that it is one of the most essential ideas in Android development. But, before we get into our topic, take out your phones and count the number of apps you have on your smartphone. On average there must be more than 30 applications. However, we only use 5–6 of these 30 apps on a daily basis. Other apps, albeit little utilized, are critical. So, on our home screen or the main screen of our phone, we make shortcuts to commonly used programs. These launchers are used to start the application’s MainActivity or Launcher Activity.

So, in this article, we’ll learn how to make application shortcuts on the home screen? No way, we’re all well aware of it. We are all Android Developers, and we must consider the technical aspects of the platform. As a result, anytime we open a mobile application via a shortcut, the MainActivity or Launcher Activity is invoked. The shortcut’s job is to maintain or save your launcher, and every time you open an app, it will launch the launcher or just MainActivity for you.

However, the problem becomes more complicated when you modify your application’s Launcher Activity. So, try changing your application’s Launcher Activity and running it on your device. Is the shortcut still visible on your home screen? What occurred just now? What happened to the shortcut? Don’t worry, you’ll find out the answers to all of these questions towards the end of this blog. So, in this article, we’ll go over Activity Aliases on Android.

Before Proceeding

We’ve noticed that if we alter the Launcher Activity of our application, the application’s shortcut on the home screen is gone. But why should the Launcher Activity be changed? The explanation is simple: every time you make a new version for your program, you may need to modify your Launcher Activity owing to new features, or you may have changed the package name and the associated Activity name. As a result, in this situation, the name of your Launcher Activity will be altered but the content stays the same.

Let’s create a project to better understand the situation

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
  
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Geeks for Geeks Old Activity"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
  
</androidx.constraintlayout.widget.ConstraintLayout>


Now, create a new Activity with the name NewActivity. Below is the code for the activity_new.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".NewActivity">
  
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Geeks for Geeks New Activity"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
</androidx.constraintlayout.widget.ConstraintLayout>


In the NewActivity.kt file, we haven’t written any code. Open the AndroidManifest.xml file now. The following code will be present:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/gfg_logo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    <activity android:name=".New">
    </activity>
    <activity android:name=".Main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

Because we used the intent-filter> in the OldActivity tag, our OldActivity is the Launcher activity. As a result, anytime we run the program, the OldActivity is also launched. Install the app on your smartphone and create a shortcut to it on your home screen. Change the Launcher activity to NewActivity once you’ve created a shortcut. As a result, the following code will be added to our AndroidManifest.xml file:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/gfg_logo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    <activity android:name=".Purani">
    </activity>
    <activity android:name=".Nayi">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

Now, launch the app and look for the shortcut you made on the home screen. There are no shortcuts on the home screen.

What is the cause behind this?

When we create a shortcut to a certain application on our home screen, that shortcut remembers the name of the Launcher Activity.

Activity-Alias

So, to retain the shortcut on the home screen even when the Launcher Activity name changes, we employ the notion of Activity-Alias. The activity-alias > tag is used to launch an Activity while keeping the launchers intact. So what’s stopping you from using the activity alias pal? But how should this Activity-Alias be used? Simply paste the following code into your AndroidManifest.xml file:

<activity android:name=".Purani"/>
<activity android:name=".Nayi"/>
<activity-alias
        android:name=".MainActivity"
        android:targetActivity=".Puranai">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity-alias>

To declare our Launcher Activity, we utilized the activity-alias> tag. See the tags listed below for a better understanding When the Launcher is used, the term “Purani” is used to specify the targeted activity. Create a shortcut to the app on your home screen, then change the Launcher Activity to NewActivity and start it. You can see that even after altering the Launcher Activity, our shortcut remains on the home screen. But what is the cause behind this?

Hence, the name would be remembered by the user. Change the name in the activity-alias> and run the program again. You will encounter an identical issue, in which the shortcut will be deleted from the screen due to a name change.

Geek Tip: To utilize activity-alias>, you must define all of your Activities (including the Launcher Activity) above the activity-alias> tag rather than below it.

Other functionalities made available by activity-alias>

Apart from maintaining the Launcher, the <activity-alias> provides the following features:

<activity-alias android:enabled=["true" | "false"]
                android:exported=["true" | "false"]
                android:icon="gfg icon"
                android:label="string resource"
                android:name="string"
                android:permission="string"
                android:targetActivity="string" >
</activity-alias>
  1. android:enabled: The android: enabled specifies whether or not the targeted activity may be created by the system. If not, the value will be false; otherwise, it will be true. It is true by default for Activity and alias, but in order to start an Activity, both of these values must be true at the same time.
  2. android:exported: This property indicates whether or not the targetedActivity may be started by components of another application. If it does not, the value is false; otherwise, it is true.
  3. android:icon: This property specifies the icon for the Targeted Activity that is displayed to the user through an alias. When the alias is displayed to the user.
  4. android:label: is used to provide a user-readable text for the alias. It is used to uniquely identify an alias by writing a fully classed class name.
  5. android:permission: The name of the permission required for the alias to launch a certain activity is present here.
  6. android:targetActivity: This property is used to indicate the name of the Activity that will be started using the alias.

Conclusion

We learned about the notion of activity-alias> in this blog. The <activity-alias> attribute is used to preserve launchers in Android applications. We may modify the Launcher Activity by using the activity-alias>, and our application shortcut will stay in the same location on the home screen.



Similar Reads

XML Parsing in Android using XmlPullParser
In android, the XMLPullParser interface provides the functionality to parse the XML files in android applications. The XMLPullParser is a simple and efficient parser method to parse the XML data when compared to other parser methods such as DOM Parser and SAX Parser. The XMLPullParser has a method next() that provides access to high-level parsing e
5 min read
XML Parsing in Android using DOM Parser
Android DOM(Document Object Model) parser is a program that parses an XML document and extracts the required information from it. This parser uses an object-based approach for creating and parsing the XML files. In General, a DOM parser loads the XML file into the Android memory to parse the XML document. This results in more consumption of memory.
6 min read
XML Parsing in Android using SAX Parser
Generally, XML (Extensible Mark-up Language) is a commonly used data exchange format to interchange servers' data. In Android, SAX stands for Simple API for XML and is a widely used API for XML parsing. Like the DOM parser, the SAX parser is also used to perform in-memory operations to parse the XML document, but it consumes less memory than the DO
6 min read
JSON Parsing in Android
JSON (JavaScript Object Notation) is a straightforward data exchange format to interchange the server's data, and it is a better alternative for XML. This is because JSON is a lightweight and structured language. Android supports all the JSON classes such as JSONStringer, JSONObject, JSONArray, and all other forms to parse the JSON data and fetch t
4 min read
JSON Parsing in Android using Volley Library
JSON is also known as (JavaScript Object Notation) is a format to exchange the data from the server. The data stored in JSON format is lightweight and easy to handle. With the help of JSON, we can access the data in the form of JsonArray, JsonObject, and JsonStringer. In this article, we will specifically take a look at the implementation of JsonOb
6 min read
JSON Parsing in Android using Retrofit Library
JSON is also known as (JavaScript Object Notation) is a format to exchange the data from the server. The data stored in JSON format is lightweight and easy to handle. With the help of JSON, we can access the data in the form of JsonArray, JsonObject, and JsonStringer. In this article, we will specifically take a look at the implementation of JsonOb
6 min read
Android - JSON Parsing using Retrofit Library with Kotlin
JSON is a format with the help of which we can exchange the data from the server within our application or a website. For accessing this data from the server within android applications. There are several libraries that are available such as Volley and Retrofit. In this article, we will take a look at JSON Parsing in Android applications using Kotl
6 min read
JSON Parsing in Android Using Volley Library with Kotlin
JSON is a JavaScript object notation which is a format to exchange the data from the server. JSON stores the data in a lightweight format. With the help of JSON, we can access the data in the form of JsonArray, JsonObject, and JsonStringer. In this article, we will specifically take a look at the implementation of JsonObject using Volley in Android
5 min read
Android - JSON Parsing using Volley Library with Jetpack Compose
JSON is a JavaScript object notation which is a format to exchange the data from the server. JSON stores the data in a lightweight format. With the help of JSON, we can access the data in the form of JsonArray, JsonObject, and JsonStringer. In this article, we will specifically take a look at the implementation of JsonObject using Volley in Android
8 min read
Android - JSON Parsing Using Retrofit Library with Jetpack Compose
JSON is a format with the help of which we can exchange the data from the server within our application or a website. For accessing this data from the server within android applications. There are several libraries that are available such as Volley and Retrofit. In this article, we will take a look at JSON Parsing in Android applications using Jetp
9 min read
How to draw Horizontal and Vertical lines in an Android App using XML
This article demonstrates how to draw a horizontal and a vertical line in an Android app. Although we have countless XML tags to meet almost all of our needs, unfortunately, we don't have any tag such as &lt;line&gt; tag to draw the line. However, &lt;view&gt; tag can be used in XML as a workaround. NOTE: The "view" tag is different than the View c
2 min read
How to Create Drawable Resource XML File in Android Studio?
Prerequisite: Android Project folder Structure A drawable resource is a common concept for a graphic that can be drawn to the screen and which one can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon. There are several different types of drawable resources files a
3 min read
A Complete Guide to Learn XML For Android App Development
XML stands for Extensible Markup Language. XML is a markup language much like HTML used to describe data. It is derived from Standard Generalized Markup Language(SGML). Basically, the XML tags are not predefined in XML. We need to implement and define the tags in XML. XML tags define the data and used to store and organize data. It's easily scalabl
7 min read
XML Namespaces in Android
In an XML file, namespaces are used to provide elements and attributes with distinctive names. Names for elements or attributes in an XML instance may come from different XML vocabularies. The ambiguity between similar elements or attributes can be eliminated if each vocabulary is given its own namespace. An identifier for a uniform resource is a n
2 min read
Android Jetpack Compose - Interoperability Using Compose in XML Layouts
Writing UI in jetpack compose can be fun, but can be difficult to migrate your whole project into Compose at one time. Fortunately, Jetpack Compose provides Interoperability api to use compose in existing XML views so that you can migrate to compose slowly. Prerequisites: Knowledge of Jetpack Compose.Knowledge of KotlinKnowledge of Android’s View.
3 min read
What to Use for Background Processing in Android?
By default, application code runs within the main thread. Every statement is therefore executed in sequence. If you perform an extended-lasting operation, the appliance blocks until the corresponding operation has finished. To provide an honest user experience all potentially slow running operations in an Android application should run asynchronous
5 min read
Android Annotation Processing
You've undoubtedly seen a lot of annotations as an Android developer: They're those strange code components that begin with @ and sometimes have parameters connected to them. Annotations link metadata to other code components, allowing you to include extra information in your code. Annotations may be used to produce new source files based on that i
4 min read
How to Set Up Processing for Android?
Introduction:Processing is a free graphical library and Integrated Development Environment (IDE) built for the electronic arts and visual design communities. It is one of the most powerful libraries available today for creating visual algorithmic artworks, both 2D and 3D. It is open-source, based on Java, and comes with a large variety of functions
3 min read
How to View AndroidManifest.xml From an APK File?
AndroidManifest.xml is one of the most important files in the entire project, It provides essential information to the Android build tools. It contains all application permissions that will be required to run the app as well as it contains all activity, service, and broadcast receiver's names. If the app AndroidManifest.xml isn’t set up correctly,
2 min read
How to Capture a Screenshot and Screen Recording of an Android Device Using Android Studio?
Android Studio is the official IDE (Integrated Development Environment) for Android app development and it is based on JetBrains’ IntelliJ IDEA software. Android Studio provides many excellent features that enhance productivity when building Android apps. Whenever we are developing an app and in that device, you don't know how to capture screenshot
2 min read
10 Android Studio Tips and Tricks For Android Developers
To become a good Android developer you should be familiar with Android Studio with some of its tips and tricks. These tips and tricks will help you to become a good Android developer and it will also help you to improve your efficiency in developing Android Apps. In this article, we will be discussing 10 Android Studio, Tips, Tricks, and Resources
6 min read
Fix "Error running android: Gradle project sync failed. Please fix your project and try again" in Android Studio
In this article, we will see how to fix the error: "Gradle project sync failed. Please fix your project and try again" in Android Studio". Before getting into the solution part, let's discuss Gradle. What is Gradle? Gradle is an open-source build automation tool that automates the creation of applications. The various steps involved in creating an
5 min read
Build an Android App with HTML, CSS and JavaScript in Android Studio
Android is a mobile operating system based on a modified version of the Linux kernel and other open-source software, designed primarily for touchscreen mobile devices such as smartphones and tablets. Yes, you read it right in the title of this article. In this article, we are going to build an Android App with HTML, CSS, and JavaScript in Android S
2 min read
Android Manifest File in Android
Every project in Android includes a Manifest XML file, which is AndroidManifest.xml, located in the root directory of its project hierarchy. The manifest file is an important part of our app because it defines the structure and metadata of our application, its components, and its requirements. This file includes nodes for each of the Activities, Se
5 min read
Android | Android Application File Structure
It is very important to know about the basics of Android Studio's file structure. In this article, some important files/folders, and their significance is explained for the easy understanding of the Android studio work environment. In the below image, several important files are marked: All of the files marked in the above image are explained below
3 min read
What's Android Interface Definition Language (AIDL) in Android?
The Android Interface Definition Language (AIDL) is analogous to other IDLs you would possibly have worked with. It allows you to define the programming interface that both the client and repair agree upon so as to speak with one another using interprocess communication (IPC). Traditionally the Android way a process cannot access the memory of some
7 min read
Android | Running your first Android app
After successfully Setting up an Android project, all of the default files are created with default code in them. Let us look at this default code and files and try to run the default app created. The panel on the left side of the android studio window has all the files that the app includes. Under the java folder, observe the first folder containi
3 min read
How to Clone Android Project from GitHub in Android Studio?
Android Studio provides a platform where one can build apps for Android phones, tablets, Android Wear, Android TV, and Android Auto. Android Studio is the official IDE for Android application development, based on IntelliJ IDEA. One can develop Android Applications using Kotlin or Java as the Backend Language, and it provides XML for developing Fro
3 min read
How to Add OpenCV library into Android Application using Android Studio?
OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. When it integrated with various libraries,
3 min read
8 Best Android Libraries That Every Android Developer Should Know
Android is an operating system which is built basically for Mobile phones. It is used for touchscreen mobile devices like smartphones and tablets. But nowadays these are utilized in Android Auto cars, TV, watches, camera, etc. Android has been one of the best-selling OS for smartphones. Android OS was developed by Android Inc. which Google bought i
5 min read
Article Tags :