Android Apps Localization

Hello everyone 🙂 As you might have noticed, I’ve recently decided to take a longer break in DSP competition. Few things to do, Easter in the meantime… but of course I didn’t give up and today I’m back 😉 In this post I’d like to show you how do we localize Android apps. Let’s see!

What is localization ?

Generally, localization is a process of adapting the product or application to specific markets by adjusting its language and cultural resources accordingly. People often think that localization is only a process of translating documentation and user interface into multiple languages, but in fact it may concern many other not less important topics, e.g. formats of numeric values or dates, currencies, numbers comparison/sorting methods, symbols, national flags or even country-specific law requirements. Android supports app’s resources localization in pretty easy way. We can quite conveniently provide different versions of strings, images and layouts used in our application for different languages/regions. Let’s take a look at the basics of Android app’s localization.

Android localization basics

In every Android application, localization is based on the language user has set on his/her device. The language can be changed in Settings -> Language & input. Changing the language in this place affects both user interface’s language and region-specific settings (e.g. dates formats). To retrieve the current locale (constant/object representing selected language) in code the following statement can be used:

var currentLang = Resources.Configuration.Locale;

Android Layout Types

If you’re just an Android user, you may not even know that each screen in your application has controls composed within different Layouts. Let’s see what are the main Layout Types in designing Android app.

What is a Layout ?

Layout defines a visual structure of an Activity (or app widget). It may be considered as a set of rules according to which controls (buttons, text fields, input fields etc.) are placed on the View.

Layouts structure

Basically, user interface in Android apps is built using Layouts. Each Layout is a subclass of ViewGroup class, which derives from View class, which is the basic UI building block. View is the base class for buttons, text fields etc., more precisely: for

widgets

Xamarin – Android Intents

We’ve already seen what are Activities, the most basic pieces of every Android app and today’s topic is associated with it. We’re going to see how to communicate between Activities (or Application Blocks) using Intents.

Android Application Blocks

Android apps are composed of Application Blocks, which are special kind of Android classes consisting several elements bundled together, including: Everything that comes in such package is coordinated by AndroidManifest.XML file. Especially, in this XML file (or by using various attributes on Activity class) it’s possible to register our Application Block to be “callable” by the others. We’ll see more details in a while, but generally your app block is “callable” when it can be used by the other apps – probably you’ve already seen Application Chooser screen on your Android phone, e.g. when you wanted to open downloaded movie and the system asked you which video player you want to use. The same can be done with your own app – if you’re developing a dialer app, you can register it to be callable for ACTION_DIAL Activity Action. Then, as soon as any other app uses Intent to make a phone call (which we’ll see in this post), Application Chooser appears and your app will be one of the possible ones to use for making this phone call.

What is an Intent ?

Android system is designed to be loosely-coupled. Moreover, there is a principle of least privilege introduced in system’s architecture. It means that each app has access only to Blocks which it requires to work. Different Blocks don’t know much about each other (even if defined within the same application). Here’s where an Intent comes into play. Application Blocks use Intents to asynchronously communicate with each other. In general, you can think of an Intent literally – it’s the intent (or a will) to do something. By sending Intents between different components, we can coordinate complex actions such as launching email app to send a message, using Location for obtaining user’s coordinates or navigating between application’s screens. For me, we can think of Intents and their usage from two perspectives:
  1. Intent as an intent – a will to do something. In this case, you use Intent to execute some action. When creating a new Intent you need to tell it what you want to do and give it necessary parameters for such operation (e.g. for send email action, you need to provide email address parameter)
  2. Intent as a message – bidirectional information having sender, recipient and potentially some content. This can be for instance navigating to a new screen (Activity), where sender is the original Activity, recipient is the destination Activity to be opened and the content may consist some parameters to pass between two screens.
We’ll now see those two approaches in details.

Intent as an intent

