Android tricks – Overriding toString() in POJOs

Although, this small trick is not specifically related to the Android SDK, it is still a useful one. The ROI (Return Of Investment and by investment I mean time) of overriding the toString() method of your POJOs (Plain Old Java Objects) is relatively high. At the end of the day, this small thing can make your developer life just a little bit better.

How to override the toString() method?

There are two different ways for overriding this method (and any other method as well) – manually or automatically. The second one is more interesting. You can use the Ctrl + O (for Windows) combination in Android Studio. It will automatically open a pop-up screen with methods which can be overridden. Just find the toString() method and press Enter. You can see the pop-up down below.

override

What to include into toString() & Why do we need that?

That’s absolutely up to you! Usually, you should provide some general info regarding the instance of the current POJO.

Let’s say you have a class called Person. In this class you have attributes like first name / last name / age / gender / etc. And constructing a String, into your toString() method, which contains a nicely formated info regarding that particular person is a great idea. In this way, whoever calls the toString() method to a particular instance of the Person POJO will get some useful information. On the other hand, if not overriding this method and still calling it, you will see some strange things like Person@asd21nfa or something similar. Sadly, that means nothing to us.

This approach is useful mainly during the development process, a.k.a. during debugging or simple logging. DO NOT USE THIS FOR PURPOSES DIFFERENT THAN DEBUGGING AND LOGGING!

Resources:

http://fragmentedpodcast.com/episodes/44/

Feel free to share, comment & give your opinion on the topic!
And only if you really REALLY liked the article, you can buy me a cup of coffee! Otherwise, don’t do it! Donate $1

Android tricks – ViewPager and Offscreen Tabs

As I wrote in one of my first posts, we use Tabs a lot in our applications. They provide a grate way of navigating to different screens through our app. And in order to use tabs, we have to work with something call a ViewPager. According to the official documentation, this is a layout manager that allows us to flip left and right through pages of data. These pages of data are usually composed of Fragments. And in order to increase the performance and efficiency of the application, the views of each fragment / tab, which is offscreen, can be destroyed. The idea is to keep as little information in memory as possible. Sometimes this optimization can lead to problems, if the number of offscreen tabs is not set properly. User input can be lost, views’ state can be reset, etc. Thus, we need to carefully choose that number of offscreen tabs.

Read More »

Flavors

Banana, blueberries, cherry, coffee, cream. These are all great ice cream flavors! Especially the first two! But because of the cold weather outside, we are not going to talk about ice cream. Instead, we will talk about one of the great features of the Android Gradle plugin called ‘Flavors’.

Read More »

Android tricks – Gradle and license agreements

Working with Gradle through Android Studio is fairly easy and pleasant thing. But sometimes, you will need to build your projects on a machine without Android Studio. Gradle can be really helpful during that process and can automatically download any missing SDK packages as long as the accepted license agreement for them is in place. If it is missing, then you will have a problem.

Read More »

Android tricks – Hide a folder from the Gallery app

Here is a small trick for preventing a folder from appearing in the Gallery application:

Code description:

The general idea is really simple. We just have to create an empty file with the name ‘.nomedia’. That’s all! And that’s exactly what we want to do with the code snippet above. When the media scanner detects the .nomedia file, the folder is just being skipped by the OS and thus it wont be visible from the Gallery application.

Why do we need it?!

The main reason for hiding a folder from the Gallery application is… well… because you just want that particular folder to remain hidden! Sometimes you have to cache images or any other media files and you usually store them in a folder. But the Gallery application is very good in finding directories which contain media files and so your folder appears in the Gallery app. And you will agree with me that it’s not a good idea to show the user all your dirty and nasty hacks, transformations and changes you had made to the media files used by your application. Thus, hiding such folders suddenly seems beneficial.

 

Feel free to share, comment & give your opinion on the topic!
And only if you really REALLY liked the article, you can buy me a cup of coffee! Otherwise, don’t do it! Donate $1

Useful Resources for Android Developers

During the past two years I’d managed to find several useful tools & resources, which I use over and over again during my every day work. There is no doubt, that almost all of them are well known to everyone, but I feel that I have to share them with you despite that fact. This is a mixture between information & tutorial resources, useful tools for networking and other small things that can make your developer’s life easier.Read More »

How to add margins between tabs in TabLayout

Tabs… Almost each and every application has at least one screen with tabs. Using that UI pattern gives us the desired high level organization of the content we have in our app.

Several months ago I had to implement a TabLayout with margins between the tabs, which at the begging seemed like a regular task. Sadly, I was wrong. I expected to be fairly easy, Read More »