Android tricks – How to add an icon to your Toolbar?

Today I had to implement something which is not so popular throughout the Android universe. I had to implement a simple Toolbar with a logo on it’s left side. That’s not so popular these days, but from time to time there always will be someone who wants something like that. All in all, the end result had to be similar to the one down below:

toolbar_icon
Toolbar with icon

Read More »

Geo-fencing with MVP Part 2

In the previous post, we talked about the idea behind geo-fencing, why is it so useful and what is the reason behind choosing a particular design pattern like MVP. It was most like an introductory step before getting our hands dirty with some actual source code. Thus, in this part of the series, I will try to go through the different layers of the MVP pattern and will try to explain as much as possible the things that are happening there.

Read More »

Android tricks – How to prevent AlertDialog from dismissing after onClick event?

Have you noticed that the default behavior of AlertDialog is to disappear after onClick event? I’m sure you have! But there are cases when we actually don’t want to dismiss the dialog. In these situation, the default behavior of the AlertDialog must be overridden. Down below you will find a snippet which is doing exactly that thing.

Read More »

Geo-fencing with MVP Part 1

Several weeks ago, I had to implement a geo-fencing functionality in one of the projects on which I was currently working on. Due to the fact that I had never done that before, I had to do a ‘small’ research on the topic. Of course, tools like Google, Stackoverflow, the Android Documentation, etc. were there to help me. Happily, I managed to inform myself on the topic, but there was a small detail. I failed to find a good example of how to implement all the logic by using the MVP design pattern. So, that is how my journey began and step by step it led me to write these several articles.Read More »

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 »