Skip to main content

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.

Logging:

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.

Resources

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
Name
What's Stored Here
values
XML files that contain simple values, such as string or integers
drawable
A bunch of visual files, including Bitmap file types and shapes. More information is here
layouts
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>
    </string>

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(R.string.today);

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.
   

Comments

Popular posts from this blog

What is context in android ?

The topic of Context in Android seems to be confusing too many. People just know that Context is needed quite often to do basic things in Android. People sometimes panic because they try to do perform some operation that requires the Context and they don’t know how to “get” the right Context. I’m going to try to demystify the idea of Context in Android. A full treatment of the issue is beyond the scope of this post, but I’ll try to give a general overview so that you have a sense of what Context is and how to use it.
What exactly is Context?


Well, the documentation itself provides a rather straightforward explanation: The Context class is an “Interface to global information about an application environment". The Context class itself is declared as abstract class, whose implementation is provided by the Android OS. The documentation further provides that Context “…allows access to application-specific resources and classes, as well as up-calls for application-level operations such as…

LocationManager vs GoogleApiClient

User Location on Android
Getting the user’s location on Android is a little less straightforward than on iOS. To start the confusion, there are two totally different ways you can do it. The first is using Android APIs from android.location.LocationListener, and the second is using Google Play Services APIs com.google.android.gms.location.LocationListener. Let’s go through both of them.
1.Android’s Location API
The Android’s location APIs use three different providers to get location - ·LocationManager.GPS_PROVIDER — This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. ·LocationManager.NETWORK_PROVIDER — This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup. ·LocationManager.PASSIVE_PROVIDER — This provider will return locations generated by other providers. You passively receive location updates …

ANGULAR-2

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 …