Skip to main content

Featured post

Tennis-Paddle game

Logging levels and Resources in Android

Logging and Resources in Android

In these series of posts, we are going to learn about the Android permission system, Network IO and how to move time consuming tasks off the main UI thread. In this post we will learn about the logging levels in android and the use of Resources directory in Android.


Whenever you run an application, you might have seen a window pop up showing different messages like “A slow app needing to skip frames” or “An Error occurred” or perhaps “Some request or process finished”. These messages are called Log Messages. Developers use log messages to communicate information about their application. When you use logs in your app, you must declare the log level that your app must use.

There are 5 log levels in Android:

  • ·       ERROR: Use error to log any error messages
  • ·       WARN: Use warn to log messages that wont crash your app but remain a concern.
  • ·       INFO: Info is mainly used to log informative messages like “CONNECED TO THE INTERNET”.
  • ·       During development, we can use both debug and verbose log messages.
  • ·       DEBUG
  • ·       VERBOSE

Error, warn and info messages are always preserved in release versions. It means that you use those messages when your application is released to the public. The generic form of a log message would be:
                                   Log.x(String TAG,String message);

Tag can be any string you want but it is recommended to use the class name as the Tag. This makes it easier to find the logs as all apps dump their log messages into the same bucket.
Android has on more logging level which is more severe than error. It is called WTF!

WTF stands for “WHAT A TERRIBLE FAILURE”. WTF is a logging level that should be used for errors that should never ever happen and most developers should never ever use it. When some error occurs WTF may halt the process and display a debug message. Because of the unpredictable behaviour of WTF, it is suggested to keep this logging level for knowledge purpose.


What is the res Directory?

The res directory is where you should put things such as images, strings, and layouts. It's included in every Android project, and you can see it in Android Studio here:

Inside of the res directory, are sub folder for the following types of resources. You may have a subset of these directories, depending on the types of resources you're using in your app. Here are some examples

Different Resource Directories
This information can also be found here.

Some Common Resource Types
What's Stored Here
XML files that contain simple values, such as string or integers
A bunch of visual files, including Bitmap file types and shapes. More information is here
XML layouts for your app

Why Resources

You should always keep things like images and layouts separate in the res folder. Keeping resource files and values independent helps you easily maintain them if you need to update, say, all your button images to match a new style. The Android Framework also easily allows for alternative resources that support specific device configurations such as different languages or screen sizes. Providing a customized experience for users from different locations or on different devices becomes increasingly important as more of the world comes online and more devices come on the market. We will see how to provide alternate resources for different configurations and locals later in this course.

Using Resources in XML and Java

You've already seen resources in action. For example, in the MainActivity, you have already seen usage of resources. When we say setContentView(R.layout.activity_main), we are referencing a resource (the activity_main.xml) file to use as the layout of MainActivity. That magical looking R.layout part of the expression above is actually a static class that is generated for us to reference resources in Java code. Working with strings.xml
In Java, you can get a String saved in res -> values -> strings.xml by calling the getString method. If you’re in an Activity, you can just call getString, and pass in the String resource ID. The String resource ID can be found in the strings.xmlXML. For example, let's look at an example strings.xml file:

    <string name="today">Today</string>

    <!-- For labelling tomorrow's forecast [CHAR LIMIT=15] -->
    <string name="tomorrow">Tomorrow</string>

    <!-- Date format [CHAR LIMIT=NONE] -->
    <string name="format_full_friendly_date">
        <xliff:g id="month">%1$s</xliff:g>, <xliff:g id="day">%2$s</xliff:g>

The id of the String with the value "Today" is today and the id of the String with the value <xliff:g id="month">%1$s</xliff:g>, <xliff:g id="day">%2$s</xliff:g> is format_full_friendly_date
If you wanted to reference the Today string, you would reference it in Java by doing something like this:
                         String myString = getString(;

In XML, you can access a String by using the @string accessor method. For the same String defined above, you could access it like this:

                                       <TextView text=”@string/today” />

For more information on String Resources check out the documentation.


Popular posts from this blog


Angular JS is an open source framework built on JavaScript. It was built by the
developers at Google. This framework was used to overcome obstacles encountered while
working with Single Page applications. Also, testing was considered as a key aspect while
building the framework. It was ensured that the framework could be easily tested. The
initial release of the framework was in October 2010.

Features of Angular 2:Components: The earlier version of Angular had a focus of Controllers but now has changed the focus to having components over controllers. Components help to
build the applications into many modules. This helps in better maintaining the
application over a period of time.

TypeScript: The newer version of Angular is based on TypeScript. This is a
superset of JavaScript and is maintained by Microsoft.

Services: Services are a set of code that can be shared by different components
of an application. So for example, if you had a data component that picked data
from a database, you …

What’s the difference between AngularJS, Angular2 and Angular4?

One question that often comes out is “What is the basic difference between AngularJS, Angular 2 and Angular 4 and how to jump from Angular 2 to Angular 4?”

Angular JS was introduced in 2010 as a JavaScript framework for building client side single page web applications. So it gained popularity and the Angular team at google started to add some more features to the core. But the framework was not designed with the needs of today’s applications in mind and moreover it was totally complex. So the Angular team decided to rewrite the entire framework using TYPESCRIPT and as a result Angular 2 was released in mid 2016. The new Angular framework is completely different from the previous version and we can think of it as a completely different framework compared to the earlier one.

The decision was frustrating to most of the developers since a lot of applications have been designed using AngularJS. I personally liked the direction that Angular developers took in rewriting the entire framework a…

Efficiency of an algorithm

Efficiency of an algorithm
Programmers find a tough time writing efficient code. But what exactly do we mean by efficiency? What difference does it make if we write normal code instead of an efficient code? Let’s find out with a simple example of traditional sorting problem.

The first, known as insertion sort, takes time roughly equal to c1*n2 to sort n items, where c1 is a constant that does not depend on n. That is, it takes time roughly proportional to n2. The second, merge sort, takes time roughly equal to c2*n lg n, where lg n stands for log2 n and c2 is another constant that also does not depend on n. Insertion sort usually has a smaller constant factor than merge sort, so that c1 < c2. We shall see that the constant factors can be far less significant in the running time than the dependence on the input size n.

    Where merge sort has a factor of lg n in its running time, insertion sort has a factor of n, which is much larger. Although insertion sort is usually faster than mer…