Android Language Project Application Forms

Android Language Editor powered by Bing Translate If you use this features, you can tranlate all android app on your native language Of course, you can translate the phone's application. The 'proper' name for this is the Android Open Source Project, and this is what people mean when they say things like Android is open and free. Android, in this iteration, is free for anyone to use as they like.

Active28 days ago

Is it possible to change the language of an app programmatically while still using Android resources?

If not, is it possible to request a resource in an specific language?

I would like to let the user change the language of the app from the app.

hpiquehpique
60.9k123 gold badges312 silver badges455 bronze badges

30 Answers

It's possible. You can set the locale. However, I would not recommend that. We've tried it at early stages, it's basically fighting the system.

We have the same requirement for changing the language but decided to settle to the fact that UI should be same as phone UI. It was working via setting locale but was too buggy. And you have to set it every time you enter activity (each activity) from my experience. here is a code if you still need this (again, I don't recommend that)

If you have language specific content - you can change that base on the setting.

Petter Hesselberg
3,4721 gold badge14 silver badges32 bronze badges
Alex VolovoyAlex Volovoy
59.5k13 gold badges68 silver badges52 bronze badges

It's really work:

fa = Persian, en = English

Enter your language code in languageToLoad variable:

AliShAliSh
6,3675 gold badges28 silver badges57 bronze badges

I was looking for a way to change the system language programmatically.While I fully understand that a normal application should never do that and instead either:

  • the user should be pointed(through an intent) to the system settings to change it manually
  • the application should handle its localization on its own just like described in the answer of Alex

there was a need to really change the language of the system programmtically.

This is undocumented API and thus should not be used for market/end-user applications!

Anyway heres the solution i found:

icyerasoricyerasor

If you want to mantain the language changed over all your app you have to do two things.

First, create a base Activity and make all your activities extend from this:

Note that I save the new language in a sharedPreference.

Second, create an extension of Application like this:

Note that getLocale() it's the same as above.

That's all!I hope this can help somebody.

Daniel S.Daniel S.

Just adding an extra piece that tripped me up.

While the other answers work fine with 'de' for example

The above wont work with for example 'fr_BE' locale so it would use the values-fr-rBE folder or similar.

Needs the following slight change to work with 'fr_BE'

triggstriggs
5,2103 gold badges26 silver badges31 bronze badges

According tothis article. You will need to download LocaleHelper.java referenced in that article.

  1. Create MyApplication class that will extends Application
  2. Override attachBaseContext() to update language.
  3. Register this class in manifest.

  4. Create BaseActivity and override onAttach() to update language. Needed for Android 6+

  5. Make all activities on your app extends from BaseActivity.

Flimm
60.6k24 gold badges150 silver badges168 bronze badges
Khaled LelaKhaled LelaForms
3,0993 gold badges24 silver badges52 bronze badges

I am changed for German language for my app start itself.

Here is my correct code. Anyone want use this same for me..(How to change language in android programmatically)

my code:

Kuitsi
1,4401 gold badge25 silver badges44 bronze badges
harikrishnanharikrishnan
1,1273 gold badges21 silver badges52 bronze badges

I know it's late to answer but i found this article here. Which explains the whole process very well and provides you a well structured code.

Locale Helper class:

You need to override attachBaseContext and call LocaleHelper.onAttach() to initialize the locale settings in your application.

All you have to do is to add

wherever you want to change the locale.

Anirudh SharmaAnirudh Sharma
7,36712 gold badges34 silver badges39 bronze badges

Create a class Extends Application and create a static method.Then you can call this method in all activities before setContentView().

Usage in activities:

V.Y.
2,5733 gold badges16 silver badges36 bronze badges
Behzad TaghipourBehzad Taghipour

For Android 7.0 Nougat (and lower) follow this article:

Old answer
This include RTL/LTR support:

DudaDuda

If you write

In every activity (in the manifest file) then no need to set it every time you enter Activity.

Ricardo A.
2202 gold badges3 silver badges27 bronze badges
Brijesh MasraniBrijesh Masrani
8392 gold badges12 silver badges27 bronze badges

The only solution that fully works for me is a combination of Alex Volovoy's code with application restart mechanism:

Matias Elorriaga
5,0302 gold badges24 silver badges51 bronze badges
MishaMisha
3,3354 gold badges30 silver badges58 bronze badges

I was facing the same issue. On GitHub I found the Android-LocalizationActivity library.

This library makes it very simple to change the language of your app at runtime, as you can see in the code sample below. A sample project including the sample code below and more information can be found at the github page.

The LocalizationActivity extends AppCompatActivity, so you can also use it when you are using Fragments.

RockneyRockney
6,4302 gold badges14 silver badges22 bronze badges

Important update:

Note, that on SDK >= 21, you need to call 'Resources.updateConfiguration()', otherwise resources will not be updated.

Максим ПетлюкМаксим Петлюк

Time for a due update.

First off, the deprecated list with the API in which it was deprecated:

  • configuration.locale (API 17)
  • updateConfiguration(configuration, displaymetrics) (API 17)

The thing no question answered recently has gotten right is the usage of the new method.

createConfigurationContext is the new method for updateConfiguration.

Some have used it standalone like this:

... but that doesn't work. Why? The method returns a context, which then is used to handle Strings.xml translations and other localized resources (images, layouts, whatever).

The proper usage is like this:

If you just copy-pasted that into your IDE, you may see a warning that the API requires you targeting API 17 or above. This can be worked around by putting it in a method and adding the annotation @TargetApi(17)

But wait. What about the older API's?

You need to create another method using updateConfiguration without the TargetApi annotation.

You don't need to return a context here.

Now, managing these can be difficult. In API 17+ you need the context created (or the resources from the context created) to get the appropriate resources based on localization. How do you handle this?

Well, this is the way I do it:

This code works by having one method that makes calls to the appropriate method based on what API. This is something I have done with a lot of different deprecated calls (including Html.fromHtml). You have one method that takes in the arguments needed, which then splits it into one of two (or three or more) methods and returns the appropriate result based on API level. It is flexible as you do't have to check multiple times, the 'entry' method does it for you. The entry-method here is setLanguage

You need to use the Context returned when you get resources. Why? I have seen other answers here who use createConfigurationContext and doesn't use the context it returns. To get it to work like that, updateConfiguration has to be called. Which is deprecated. Use the context returned by the method to get resources.

Example usage:

Constructor or somewhere similar:

And then, whereever you want to get resources you do:

Using any other context will (in theory) break this.

AFAIK you still have to use an activity context to show dialogs or Toasts. for that you can use an instance of an activity (if you are outside)

And finally, use recreate() on the activity to refresh the content. Shortcut to not have to create an intent to refresh.

ZoeZoe
15.7k12 gold badges66 silver badges96 bronze badges
altan yukselaltan yuksel

Localeconfiguration should be set in each activity before setting the content - this.setContentView(R.layout.main);

cheskapaccheskapac

At first create multi string.xml for different languages; then use this block of code in onCreate() method:

Tonechas
8,0359 gold badges26 silver badges54 bronze badges
Mohsen mokhtariMohsen mokhtari
Paul Chu
1,0673 gold badges11 silver badges20 bronze badges
Til SchweigerTil Schweiger

Alex Volovoy answer only works for me if it's in onCreate method of the activity.

The answer that works in all the methods is in another thread

Here is the adaptation of the code

Hope that it helps.

Community
gmauri21gmauri21
Application

Take note that this solution using updateConfigurationwill not be working anymore with the Android M release coming in a few weeks. The new way to do this is now using the applyOverrideConfigurationmethod from ContextThemeWrappersee API doc

You can find my full solution here since I faced the problem myself:https://stackoverflow.com/a/31787201/2776572

Community
XexizXexiz

This is working when i press button to change text language of my TextView.(strings.xml in values-de folder)

ashishdhiman2007ashishdhiman2007
3881 gold badge7 silver badges19 bronze badges
Adeeb karimAdeeb karim

There are some steps that you should implement

First, you need to change the locale of your configuration

Second, if you want your changes to apply directly to the layout that is visible, you either can update the views directly or you can just call activity.recreate() to restart the current activity.

And also you have to persist your changes because after user closes your application then you would lose the language change.

I explained more detailed solution on my blog post Change Language Programmatically in Android

Basically, you just call LocaleHelper.onCreate() on your application class and if you want to change locale on the fly you can call LocaleHelper.setLocale()

GunhanGunhan
3,3021 gold badge32 silver badges31 bronze badges

similar to the accepted answered but 2017 version and added restart (without restarting, sometimes the next Activity still renders English):

ericnericn
5,63910 gold badges47 silver badges83 bronze badges

First you create directory name values-'Language name' like hindi than write 'hi' and same string file name copy in this directory and change value do not change parameter after set below code in your action like button etc....

Dhaval ShingalaDhaval Shingala

In example we set English language:

Please, remember this works only if language is found in Device system also, not only in application

Pavel PekkiPavel Pekki

None of the solutions listed here helped me.

The language did not switch on android >= 7.0 if AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

This LocaleUtils works just fine:https://gist.github.com/GigigoGreenLabs/7d555c762ba2d3a810fe

LocaleUtils

Added this code to Application

Code in Activity

Pavel ShirokovPavel Shirokov

Just handle in method

Follow the Link

I think it is useful

kleopatra
45.8k16 gold badges76 silver badges168 bronze badges
An NguyenAn Nguyen

I encountered the same problem: I needed to set my language to a language chosen in my app.

My fix was this:

  1. Keep your strings in your XML file, don't extract it to resources
  2. Make an exact copy of your XML and rename it to _languagecode, like_fr (use lowercase!)
  3. Fix your translations in your XML copy
  4. In code you check your app-level language and inflate the relevantXML

Example:

From these XML's, you can still extract the needed strings to resources.

TomCBTomCB
1,8456 gold badges27 silver badges56 bronze badges

Not the answer you're looking for? Browse other questions tagged androidlocalizationresources or ask your own question.

Tutorial

Get started developing Android mobile apps using Android Studio

This tutorial introduces Android application development with the Android Studio integrated development environment (IDE), including the construction of an example application. The application is a basic starter experience, complete with all phases of building, running and monitoring. While the application has the “basics,” it also demonstrates a key feature of the Android platform–launching another application as it takes user input and passes that information to the mapping application. To get the most from this tutorial, mobile-development experience is helpful, but not required. Java programming skills are required for Android applications, but are not an explicit requirement for this tutorial.

Why do we care about Android? We care about the Android platform because it is the most widely distributed mobile platform on the planet and is accessible to consumers around the globe. In economies where the ownership of a personal computer is a luxury, mobile device usage is the norm–and Android is playing a major role in connecting people to one another. As you will learn in this tutorial, Android takes a distinct approach to applications. The architecture of Android permits a highly customizable software environment thanks to its runtime binding of requested actions and the code to satisfy those requests. Whether it’s market-driven considerations or the technical aspects of Android, it is a platform worth examination. Further, if you are giving serious consideration to having a mobile experience for your platform, you cannot ignore Android.

Prerequisites

This tutorial requires several technologies that work together. You need all of them for this tutorial.

  • Android Studio – Android Studio is the primary starting point for constructing Android Applications
  • Android device or Emulator – You will want either a physical Android device, or the Android Emulator. Either way, reading through this tutorial will aid you in understanding the basic connection points of an Android application.
  • Source code – Source code snippets in this tutorial include:

    • AndroidManifest.xml snippet — This file is the application deployment descriptor for Android applications.
    • MainActivity.java — This implements an Android activity, the primary entry point to the sample application of this tutorial.
    • Activity_main.xml — This contains definitions for the visual elements, or resources, for use by Android activities.
    • AndroidManifest.xml complete — This lists a full AndroidManfest.xml file, along with a description of each of the important elements.

Introducing the Android architecture

Before diving right into the ins and outs of the Android Studio and developing Android applications, let’s have a look at the architecture of Android and some of the key terms that will be helpful in the tutorial and beyond, as you begin to build Android applications for yourself.

Language

Android terminology

An understanding of the terms below is helpful in Android application development with Android Studio.

  • Android is an open source operating environment targeted for mobile devices. Increasingly, Android is found in applications beyond “smartphones”. These include wearable technology, appliances such as projectors, speakers and televisions, and even automobiles.
  • Emulator is a software tool representative of another system. Often, an emulator is an environment that runs on a personal computer (PC, Mac, Linux machine) that emulates another environment, such as a mobile-computing device.
  • Linux is an open source operating system kernel at the heart of many computing platforms, including servers, desktop computers, networking appliances, and mobile-computing devices. Android runs on top of a Linux kernel.
  • Android Run Time (ART) is an operating environment found in the Android stack, which executes application code at runtime. The Android Run Time replaces the “legacy” Dalvik Virtual Machine (VM) from earlier versions of Android which operated in a manner similar to a compliant Java VM.

The intent

Since its inception, Android has become a dominant force in the mobile phone space. Its application model is unique in that applications are not monolithic, menu-laden applications that require a great deal of clicking and tapping to operate. Sure, there are menus and buttons to be tapped, but Android has an innovative design element to its architecture known as an intent.

An intent is a construct that permits an application to issue a request, which is somewhat like a help-wanted sign. It might look like these:

  • “Wanted: An application to help me look up a contact”
  • “Wanted: An application to help me display this image”
  • “Wanted: An application to perform this geographic-based search.”

In a similar and complementary fashion, applications can register themselves as capable and interested in satisfying various requests or intents. To follow the classified advertising paradigm, these might look like this:

  • “Available: Application ready and willing to present contact records in clear, concise manner”
  • “Available: Application ready and willing to perform a geographic search.”

These are examples of IntentFilters, which are discussed next.

The IntentFilter

Applications announce their availability to perform these types of operations via a construct known as an IntentFilter. The IntentFilter is either registered at runtime or is enumerated in the AndroidManifest.xml file. The following snippet comes from an Android application that responds to incoming SMS (text) messages:

After this brief introduction to the intent and IntentFilter, the next section introduces the four main types of Android applications.

Types of Android applications

Let’s take a moment to examine the four main types of Android applications:

  • Activity
  • Service
  • Receiver
  • ContentProvider

We will also take a look at the views to display the user-interface (UI) elements.

Activity apps

The activity is the most visible and prominent form of an Android application. An activity presents the UI to an application, along with the assistance of a class known as a view. The view class is implemented as various UI elements, such as text boxes, labels, buttons, and other UIs typical in computing platforms, mobile or otherwise.An application may contain one or more activities. An activity is typically defined on a one-to-one relationship with the screens found in an application.

An application moves from one activity to another by calling a method known as startActivity() or startSubActivity(). The former method is used when the application desires to simply “switch” to the new activity. The latter is used when a synchronous call/response paradigm is desired. In both cases, an intent is passed as an argument to the called method.

It is the operating system’s responsibility to determine the best-qualified activity to satisfy the specified intent.

Service and receiver apps

Like other multi-tasked computing environments, there are applications running “in the background” that perform various duties. Android calls these types of applications services. The service is an Android application that has no UI.

The receiver is an application component that receives requests to process intents. Like the service, a receiver does not, in normal practice, have a UI element. Receivers are typically registered in the AndroidManifest.xml file.

The following snippet (which is the same as the one shown for IntentFilter) is an example of a receiver application being defined. Note that the name attribute of the receiver is the Java class responsible for implementing the receiver.

The following snippet is an example of receiver code:

ContentProvider apps

The ContentProvider is the Android mechanism for>Note that each element has one or more attributes in the android name space.

The next section walks through obtaining the Android SDK and configuring it for use with Android Studio.

Getting started with Android Studio

Now that we have a feel for the components of the Android platform, let’s get started with Android Studio. This section walks through obtaining Android Studio and a discussion of the numerous versions of the Android SDK.

Obtaining and installing Android Studio

This first step is very straight-forward. Open your web browser to https://developer.android.com/, and scroll down to download Android Studio. Android Studio is available for Windows, Mac, and Linux.

Android Studio is based on the IntelliJ IDE platform. If you’re the kind of developer who is interesting in leveraging keyboard shortcuts, you might consider learning more about IntelliJ from the jetbrains website.

Getting started with the Android SDK

Now that we have Android Studio installed, it is time to get the Software Developer Kit (SDK) for Android.

But, before we go any further, we need to have a discussion around Android versions.

Android versions

Since its inception, Android has been released under a somewhat parallel path of numeric release revisions, API levels, and also “sweet” names including Jelly Bean, KitKat, Oreo, and other confectionary favorites.

It is easy to be overwhelmed by the choices available. For now you need just a little bit of understanding of this “version landscape” to get started as an Android developer.

There are many versions, or revisions, of Android in the wild. If you are going to write an application for commercial release, you will need to know which devices you are targeting. Wikipedia has a nice summary of the Android versions, the names, the release dates as well as a graphic depicting the approximate number of each device in the marketplace.

Newer devices run the latest code, and older devices can be somewhat constrained in their ability to upgrade to a newer version of Android. Sometimes the newer versions support capabilities that the older handsets are not capable of supporting (think second camera, Near Field Communications, and so on). Another non-trivial reason that older handsets may not be able to run the latest Android version is that both the manufacturer and the telecom carrier (Verizon, AT&T, and so on) need to support the software running within their ecosystem. Supporting a new operating system requires resources on the part of the device manufacturer and the carrier. Market forces suggest that these market participants are more concerned with future sales than supporting prior sales and invest their human capital accordingly.

As a software developer, what does it matter that there are new versions of the operating system? This breaks down into two broad categories when it comes to programming applications:

  • A new version of the operating system often introduces new features which we can take advantage of and/or our user base expects our applications to support.
  • Older (or legacy) features of the environment may be actually hindering newer features and/or represent an inferior approach to providing a specific set of functionality. Sometimes code needs to actually be removed from the API definitions. There is a special term for this “removal” of formerly working code – we call it “code deprecation”. When an API has been marked for removal, we say that this is a deprecated API. This is essentially a message to the developer that goes something like this: “Warning” this code will be removed in the future, please plan accordingly.”

The Android platform has been fairly liberal (that is, favorable to the developer) with its approach to deprecated code by permitting applications to continue using older APIs for quite a while after being marked as deprecated. (This approach contrasts with Apple’s approach with deprecated code in iOS where older code can simply just stop working on the next OS release. With iOS, even the programming language seems to change while we are not looking!)

While Android may be a bit more forgiving with its long-suffering of deprecated code, we do need to keep an eye on the documentation tag which says that a particular API is deprecated. If some code that we’re using is marked as deprecated, we will need to eventually change our approach in a future version of our application because any API which is marked as deprecated can be removed in a future release. Deprecation markers in the SDK are our roadmap to future opportunities for improvement and for potential headaches. Sometimes the “new” way of doing things is simple and straight-forward, but not in every case!

Being a successful mobile application developer requires an ongoing investment over the lifetime of an application where time tends to pass like dog years…very quickly!

With this basic intro to the versions of Android, it’s time to obtain the Android SDK.

Obtaining and installing the Android SDK

Start Android Studio. Under the Tools menu, select SDK Manager. This menu option loads the Android Studio Preferences dialog and pre-selects the Android SDK sub menu. This menu is our primary means of interacting with the various SDKs available for the many versions of Android.

With each release of the operating system, the API level increments. Notice that the revision increases slower than the API level. You can think of the revision as a “generation” of the platform, or as a “Major” version number. When you browse the Android documentation, be sure that you note which API level that a particular capability was introduced. Each API is available at the API Level indicated and greater, unless the API has been deprecated and subsequently removed. For example, the ContactsContract API was introduced at level 5, which is quite “old” at this point.

Select one or two SDK levels to work with for now. The SDK Manager will subsequently download the related files and make them available for use within Android Studio.

Also under the Tools menu is an option for the AVD Manager. AVD stands for Android Virtual Device. This is the emulator. The emulator is helpful because it permits us to test applications on various device types, sizes, and versions. The screen shots in this tutorial are taken from the emulator. More on this later.

Building your first Android application

This tutorial steps you through creating a basic Android application, called SaySomething, using Android Studio and demonstrating it on the Android emulator.

Step 1: Create a new project

  1. Select File >New Project.
  2. Fill out the Create Android Project dialog, and click Next.

    The requirements for the new project include:

    • Application name
    • Company domain (this gets reversed into com.domain) to keep applications partitioned even if they have the same Application name.
    • Project Location – where to store the files
    • The Package name is suggested from the Application Name and Company domain
    • By default, Android applications are written in Java. If you want to support alternate languages, such as C++ (for native code) or Kotlin, select the appropriate check boxes.
  3. On the Target Android Devices dialog, specify the API level and target platforms. As mentioned earlier, Android is for more than just phones, though for our purposes we will simply select the Phone and Tablet form factor for this tutorial. Then, click Next.

  4. On the Add an Activity to Mobile dialog, select the Empty Activity type. Then, click Next.

    This will create a default application ready to be built and run directly from Android Studio.

  5. On the Configure Activity dialog, name the files for the application.

Step 2: Review the code

The following figure shows the components of our new project:

There are two folders: app and Gradle Scripts.

  • The app folder contains all of our application’s code.
  • The manifests folder in the app folder contains AndroidManifest.xml which is the deployment descriptor for this application. This file tells the Android device about how to interact with the application, which Activity to show when the application is started, which Intents the application services, which icons to display and much more.
  • The java folder contains the source code for the application. In this case, the file which implements our activity plus a couple of class files for performing automated tests (which we will ignore for this tutorial).
  • The res folder contains the resources for the application, including icons, layout files, strings, menus, and so on.
  • The Gradle Scripts folder contains all of the scriptable elements for building the application. The Gradle build system is a topic unto itself. For now, understand that these scripts govern the process of building the application – and importantly for more mature projects and teams, each of these steps is able to be run from the command line. This is important because this approach supports automation and fall into the category of Continuous Integration.

Now, let’s examine the source code in further detail.

Primary activity for the application

Our sample application consists of a single activity, named MainActivity.

Things to note about this source snippet:

  • MainActivity is a normal Java class, with a package and imports, as expected.
  • MainActivity extends a base Android class named AppCompatActivity, which is located in the package named android.support.v7.app.
  • The onCreate() method is the entry point to this activity, receiving an argument of type Bundle. The Bundle is a class which is essentially a wrapper around a map or hashmap. Elements required for construction are passed in this parameter.
  • The setContentView(..) is responsible for creating the primary UI using the R.layout.main argument. This is an identifier representing the main layout found in the resources of the application. Note that any identifiers in the “R” class are automatically generated by the build process. Essentially all of the xml resource files under the res folder described earlier are accessed with the R class.

Resources for the application

Resources in Android are organized into a subdirectory of the project named res, as described previously. Resources fall into a number of categories. Three of these categories are:

  • Drawables – This folder contains graphics files, such as icons and bitmaps
  • Layouts – This folder contains XML files that represent the layouts and views of the application. These will be examined in detail below.
  • Values – This folder contains a file named strings.xml. This is the primary means for string localization for the application. The file colors.xml is useful for defining color codes used by the application. This file is analogous to a css style sheet for those familiar with web programming techniques.

Primary UI resources in the main_activity.xml file

The sample application contains a single activity and a single view. The application contains a file named main_activity.xml that represents the visual aspects of the primary UI of the activity. Note that there is no reference in the main_activity.xml where the layout is used. This means it may be used in more than one activity, if desired. See the following code listing

This is an example of a fairly straight-forward layout. There is a single linear layout, which is oriented as a vertical layout, meaning all contained elements are in a single column. Within this encapsulating LinearLayout, there is a second LinearLayout organized as a horizontal layout. This inner layout contains a pair of button widgets. Below the horizontal LinearLayout there is a single TextView element, which can be likened to a label in other development environments. A TextView represents static text that is not editable. Below the TextView, there are two EditText widgets, configured to take numeric inputs. These input elements are used to capture a Latitude and Longitude value which will subsequently be passed along to a mapping application, as demonstrated below.

Note that each view element has attributes in the android name space. Some attributes are common to all views — the android:layout_width and android:layout_height attributes, for example. The values available for these attributes are:

  • Match Parent – This extends the view element to take the maximum space available. This can also be thought of as meaning “stretch.”
  • Wrap Content – This option results in the element expanding just enough to contain its child views or attributes such as text or image.

Deployment descriptor for an Android application (AndroidManifest.xml)

The AndroidManifest.xml file represents the deployment descriptor for an Android application. The file lists any activity, service, content provider, or receiver contained in the application, along with the appropriate IntentFilters supported by the application. Here is the complete AndroidManifest.xml file for the sample application:

Things to note about the AndroidManifest.xml file:

  • The package name from the source file is represented here. This follows a similar pattern to a Java source file and imports. The <manifest> tag is in essence importing classes from this package. All non-fully qualified classes in this file are found in the package identified in the package attribute.
  • The <application> tag has an attribute that references a resource from the application’s resources. Note the @ symbol preceding the various identifiers. This is a hint for the toolchain to look in the respective folder of the application’s resources for each resource.
  • The <activity> tag contains the following attributes and values of note:

    • android:name represents the Java class implementing this activity
    • android:label contains the name of the application. Note that it is referencing one of the string resources. The string.xml file contains localized strings for the application.
    • <intent-filter> represents the IntentFilter available in the sample application. This specific set of action and category attributes are the most common IntentFilter seen in Android applications. This filter essentially says that it implements the main action (or entry point) and is available in the currently operating launch screen for the OS. In simple terms, this means it can be started as an application from the primary list of applications on an Android device.

Step 3: Build the application

To build the application, select the wrench icon (looks more like a hammer to me) in the menu of Android Studio.

The build process, as introduced earlier, is a multi-step process that may be customized for any particular application. For this tutorial, we do not modify the build process. The output of the build process should look something like this:

To demonstrate the build process, we have introduced an error into the source code; we added an extra space in the name of the method findViewById, shown in the following figure:

This creates a syntax or parsing error which is picked up by the build process and displayed in the details of the build results window in Android Studio. Upon fixing the error in the source code, the application builds properly, and the errors are removed from the problems list.

Step 4: Run the application

Now that the application has compiled successfully, it’s time to run the sample application.

Earlier in this tutorial the AVD Manager was introduced as the means for configuring the emulator for testing applications without the need for a real device. Running an application by using the emulator is particularly useful because it permits testing without the need to purchase numerous costly devices.

To setup an emulator, or more properly, an Android Virtual Device (AVD), select Tools >AVD Manager. Or, simply click on the Play (Run) icon.

This launches a window that lets you select either a real device that is connected by a USB cable or an available AVD instance, as shown in the following figure:

If there are connected devices, each shows up in the drop-down below Connected Devices. Alternatively, select from one of the available Virtual Devices. In the previous figure, there is a single AVD defined entitled Nexus One API P. To create a new AVD, select the Create New Virtual Device button.

Finally, click OK to launch the application on the selected deployment target. The AVD or emulator launches with our sample application running on it.

Step 5: Test the application in the emulator

Now that the application is running on the emulator, let’s have a look at what is happening behind the scenes. Within Android Studio, select View >Tool Windows. We will highlight just a couple of these tool options, though each of them is a topic unto itself.

Android Studio Tools

First, look at the Android Profiler which depicts a running chart of CPU, Memory, and Network traffic. The Android Profiler permits us to toggle between different devices/emulator instances and respective running processes. The following figure displays the metrics for our sample application running on an emulator instance.

Logging is a useful tool for any developer on any platform. For Android developers, the name of the tool is LogCat.

The LogCat is a running log file of activity taking place in the VM. Applications can make their own entries to this list with a simple line of code as follows: Log.i(tag,message);1, where tag and message are both Java strings. The Log class is part of the android.util.Log package.

Another useful tool is the file explorer, which permits file system access of the Emulator. The following figure shows where the tutorial’s sample application is deployed on the Emulator.

User applications are deployed in /data/app while Android built-in applications are found in the /system/app directory.

Full-scale debugging of an Android application is beyond the scope of this tutorial, though rest assured that Android Studio supports all of the modern application debugging tools we can hope for with things like Stepping In and Stepping Over function calls.

Android Language Project Application Forms Pdf

Testing the sample application

Tap on the Hit Me! button, and the contents of the label changes to “Ouch!”

Android Application Project

Tap on the Map Me! button, and a mapping application is launched with the specified latitude and longitude that is contained in the EditText widgets.

Android Language Project Application Forms

The wiring of the buttons is in the MainActivity.xml code. In particular, note how the latitude and longitude values are extracted and packaged into an Intent, which is subsequently used to launch the mapping application.

Summary

Android Application Development

This tutorial introduced the Android platform, the Android Studio, and the key elements of Android development. You should now be ready to create your own Android applications.