Month: August 2013

Justifying block of text inside TextView in Android

2013-08-31 Android 15 comments

Justifying text inside TextView in Android is not that simple as we may expect it to be. Unfortunately, Android does not have attribute for TextView, which supports justifying text. We can set alignment to the right or to the left, but we cannot simply justify text with generic attributes. In order to do that, we need to perform a few tricks.

First of all, we need to create file named justified_textview.css and we need to put it into the assets/ directory of our project. We will use it later in our custom view. Source code of this CSS stylesheet is presented below:

Next, we need to create class for customized view extending WebView. Let’s call it JustifiedTextView.

After that, we need to update res/values/attrs.xml file and put inside the following declaration:

Now, we are ready to use our custom JustifiedTextView.
In order to do that, we need to create layout containing JustifiedTextView as follows:

Next, we can access JustifiedTextView in our activity:

We can also set text inside XML layout instead of doing this programmatically. It depends on our project.

We also should turn off hardware acceleration for given activity in AndroidManifest.xml file in order to bring this solution to work.

That’s it. It’s not perfect solution and it’s kind of workaround, but I haven’t found any better idea how to deal with this problem.
I tested a few solutions and this one is, in my opinion, the best available and most elegant option for now.

Please note: There may be a problem with proper execution of this solution on Android OS version lower than 3.0 (API 11).

If you know any better ideas, how to deal with this problem, please let me know about that in comments below this article.

Asynchronous loading and caching bitmaps with Volley

2013-08-26 Android 3 comments

As I mentioned in previous post, we can use Volley library from Google for very clean, simple and easy loading of the images from Internet.
Volley uses LRU cache, so first of all, we need to create BitmapLruCache class extending LruCache class.

Next, we need to create VolleyHelper class.

Then, somewhere in our activity, we can use the following code snippet:

As we can see, we can define image in case of error or no image.
What is nice and important, Volley will take care of asynchronous downloading of the bitmaps, so we don’t have to create additional AsyncTasks. It will also create cache in temporary memory and on disk with using LRU cache.

As we could easily notice, Volley is really good and small, but powerful library, which can make our life easier. It was also created and tested by Google, what assures its stability and quality.

Simple Android Networking

2013-08-22 Android No comments

I’ve recently found an interesting article about Simple Android Networking by Teehan+Lax where you can find easy way of handling network requests, JSON data and images. Described Volley library released by Google also takes care of caching data and memory performance. Linked article is based on: Google I/O Volley Talk.

How to change Fragment layout on orientation change?

2013-08-15 Android 8 comments

When we work with Activities in Android, defining different layouts for different screen orientations is easy. The only thing we need to do, is to create two *.xml files with the same name for layouts in two separate directories (res/layout/ and res/layout-land/). In Fragments it’s not that easy. In such case, we need to perform additional operations in order to achieve our goal.
Fragment in Android is not re-inflated on configuration change, but we can recreate layout and repopulate view manually.
We need to create two layouts for different orientations of the screen in the same way as for Activity:

  • res/layout/my_fragment.xml
  • res/layout-land/my_fragment.xml

Then, we need to put code shown below to our fragment.
Of course, it requires little adjustments depending on concrete project.

That’s it!

Clean Code Cheat Sheet

2013-08-11 Software Development No comments

Recently at BBV Software Services blog I have found very interesting and handy Clean Code Cheat Sheet, which can help you to be better programmer. This document is actually based on famous book Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin.