Android Interview Questions and Answers

Today we are going to dive a bit deep into Android. I know you guys have already gone through the intermediate level of Android Interview Questions. Now it’s time for us to take a deep dive and understand more conceptual and more indirect questions that may require a little extra effort to get through.

We can’t expect what will happen in an interview and how your interviewer may twist the most straightforward question to the toughest. So, to handle such scenarios, you shall be well prepared, and to assist you, we have brought you a set of advanced-level Android Interview questions and Answers.

Android Interview Questions and Answers

Q1. What is the way to make an activity a launcher activity in Android?

Ans. We know by default the MainActivity acts as a launcher activity for any application. We can change this and keep any other activity as a launcher activity. To do so, you need to locate your Android Manifest XML file and then first remove the intent filter from your Main Activity and place it in your desired activity.

To make it a launcher activity, you must paste the below code inside your desired activity’s <activity> tags.

Code:

 <intent-filter>

            <action android:name="android.intent.action.MAIN" />




            <category android:name="android.intent.category.LAUNCHER" />

 </intent-filter>

Q2. Why is it important for Android to destroy an activity after the user has closed the application?

Ans. We very well know that whenever the user closes the application, all the activities he was using before are destroyed, and an onDestroy() callback method is triggered. Whenever we run an application, we use some resources like phone RAM, memory, processors, etc. Now, by destroying, we free up these resources which were being used by the application.

Android needs to free up these resources to allow other applications to run without any hustle. Every application that runs on your device requires these resources, and Android should make sure that the resources are made accessible for the next application after using that application. 

Q3. Can a service run in the foreground of the application? If yes, please explain how. 

Ans. Yes, a service can run in the foreground. We can bind the service to the application itself to make it run in the foreground. The foreground service is usually visible to the user, and users can interact with this kind of service. 

For example, consider your music application where the music playing service is bound to the application. We can turn on/off the music player from the application. Whenever we keep music in the application, we can go out of the application and operate other applications. Whereas if we close the application, then the service of playing music also closes. 

So, in this way, if we bind the service to the application itself, then we can run a service in the foreground. 

Q4. How do we identify the various types of data while dealing with content providers?

Ans. We use the content URI(Uniform Resource Identifier) to locate data in the content provider. The content provider takes URI as the parameter to find the specific data. Usually, the content URI is structured in the following manner:

  • Prefix –  Usually, the prefix is set to content://
  • Authority – It contains the name of the Provider.
  • Path – It includes the location of the data.
  • ID – This is an optional parameter that is used to access specific records.

Q5. How can we create multiple sub-activities in a given activity?

Ans. Fragments are popularly known as sub-activity of an activity. Using the ideology of fragments, we can create multiple sub-activities(or fragments) in a given activity.  Fragments are very much similar to activity and provide a user interface for users to interact. The only disadvantage with this kind of sub-activities( or fragments) is that they solely depend on their parent activities for their existence.

To create multiple sub-activities in a given activity, you can use the below code.

Code:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

  <FrameLayout

       android:id="@+id/our_frag_holder"

       android:layout_width="match_parent"

       android:layout_height="match_parent">

  </FrameLayout>

</LinearLayout>

Now to create the fragment transaction, you can use the below code. 

Code:

 val transaction = fragmentSupportManager.beginTransaction()

 transaction.replace(R.id.our_frag_holder,new OurFragment1())

 transaction.commit()

So using the above, we can place any of our fragments with the created FrameLayout. 

Q6. Suppose you are asked to build an application where if a user presses on a URL, you need to open the browser application with the clicked URL. How will you explain this? 

Ans. In such scenarios, we will use an implicit intent that helps us to open other applications. We actually use implicit intents when we know that our application can’t solely satisfy the user’s needs. We simply need to specify the type of action that we wish to perform, and the intent does that for us when triggered. 

In the case of the above scenario, we can write the following code to open the URL in the browser. 

Code:

my_intent = Intent(Intent.ACTION_VIEW)  

my_intent.setData(Uri.parse("https://techvidvan.com/tutorials/"))  

startActivity(my_intent)

Q7. You are assigned to develop an app that can respond to long clicks. In such a scenario, how and what will you use to accomplish the task?

Ans. We very well know that event handling is the concept we can use to handle long clicks or any other events in android. To be specific, we will use the onLongClick() method to respond to long clicks. 

So, what we need to do is, firstly, we need to register the view with the onLongClick() callback method. Inside the callback method, we will specify the task we intend to perform. 

For example, we can use the below to register for long clicks. 

Code:

 your_view.setOnLongClickListener {

            Toast.makeText(this@MainActivity, "Your View was pressed long", Toast.LENGTH_LONG).show()

            true

     }

Q 8. Is it possible to create fragments within a fragment? Please explain. 

