Tuesday, 18 June 2013

Applying colorFilter to imageView

Need a different color to what you have in your drawable? Here is a quick code to change color? Note that this only works for pure white color drawables (bitmap,shape).

 imageView.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);  

Original source: http://stackoverflow.com/questions/10114420/applying-colorfilter-to-imageview-with-shapeddrawable

Thursday, 31 May 2012

Android Painful Orientation Change

Don't get me wrong because of the post title. I don't mean to insult Android.

The point is that it is really painful when you are fetching data from Web-services and put them into a list. When  device orientation changes, the activity reloads. And guess what, you are back to starting point (Fetch data again).

I found "faster screen orientation change" article on Android Developer site which is very useful. But personally it is not the easiest way to handle the change. I prefer to use this piece of xml to handle orientation change.



The result is pretty much I would have expected. People would say this is dirty solution or whatsoever. But hey, it works like a charm without having to rewrite java code.

Last but not least, you may notice there is extra piece of code "screenSize". This is for android API level 13 and above. From API level 13 onward, screen size also changes when we rotate our device. You may refer to Handling Runtime Changes articles on Android Developer site for more clear and precise information.

Tuesday, 17 January 2012

48 dp rule in touch screen control



I would quote the whole paragraph from Android Blog.

Why 48dp?

On average, 48dp translate to a physical size of about 9mm (with some variability). This is comfortably in the range of recommended target sizes (7-10 mm) for touchscreen objects and users will be able to reliably and accurately target them with their fingers.
If you design your elements to be at least 48dp high and wide you can guarantee that:
  • your targets will never be smaller than the minimum recommended target size of 7mm regardless of what screen they are displayed on.
  • you strike a good compromise between overall information density on the one hand, and targetability of UI elements on the other.
The whole point is to make touch controls 48x48 dp minimum so that user won't miss the target when touching the control.

Tuesday, 29 November 2011

Java ConcurrentModificationException Problem

As a java developer, it is inevitable to face multi-threading problems. One of them is

ConcurrentModificationException

My experience with it comes from accessing ArrayList items. The gist is simple - 

"Don't ever let another thread access arrayList item while another is iterating over it"  

 To solve this, I had found some ways as listed below. 

  • Locking The Collection
    • Optimistic Approach
    • Pessimistic Approach
  • Copy On Write
  • Fast Fail
Among them, my favourite is "Fast Fail" method. It has little overhead compared to others although it has its own pitfalls.

Implementation is simple. Just put a try catch bloc around the code used to iterate the list. In the catch bloc, just run the method/code bloc. That is. Simple!!!!!!!!!!!!!!!!!!!!!

 For more information on Concurrent Modification Exception solving method please refer to http://www.javaperformancetuning.com/articles/fastfail2.shtml

Monday, 28 November 2011

Limiting result from Android SQLite Database

Android has a very useful helper class to query sqlite database. It is safer than using raw query. It has a number of overload methods. To be exact, it can accept 7,8 and 9 arguments. Usually we use the method with 7 arguments.

public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)


In fact, we can append any extra sql at 7th argument. For limiting the query, we can do as such.

mDb.query(DATABASE_TABLE, new String[]{KEY_ROWID, KEY_TITLE}, null, null, null, null,"null LIMIT 1000");

This will also work just as 8 argument query method.  

Tuesday, 1 November 2011

Custom Seek bar/Loading bar Android

Customizing seek bar is inevitable in android development. Today i just saw a web post from someone about custom-style seek bar. It is a good explanation and sample code. Here is the link http://www.mokasocial.com/2011/02/create-a-custom-styled-ui-slider-seekbar-in-android/ . To sum up, you have to create 3 drawables and two xml files(one is also fine if you have less complicated loaded graphics).

3 drawables
  • Seek bar background
  • Thumbnail of seek
  • Progress drawable
2 xml files (Layer List)
  • Important one



  • Less Important one

Monday, 31 October 2011

Loader Loader Loader......

In android, getting cursor out of content resolver with query() method can be pain in the ass for pretty much of data. I had experienced it with around 20-30 rows of data from sqlite database. Fortunately, google brought cursor loader for those tasks. It is quite easy to implement as well. First you got to create an empty adapter using "null" as an cursor. Yes, use "null" as your cursor.


Next step is to initiate cursor loader
getLoaderManager().initLoader(0, null, this); 

If you are using compatibility pack, you will have to use
getSupportLoaderManager().initLoader(0,null,this); 

Here "0" is the key. Loader Manager will try to find loader with id "0". If can't be found, it create Loader with id 0. Next stop is to implements call-backs.

Of course, don't forget implements LoaderManager.LoaderCallbacks at class declaration. That is and we have a loader which does a nice threading for us.