Butterknife - A viewbinding tool for Android

Are you tired of writing findViewById(R.id.view) everywhere in your app where you are dealing with views? There is a tool by Jake Wharton at Square, called Butterknife that uses annotation processing to generate this boilerplate code for you. It can do field and method binding for your views, which can save you a lot of typing, and make your code a tad cleaner. Installation This is the easiest part ever. Add Butterknife to your app with Gradle.

» Read more

Useful ADB Commands

If you are developing Android apps, using the Android Debug Bridge tool (ADB) may sometimes be necessary, or could save you a lot of time. With ADB you can install/uninstall apps, forward ports, push/pull files to your device, remotely connect to SQLite databases on your device, record the device screen, and more. See the link at the bottom for a full reference. Setting up the ADB tool The ADB tool comes with the Android SDK, and you can

» Read more

How to fix ArrayIndexOutOfBoundsException in your custom ListAdapter

If you are creating your own ListAdapter, perhaps you want to show different list items, or list item headings in your list by overriding the getItemViewType and the getViewTypeCount in the implemented Adapter class, then you may have come across the ArrayIndexOutOfBoundsException, as the example below: ERROR/AndroidRuntime(400): FATAL EXCEPTION: main ERROR/AndroidRuntime(400): java.lang.ArrayIndexOutOfBoundsException ERROR/AndroidRuntime(400): at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:4540) ERROR/AndroidRuntime(400): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:

» Read more

Android string.xml resource formatting

If you are using the strings.xml file for your applications string resources (which you should!) then you might have wondered what to do if you need to combine a string and a value. Take this text for example: Hi, my name is Ruben and I am 26 years old String resource formatting This can easily be solved by using string formatting to insert the values; Ruben and 26. In you strings.xml file, create a new string resource like

» Read more

Android Volley library - Fixing the empty POST parameters

Recently, I came across the Volley library for Android. I already had an app that used network requests, to post and get data from a Node.js webserver with Express.js. The code I had for doing the network part, downloading JSON data and images, with memory and disk cache of images, was working fine, but I thought I’d make use of volley instead. As it had better support for certain things, one of them; grouping and cancelling requests

» Read more

Got a problem with spaces in strings.xml getting stripped away?

If you have a string in your strings.xml file that you want to have a space character in front or after the string itself, you may notice that this space gets trimmed away automatically. To avoid the string getting trimmed away, you can use one of the following tricks: Trick one – Wrapping the string in quotes If you wrap the string in a pair of quotes, the space will not get stripped away. Also, the quotes themselves does not

» Read more

How to extract strings into strings.xml resource files in Android Studio

I have previously posted a guide on how to extract string resources in Eclipse, so I thought I would do it for Android Studio too, as it is not directly obvious how to acheive this. All your applications strings should be placed in the various /values/strings.xml files in your app. This is the best practice, gathers all texts, and easily enables you to translate your app into other languages. While developing apps, it might be easier to just

» Read more

How to detect changes to your Android Preferences

This will demonstrate how to listen for changes to the user preferences in your app, so  that you can perform tasks depending on what the user selected/changed on the preferences screen. I have an app, with a listfragment displaying rows of text. There is also a preferences screen (SettingsActivity.java) where users can set the sorting order of the list. When users change the sort order, the list should be refreshed, to disaply the items in the new order.

» Read more

Delayed task execution in Java and Android

If you need to do some delayed task in for example your Android App, you can easily achieve this with a Java TimerTask. This is a short example, where some method ( removeDialog() ) is run with a delay of 2 seconds. //import statements import java.util.Timer; import java.util.TimerTask; //in your method, use the Timer Schedule function: new Timer().schedule( new TimerTask() { @Override public void run() { removeDialog(); } }, 2000 );

» Read more

Android Emulator: How to access the sqlite database of an app

This is an example of how to access the sqlite database of an Android app running on an emulator. Make sure you have the android adb tool on your PATH. Run this command in the terminal: adb version If you have adb on your path, it should show something similar to this: Android Debug Bridge version 1.0.31 If not, follow this guide: Adding the adb tool to the terminal for easy access. Accessing the SQLITE database Open a

» Read more