Sometimes you want to change content of your activity during some manipulation (it could tap on screen, some gesture or button click). If you have TextView and you can change text it is really easy to do. The same situation is with an image of ImageView. But sometimes the change is more complicated:

Change view to another view (same or different type)

Have animation during changing the content

In this case you can set visibility property on one view to View.GONE and to VIEW.VISIBLE to another. You should manage animation as well. But there is better solution – ViewAnimator.

ViewAnimator is ViewGroup. Actually it is extended from FrameLayout. So it could contain other views. But at any moment ViewAnimator can show no more than one view. We can ask ViewAnimator to switch to another view by calling showNext(), showPrevious() as well as setDisplayedChild(viewIndex) methods. And ViewAnimator is absolutely indifferent what kind of view it contains.

ViewAnimator would have another name if it was not be able to animate view changes. You can set in and out animation in xml or calling setInAnimation(animation), setOutAnimation(animation) methods.

That is it. It is really easy. Here is example. I set ViewAnimator in xml layout file. Add three child views to it. Then in java file I switch view when user touch screen. Moreover I create 2 alpha animation objects and attach it to the container (ViewAnimator). You can make animation in xml as well but to save a space I set it in code.

If you want views to be changed periodically you should use ViewFlipper. You don’t have to implement any timers. Just set interval by calling setFlipInterval() and then startFlipping() on ViewFlipper instance. You can set in on xml layout file with no need to write code.

Factory creates 2 objects immediately after it was set to ViewSwitcher instance. After it makeView() method is not called. So there are only 2 views in the container. I don’t see the reason to use the class. ViewAnimator can cope with the same task.

Judging by forums questions (and my own experience) sending mms programmatically is quite complicate issue. That is because there is no documented API for it. To start working with mms we will need to use some undocumented classes. Happily Android OS is open source project. So we can download some classes from http://android.git.kernel.org/ and use it in our code. I know that it is not the best solution (while in future version this code could be modified) but it is the only one if you want to work with mms from your code.

There are different Multimedia Message Types. Each of them has own purpose. Here is the most important ones that we will use below:

“m-send-req” – sending message

“m-notification-ind” – provide the MMS Client with information about a MM located at the recipient MMS Proxy-Relay and waiting for retrieval. Client after that should fetch MM. It doesn’t contain Message Body but only Header.

If result is 0 (APN_ALREADY_ACTIVE) we can start sending MMS. But likely it is not. In that case we have to wait while it become ACTIVE. We can register broadcast receiver that listens “ConnectivityManager.CONNECTIVITY_ACTION” action. At onReceive() method of our receiver we check whether connectivity manager is ready to send mms:

How to send mms when we are ready to do it? We can watch sources of standard messaging client that are open as almost everything in Android. To implement the same solution is not too complicated but quite intricate.

Or we can use API that was designed by Nokia that encapsulates all complexity. You can find more information and links here.

But none of the solutions works with Samsung (at list Galaxy S and Galaxy Tab). It throws exception during http request to mmsc. It seems that Samsung have changed some network properties. I have not yet figured out how to cope with it.

Ok, here we can make a try to update data in storage. But it is not necessary. Standard messenger will do it for us. While device is not rooted it is impossible to delete or shut down standard client. And it makes our way a bit easier.

But if we want to make it anyway we get delivery status and make appropriate update of storage:

When we have new inbox MMS we receive (in our broadcast receiver that is described above) pdu that has “m-notification-ind” type. But this pdu doesn’t contain multimedia data but only location at MMBOX of our mmsc where the content is situated. And we have to download it from that location.

Actually standard message client will do it for us. I will not write how to do it by our own. It is complicated process that take a lot of code at dozen classes. You can follow the path of tasks starting from com.android.mms.transaction.PduReceiver onReceive() method. If you really need the solution you can always use the code from that package.

This post is like a summary for me to remember. I have always easily used I/O Streams realizing that there are plenty of them but didn’t know the differences. So I decided to clarify it for me.

First of all streams differ from “destination”. Input Streams are used to read data from local files, networks, other programs etc. And Output Streams are for writing the data to a destination. Ok, it is obvious.

Character streams are intended to work with characters (16-bit code units). Character streams descent from Reader and Writer abstract classes. They are also easy recognizable by Reader/Writer suffix in classes names (FileReader, CharArrayWriter etc).

Character streams are based on byte streams. It is just high level solution to send/receive character data. All the data are transferred in bytes. But there should be something to translate character data to bytes. Here are “byte-to-character” bridges classes – InputStreamReader and OutputStreamReader. They read/write characters from/to bytes using specified charset that maps bytes to 16-bit Unicode units. How it is done?