In order to trigger another (external) action, e.g. camera recording or email sending, we can create an Intent and start a new Activity. Let’s see it by the example I implemented in MoneyBack – I added an additional ListActivity (PeopleListActivity class) which is the list of people added to app’s database. When a person on the list is clicked, I create an Intent in order to call his/her phone number as the following code presents: .gist table { margin-bottom: 0; } What those 4 lines of code do, is when tapping an entry from the list:
MoneyBack – people list Activity
it opens the phone dialer app with selected person’s phone number to call:
Phone Dialer opened
NOTE: If I had more than one dialer app installed (if the other dialer app has itself registered as receiver for ACTION_DIAL, of course) Android would ask me to choose which app I want to use. How did that happen? First actually interesting line of code is the following one:

var intent = new Intent(Intent.ActionDial, uri);

My list of must-have apps

In this post I’d like to share with you a few applications I use (both mobile and PC ones) that are must-haves for me. Maybe you’ll find some of them useful (if you’re not using them yet). You can click on each application’s logo to go to its homepage.

Windows PC apps



Evernote allows to store notes and synchronize them across multiple devices. I’d say it’s a very good alternative to OneNote used by many people. I prefer it to OneNote, because it’s simpler. Saved notes can contain photos, files and be quite rich-formatted. Free basic plan allows to synchronize two devices, which is enough for me. I don’t use Evernote on my mobile, I only use it for longer notes, e.g. meeting minutes, blog posts drafts/ideas etc.

As you may know

Unit Testing Xamarin application

Today we’ll see how to add unit tests to Xamarin Android application, testing both platform-independent logic and Android-specific features.

Issue with unit tests in Visual Studio 2017

When I started to create my data access methods in MoneyBack, I really wanted to start writing even some basic unit tests of that logic. My first trial was to add a new project to my solution which contains unit tests. So I checked what kind of projects templates I have available and I I found Unit Test App (Android) project template and added it to my solution. Generated project contained TestsSample class with some unit-looking tests methods, but I had no idea how to execute them. With ReSharper installed, I didn’t have any “Run Tests” option on this project. I read somewhere on the web that this project type is used to execute tests on the device, but I couldn’t figure out how to do it. I gave up. Then I found another project template – Class Library (Android) with nUnit. Sounds better, nUnit in the name suggests unit tests, so I added this project to my solution. This time when right-clicking the project I had “Run Tests” option, but when clicking on it I got the following exception coming from ReSharper:
ReSharper error when executing unit tests
“Hmm… Visual Studio 2017”, I thought. “Some bugs for sure”. And I… … didn’t give up this time and with some help of folks from StackOverflow I figured out how unit tests should be done in Xamarin app. Let’s see that in the next chapters.

Levels of testing Xamarin application

There are basically three “levels” of testing Xamarin apps:
  1. Classic unit tests of pure .NET/Mono
    • standard, good old unit tests of logic independent from targeting platform (Xamarin.Android/iOS/Windows Phone)
    • unit-testing frameworks can be used (nUnit / xUnit)
  2. Platform-specific tests
    • unit tests of functionalities specific to targeting platform (e.g. Bluetooth, Location, GPS, SMS etc.)
    • specific for each platform
    • don’t contain GUI tests
    • executed on the emulator/device
  3. UI tests
    • tests of UI elements of the app and how those react for input (touch) events
    • executed by cloud/testing lab services (local or remote)
We’ll dive into the first two, UI tests is more complex topic, which requires setting up tests lab or using cloud testing services. For small projects, in the beginning it’s not necessary.

Classic platform-independent unit tests

In order to simply use ReSharper for executing unit tests of logic, which is independent from Android/iOS/Windows Phone, there is a need to add a new project using template Unit Test Project (.NET Framework) to our Xamarin solution. To such project, you can add your favourite unit-testing framework (such as nUnit or xUnit) by simply installing it from Nuget.  After, ReSharper allows to run your unit tests as in old good times:
nUnit tests executed by ReSharper
The exception I was getting with ReSharper previously was because I added the project using template Class Library (Android) which has its target set to Android device/emulator. ReSharper doesn’t know about any Android-specific (or Xamarin-specific) testing, so it was displaying unhandled error. In order to test platform-specific (in out case Android-specific) functionalities of your application, the tests need targeting runtime environment to run. It means they need to be executed on the physical device or emulator. Providers of the most popular unit-testing frameworks created wrappers for on-device testing. I’m using