Ans.  Yes, it is possible to create fragments within a fragment in Android. It is also known as the nesting of fragments in Android.  You can achieve this by simply placing another FrameLayout inside the existing layout of the fragment.

For example, see below, where a fragment with root layout as LinearLayout carries a FrameLayout inside it.

Code:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent" 

    android:layout_height="match_parent"

    android:orientation="vertical" >

<FrameLayout

        android:id="@+id/inside_fragment"

        android:layout_width=”300dp"

        android:layout_height="300dp" />

</LinearLayout>

Q9. Explain the process to change the theme of your application in Android.

Ans. Android gives us a feature to edit the style and theme of our application. We can even add or remove the app bar or even style it according to our needs. To do so, we need to locate the res directory and then open the values folder. In the values folder, we have the themes.xml file that we need to edit to style our application’s theme. 

Q10. Explain the concept of drag shadow in Android with an example.

Ans.  Drag Shadow is the path of the element through which it was dragged from one view to another. We use the DragShadowBuilder class to describe the path of the dragging. For example, suppose you drag a box from one room to another, then the trace of that box that is left behind after moving the box is known as the drag shadow. Drag shadow helps us to identify the path of the elements when dragged. 

Q11. Is there any feature in Android using which you can generate custom notifications on the user device? 

Ans. Android comes up with a Notification.Builder class that helps us to create our own custom notifications. We can provide text, images, and even data along with a notification. Android also allows us to decide the priorities of our built notification, and based on the priority it triggers the same to the user.

Suppose, if the priority of the notification is high, then it may occupy the first position in your status bar. Your device may also give a notification tone(if notifications are not disabled) to the user. 

You can use the below code to design your custom notifications. 

Code: 

builder = Notification.Builder(this, CHANNEL_ID)

                    .setContent(contentView)

                    .setSmallIcon(R.mipmap.YOUR_APP_ICON)

                    .setLargeIcon(BitmapFactory.decodeResource(this.resources,R.mipmap.YOUR_APP_ICON))

                    .setContentIntent(pendingIntent)

Q12. Is there any way to track your current location using Android? If so, please explain briefly how we can implement it. 

Ans. In Android, we have a feature known as location-based services, which enables us to track current locations, find nearby places, geofencing, etc. It has the capability to fetch your location based on GPS, WiFi, or even cellular network. We need to first enable the google play service for our application, and then we can use the FusedLocationProviderClient class to fetch our current location. 

Using the below code, we can access the current location, also known as the last known location in Android.

Below is an example of the same.

Code: 

private lateinit var fusedLocationClient: FusedLocationProviderClient




override fun onCreate(savedInstanceState: Bundle?) {

   

    LocationClientObject = LocationServices.getFusedLocationProviderClient(this)

}

Now just call the last location method with a listener. 

Code:

LocationClientObject.lastLocation

        .addOnSuccessListener { location : Location? ->

              //on getting the current location what you wish to do

  //specify here

        }

Q13. “Our company wants to build an application where we can update users about their credit score through SMS,” can you explain to us if it is possible to do so in Android?

Ans. Android gives us features to send SMS directly from our application. There are two ways using which we can send SMS directly from our application.

Firstly, we can use the SMS Manager API. Secondly, we can use the implicit intent way to send SMS. Below is an example of both of the methods. 

Using SMS Manager API

Code:

val smsManager: manager = SmsManager.getDefault()

manager.sendTextMessage(user_phone_number, null, credit_score, null, null)

Using Implicit Intent Method

Code:

val msg_uri = Uri.parse("smsto:user_phone_number")

val msgIntent = Intent(Intent.ACTION_SENDTO, msg_uri)

msgIntent.putExtra("message", "credit score of user")

startActivity(msgIntent)

Q14. Briefly explain the process of generating APK that can be deployed into the Google Play Store.

Ans. Firstly we need to code our application either in Kotlin or Java. Secondly, we will build our project, and the Gradle build tool helps us in doing that. After that, we shall try and test the debug version of our APK. After the testing process is accomplished, we’ll generate a signed version of APK. 

A signed version means adding details like manufacturer name, location, alias password, certificate validity, etc. After generating the signed version of APK, we can directly upload it on the google play store. 

Q15. Explain the task of an interpolator in Android.

Ans. An interpolator is a tool that helps in adding features and transitions between the start and end of an activity or task. We can use an interpolator to alter or insert between the initial and final state. 

There are nine types of interpolators in Android.

1. Linear Interpolator – 

It depicts that the rate of change of the animation is constant. 

2. Accelerate Interpolator – 

It helps to accelerate the animation. Starting with slow, then gradually speeds up the animation. 

3. Decelerate Interpolator – 

It helps to decrease the motion of an animation gradually. 

4. Anticipate Interpolator – 

It helps to make the animation work in reverse. 