You can notice that byte streams as well as character streams have method read/write that works with int. In case of byte stream int value include just 8 bit data. It could be 0-127 or -1 as end of file flag. Character stream works with 2-bytes data (so int data from read/write methods contains 2 bytes in it). And every time character stream read character data InputStreamReader request InputStream to read 2 bytes, then translate it to character code and pass to Character stream.

now looks quite logical. Yesterday I couldn’t explain why we should open 3 streams.

Thirdly streams could be differed by data they transfer. There are data streams that supports primitive data (boolean, char, short, int, long, float and double) and String values – DataInputStream and DataOutputStream. And there are object streams that use serialization and represented by ObjectInputStream and ObjectOutputStream. Such classes make work with data (primitive or object) easier.

If you work with Bitmaps in Android (pre Honeycomb) it is extremely easy to receive

OutofMemoryError: bitmap size exceeds VM budget

As you know all application have memory (heap size) limits. For some devices it is 16Mb, for others – 32 or 64Mb.

16Mb is not so much but in most cases (until you load 3264×2448 ARGB8888 bitmap) is enough for application. But why working with even small bitmaps OutOfMemory error occurs.

The reason of the error is Bitmap instances are stored not in managed heap as all other objects but in native heap. And memory limit includes the sum of managed and native heap sizes. And at least one reason why native memory is not called “managed” is a garbage collector can’t remove objects from it. You must do it by your own by calling recycle() method of your bitmap instance.

The recycle() method cleans the memory occupied by the object in native heap. It is done immediately after calling recycle(). And after that you shouldn’t use bitmap instance anymore. Overwise you will get

At this snippet we don’t actually create object in heap. We set inJustDecodeBounds flag for BitmapFactory.Options instance. This flag means that image data will be retrieved without creating the bitmap instance (and at this moment originalBitmap is null). After that we get actual width and height of the image.
Then you can set scale parameters to BitmapFactory.Options instance. But this scale parameter should be multiple of 2:

Here we calculated the scale that is multiple of 2 and close to the size we want to have. Then we set it to options.inSampleSize and decode the file. Besides that we set temp storage for decoding (16K) as well as RGB565 config. The last reduces decoded bitmap size 2 times relative to ARGB8888 config.

Someday I met a problem concerned retrieving data from Contact list in Android.

I had to get list of contacts comprising name and phone number without external apps. Sure there are plenty of tutorials and it is not hard to get the data. But the problem was all tutorials, answers in StackOverFlow, articles from developer.android.com gave solution that took 3-8 seconds to retrieve 100 contacts while it query Content Provider for every single contact to read its phone number. It was not an option for me. I couldn’t afford users to wait so long.

While I hadn’t coped to find solution in Internet I started to experiment with android.provider.ContactsContract. Hopefully I’ve found that I searched for.

But before the practise I will give a bunch of theory.

All the Contact data in Android have three level hierarchy (three-tier-model):

A row in the ContactsContract.Data table can store any kind of personal data, such as a phone number or email addresses. The set of data kinds that can be stored in this table is open-ended. There is a predefined set of common kinds, but any application can add its own data kinds.

A row in the ContactsContract.RawContacts table represents a set of data describing a person and associated with a single account (for example, one of the user’s Gmail accounts).

A row in the ContactsContract.Contacts table represents an aggregate of one or more RawContacts presumably describing the same person. When data in or associated with the RawContacts table is changed, the affected aggregate contacts are updated as necessary

Actually all the data is in Data layer. Other levels are abstraction that structures the way to that data.

So tutorials suggested:

to get Contact list querying ContactsContract.Contacts.CONTENT_URI that is the most abstract level

iterate through contacts and query ContactsContract.CommonDataKinds.Phone.CONTENT_URI to retrieve phone number list for each contact (it is a bit more specific abstraction layer)

All this two steps asks data from abstraction that based on third layer – Data (ContactsContract.Data). But the problem is the structure of the Data table is really confusing and there is no description of it. It is easy to explain while the table is universal and contain data about everything (names, emails, groups, phone numbers etc). And there are a lot of columns that in one case could contain group name but in another email or phone number.

It took a bit time to analyze the structure and I got the solution.

So to get cursor with names and phones you just should make such query:

The first column is id of the data row. The second is Contact name and the last on is its phone number. It is sorted by name. If contact has more than one phone number there would be several rows (one for each number). If you need to group them you can do it programmatically and it takes much less time than querying each contact separately.