NUnit Xamarin Runners

Using SQLite database in Xamarin.Android

I started to define development tasks to be done within MoneyBack project in the coming days (maybe I’ll even publish my Trello board soon 🙂 ) and it turned out that each functionality the app is going to offer needs persistent storage (e.g. to store people, payments, events etc.). The obvious choice is the local database in which I could store my entities and application’s data. In this post, I’d like to show you how quickly and easily SQLite database can be added and started to be used in Xamarin.Android project using SQLite.NET and Visual Studio 2017.

What is SQLite?

SQLite is an open-source database engine, which allows to store data in SQL structures. The whole database is stored in a single file, which makes it easily manageable on mobile devices. It’s lightweight, small and easily portable. It’s also prepared to work on multiple platforms. There are some limitations in contrast to “classic” SQL database engines, including:
  • OUTER join syntax is not fully supported
  • for altering tables, only “RENAME” and “ADDCOLUMN” operations are available
  • writing to views is impossible (views are read-only).
SQLite database can be easily operated using SQLite.NET ORM library available to download and install via Nuget. Let’s see how to do it.

Installing SQLite.NET package

In order to install SQLite.NET package, simply open Android.Xamarin solution in Visual Studio, open Package Manager Console and type the following command:

install-package Sqlite-Net

Simple habits for better productivity

It’s been a really tough week, so today I have a non-technical post for you. I’m going to move on with the project this weekend, so I can put some technical stuff here tomorrow. If you don’t really care about productivity, organization of your days, planning of tasks to be done during next X days, this post is for you. I totally didn’t care as well, but I felt a bit disorganized, I knew I could do more or at least be conscious of what I’m able to do and what I’m not. Finally, without studying anything like GTD, I managed to introduce few simple habits to my everyday life that make my life easier, allow to complete some tasks faster and make me aware of what I have to do and when. I’d like to share those manners with you – maybe you’ll also find them helpful.

Write everything down

I heard from many people promoting better life organization, that writing things down is a very important step in being more productive. I definitely confirm. Writing things down cleans up your brain – it makes you not thinking about something you have to do – you just write it down and you are sure you’ll take care of it when the time comes. Such brain-liberty is very powerful. You can use your brain to think about the activity you’re actually working on at the moment, not having many “background” topics taking your brain’s “resources”. Of course, you need to find a place in which you store your notes (or tasks – whatever you call it). For me, such place must meet the following requirements:
  • be easy and fast to use in every situation
  • be accessible from every place, on every device
  • synchronizing across multiple devices
  • allowing to set deadlines for each task
  • giving a possibility to group tasks
I’ve personally tested few todo lists / tasks management applications, including

Freedcamp

Solving performance issues with dotTrace

If you’ve ever had any performance issues with your .NET applications, you may find this writing interesting. In this post I’d like to present to you a tool produced by JetBrains – dotTrace, showing how using only one of its functionalities can help in finding causes of performance issues in .NET applications.

Performance issue on production

Months ago some of your colleagues implemented an application and delivered it to the Customer. The app is “Countries and Cities Manager”, which is used by some administrator working in local government unit. That’s how it looks:
Countries and Cities Manager
That’s cool, isn’t it ? This “administration panel” allows to add a new city to the country. User fills name of the city and provides ISO code of the country considered. You may think “who would ever use it and for what?”. True. But first of all, this is just an example 🙂 , and secondly, imagine that Russia annexes some big European country. What happens then ? User administrating local government system must use your manager application to insert hundreds or thousands of new cities in Russia. Gotcha! The next day you receive a ticket in your company’s issues tracking system. Taking into consideration that an average number of cities in east-European countries is ~600, the ticket’s priority is set to “Blocking”. The description of the issue is:
  1. User fills the “City” and “Country ISO Code”, clicks “Add city”
  2. Waits for 10 seconds…
  3. Waits for 20 seconds…
  4. Waits for 30 seconds…
  5. Error message received:
    ISO not found
  6. User corrects “RU” to “RUS”
  7. Waits for 10 seconds…
  8. Waits for 20 seconds…
  9. Waits for 30 seconds…
  10.  City added!
  11. Scenario repeats for each new city to be added. Performance is unacceptable.