5. Overshoot Interpolator – 

It helps to make the animation move in a forward direction. 

6. Bounce Interpolator – 

It helps to make the animation show bouncing movements. 

7. Cycle Interpolator – 

It helps to repeat the animation for a certain number or an infinite number of times. 

8. Anticipate Overshoot Interpolator – 

It drives the animation to first go in reverse then go in a forward direction. 

9. Accelerate Decelerate Interpolator – 

It helps to make the animation movement in the following order, first slow, then fast in the middle, then again slow. 

Q16. In Android, if we want to copy one text from one place and then paste it to another place, then how can we do it?

Ans. In Android, we have clipboards using which we can copy textual data and keep it in the clipboard. Later, when we need it, then we can get it back from the clipboard. Usually, the clipboard is located in the RAM of your device. In order to access the data stored in a clipboard, we need to use the Clipboard Manager. 

In order to copy data and store it in the clipboard, we can use the newPlainText() function as shown below. 

Code:

val yourClipData = ClipData.newPlainText("copiedText", enteredText)

yourClipboardManager.setPrimaryClip(yourClipData)

To paste the data from the clipboard, we need to use the getPrimaryClip() function as shown below. 

Code:

val clipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager

pastedTextView.text = clipboardManager.primaryClip?.getItemAt(0)?.text

Q17. What will you do if you are given a task to get data from a provided API and place it in the form of a list?

Ans. We will use the concept of Android Adapter and ListView to implement the given task. Firstly, we will create a ListView in the layout XML file. Using the ListView id, we will connect to our created ArrayAdapter. The ArrayAdapter acts here as a medium to bring the API’s data and then put it in the ListView. Further, if we wish to style each item of the list, we can create a design for each item and then place the same while creating the ArrayAdapter instance. 

Q18. Is there any possible way to get the details of the subscribe and the network modules present in the user’s device?

Ans. Yes, it is possible with the help of the Android Telephony package, but it requires the user’s permission before getting any sort of details. Android Telephony is a package provided by Android to operate and get information about the network interfaces and device details.  Android Telephony has several interfaces and classes that are used to provide various functionalities. 

To get the details, we need to use the Android Telephony manager, which is present in the package called android.telephony. 

You can use the below code to create an instance of the Android Telephony Manager class, and then by using the class, we can get all the desired results. 

Code:

val yourTelephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as

                TelephonyManager

Q19. We know that Android has an SQLite database where we can store data in a relational form. What if we want to keep them in the form of key-value? Please explain how you can do that.

Ans. In Android, we have a storage type known as Shared Preferences. Using Shared Preferences, we can store data in the form of key-value pairs. The only demerit of Shared Preferences is that it can’t work with a large amount of data. Usually, Shared Preferences only accept data of primitive types, as shown below.

  • Boolean Types
  • Integer Types
  • Float Types
  • String Types
  • Long Types

To create our own Shared Preferences Object, we can use the following code:

Code:

var yourSharedPrefObj: SharedPreferences? = null

yourSharedPrefObj = getSharedPreferences("File_Name", MODE_PRIVATE)

Q20. What is the tool that helps us in building our application? Briefly explain the build process. 

Ans. In Android, we have a build tool known as Gradle that helps us in building our application. The Gradle consists of build scripts that automate the build process by converting source code into an executable application. To understand it more clearly, read the following:

  • First and foremost, the Kotlin Source code, resources, and AIDL(Android Interface Definition Language) files are bound together and compiled.
  • Simultaneously the Gradle file binds the several dependencies (Library Modules, AAR Libraries, JAR libraries) and makes it ready for compilation.
  • After the files are successfully compiled, a .dex file is generated, sent to the APK Packager.
  • The APK Packager finally combines everything and generates an executable APK file.

The above whole process is guided and assisted by the Gradle build tool and finally, we have our APK using which we can install our application. 

Q21. Company ‘X’ wants to build a music application with features of pause/play and also to change the volume. Can you briefly explain how you can do it on Android?

Ans. In Android, we have a class known as MediaPlayer, using which we can implement the music player application. Using the functions like pause() and stop() that are present in the MediaPlayer class. These functions assist us in playing and pausing the music. So, starting with, we need first to create the MediaPlayer class object and take the permissions to access the storage of the user’s device. After that, we can proceed with adding the desired functionalities. 

For creating the object of the MediaPlayer class, we can use the following code:

lateinit var yourPlayerObj: MediaPlayer

yourPlayerObj = MediaPlayer.create(this, R.raw.your_music_file)

yourPlayerObj.isLooping = true

Q22. Why can’t we use a CheckBox instead of a RadioButton to select the gender option on some application ‘Y’?

Ans. We know that gender can’t be more than one for any user. Gender options are usually:

Male, Female, and Prefer Not to Say.

