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
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:
<!-- For labelling tomorrow's forecast [CHAR LIMIT=15] -->
<!-- Date format [CHAR LIMIT=NONE] -->
<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(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.