Initially you have no idea what could be the reason. Such simple operation and such terrible performance? Let’s see…

Looking for issue with dotTrace profiling

In such cases, especially if I don’t know the code of the application considered, the very first thing I do it to use dotTrace in order to profile the application when executing bad-performance actions. It provides many features, including: detecting performance bottlenecks in .NET applications getting information about calls execution times analyzing calls order on a timeline profilling SQL queries and HTTP requests profiling unit tests … and

many more

Hello Xamarin World – first steps

This time I want to say “Hello” to Xamarin world.

First thing that needs to be done before starting Xamarin development, is obviously the installation of all necessary components.

In this post I want to share my feelings about the installation (not without issues of course), configuration of my Xamarin Android solution and deployment process to the device.

Xamarin installation into Visual Studio 2015

I’ve created my account at, logged in and chose Download Xamarin for free to download the installation package. There are generally two IDEs that can be used for Xamarin development: Visual Studio or Xamarin Studio. I’ve already had VS 2015 installed and decided I don’t want to explore a new IDE, so I downloaded installer for Visual Studio. What’s more, following Xamarin’s website, “Xamarin Studio is no longer included” in the Windows installer and they “encourage Windows developers to move to Visual Studio”. Well, that’s understandable, Microsoft acquired Xamarin, didn’t they? 😉

BTW, when downloading Xamarin I was obliged to fill the “Company” field – does anyone know why is that?

 Android SDK and emulator images size

Interesting parts of the installation (if there could by any at all?) are Android Emulator installation which is then integrated into Visual Studio as well as Android SDK installation – fortunately it’s possible to change the location of Android SDK installation, because right after the setup it takes an enormous amount of disk space (with mandatory tools mentioned in the installation guide and two Android APIs):

Android SDK size - 41.6 GBs
Android SDK size

I’ve examined a bit more and it turns out that system images take the most of this space. Those are used only by emulators to be able to create a virtual device and debug on a simulator instead of deploying the app to a physical device every rime.

When I used to write some Android apps in Java and Android Studio long time ago, I found it very hard and complex to debug/test anything using emulator – it was simply very, very slow to load and work. This time I’ll see if emulator built in Visual Studio will do the job – if yes, why not using it ? I will of course also deploy directly to the device for tests, but it also takes time. I remember that when I wanted to test some small adjustments done “on the fly” (e.g. UI modifications/alignments) I dreamt about doing it quicker then deploying app to the phone every time. We’ll see, if emulator won’t do the job – I will clean up those folders.

 First issue in DSP

I had to create a separate paragraph for that 🙂 Of course, it must have finally happened – first issue on my DSP way! Installation error, of course – “Installation of ‘Xamarin’ failed with more then one exception (attempt 3). It was not possible to complete an automatic installation.”

I expected the installation to terminate and evening spent on looking for a solution, exploring StackOverflow threads, but to my surprise a quite familiar-looking screen appeared explaining what to do next, i.e. how to install all the components manually. I had to download and install Java JDK, Android SDK and Xamarin.

Android Solution Creation

VS Android Xamarin Solution
VS Android Xamarin Solution
  • Properties – here AssemblyInfo.cs is probably well-known to all .NET developers (for the others: this file is .NET assembly’s metadata file, containing basic data about our application, like its name, version, company etc.), but there is also AndroidManifest.xml, which contains details and requirements for Android application, e.g. app’s name, supported SDKs versions numbers and required permissions for which the end user is asked when installing the app
  • References – contains other assemblies required for the application to build; in case of Xamarin Android solution, it references both .NET assemblies like System or System.Core and Mono.Android assembly
  • Components – stores Xamarin development packages that can be retrieved from Components Store (something like Xamarin-specific Nuget, I guess 😉 )
  • Assets – allows to store additional files needed by the app (e.g. text files) available easily from the code
  • Resources – contains strings, images and layouts of the application

[Activity(Label = "MoneyBack", MainLauncher = true, Icon = "@drawable/icon")] read more...