So in such a scenario, the user can select either of the above options but not multiple ones. As we know, RadioButton only allows you to choose one of the three given options, whereas, in a checkbox, you can select one, two, or even all options presented to you. So, in such cases, we prefer RadioButton instead of CheckBox. However, programmatically we can check the selections made by CheckBox and alert the user that he is not allowed to select all options. 

Q23. Describe the various components present in an Android Widget and also state the use of each of them.

Ans. Android Widget is made of three components that are classified as follows:

1. AppWidgetProviderInfo: AppWidgetProviderInfo is an instance of the widget containing the widget details like its layout, updates, etc. 

2. AppWidgetProvider: AppWidgetProvider provides the facility of paginating over a collection of an undefined number of objects. 

3. ViewLayout: ViewLayout is used to define the layout of your widgets. 

Q24. Which virtual machine does Android use to execute android applications? Is there any alternative to this virtual machine?

Ans. Android uses the Dalvik Virtual Machine to execute the android applications. Android Dalvik Virtual Machine provides a platform where the android applications can execute and perform the desired task. Dalvik Virtual Machine also provides better memory management and performance. It allows faster execution of the applications without much affecting the battery life. 

An alternative to Dalvik Virtual Machine is Android Runtime, known as an upgraded version of the Dalvik Virtual Machine. 

Q25. Does android allow your application to do anything on the user’s device? 

Ans. No, our application can’t do anything on the user’s device. Even things like accessing contact or opening a gallery require permission from the user. Without the consent of the user, you can’t do most things on the user’s device. However, some of the permissions, like the internet, are by default enabled for all applications. Your application can use the internet as and when required.

There are certain things for which even if the user gives permission still the app can’t make any changes. For example, your application can’t change the IMEI of your device. However, some unethical users try to change their phone IMEIs by rooting their device, which is illegal, and discard your device’s warranty. 

Q26. Explain the type of view which is used in building gallery-like applications.

Ans. For gallery-like applications, we usually use GridView. GridView gives us a layout where you have rows and columns containing elements in it. You can decide the number of columns or the number of rows you wish to have in your view. You can scroll through a GridView whenever required. If you want to load GridView with some data items, then you can use some Android Adapter. To create a GridView, you can use the following code:

<GridView

        android:background="@color/black"

        android:id="@+id/your_grid_view"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:numColumns="3"

        android:horizontalSpacing="12dp"

        android:verticalSpacing="15dp"

        android:layout_margin="10dp"/>

Q27. Can we use Android Studio to develop games in Android? Is there any alternative tool that we can use for developing games for the Android platform?

Ans. Yes, we can use Android Studio to develop games in Android. Android Studio allows you to develop extreme quality 2D games restricted to Android. With the Android Game Development Kit(AGDK), the task has become more accessible and straightforward. The AGDK allows text input, frame pacing, high-performance audio, and game controllers. The AGDK even provides GPU Profiling and debugging. 

Unity and Unreal Engine are some of the widely used alternatives to develop games for Android platforms. 

Q28. What all operations does the SQLite database support in Android?

Ans. SQLite supports CRUD operations and allows users to execute multiple simple and complex queries. Using the SQLite database, you can create a database and insert data into it. You can read the stored data and even alter it if needed. Suppose you feel some data is absurd or not required, then you can even delete it from the SQLite database. Moreover, you can use the SQLite database to store user’s data of your application locally. 

Q29. For using Push Notifications, is it necessary to use Firebase only? Are there any alternatives that we can use for sending Push Notifications?

Ans. No, you don’t need to use Firebase only. You can even use other tools to send push notifications to your user. There are several tools in the market that any developer can use to enable push notifications in their applications.

Some of the most popular tools are as follows:

  1. AWS Simple Notification Service (SNS) 
  2. Pushwoosh
  3. One Signal
  4. Airship
  5. Kumulos
  6. Leanplum

So, above are some of the tools we can use to send push notifications on Android devices. Hence, firebase is not a compulsory thing for push notifications. 

Q30. How can you manage form-data or Multipart in Android?

Ans. To handle form-data or Multipart in Android, we can use OkHttp, Volley, or Retrofit libraries. Multipart or form-data is a type of media type that helps us send large files over a network. Suppose you are given an API where you need to submit an image file. If the image is small, we could convert it to base64 and send it, but we need to use form data for large images. 

We need to use external libraries like Volley, OkHttp, or Retrofit to work with these media types. After adding the dependency of one of these libraries, we then use the functions present in these libraries to make the appropriate request. 

Summary

You came across several android advanced-level interview questions and answers through this article that indeed would have made you think a bit before answering. As we know, it’s not always the case that the interviewer would ask a direct question to you. Sometimes questions may contain a twist, as discussed above, and you need to tackle them smartly. I wish you a stroke of good luck and hope you enjoyed going through the above questions.