Friday, September 17, 2010

ExpandableListView on Android

ListView is pretty widely used. There are situations when you would like to group/categorize your list items. To achieve such a thing on Android, you would probably use the ExpandableListView. The data to the ExpandableListView is supplied by a special kind of adapter called the SimpleExpandableListAdapter which extends the BaseExpandableListAdapter.

As with any other widget on Android, you are free to customize the widgets as per your needs. Here, I will show how to create such a custom list adapter for the ExpandableListView.

For this example, I want to show a list of vehicles with their names. Also, I want to group them according to their category.

I have 4 classes.
1. Vehicle : The parent class for the rest.
2. Car: Extends the Vehicle class.
3. Bus: Extends the Vehicle class.
4. Bike: Extends the Vehicle class.

I have a method called getRandomVehicle(String name) which returns a random vehicle instance setting the name that I pass. The vehicle can be a Bus, Car or a Bike. Its completely random.

In the ExpandableListAdapter, (the custom adapter), there's a method called addItem(Vehicle vehicle), which manages the groups and their children.

In the SampleActivity, I have initialized a blank ExpandableListAdapter and set it to the list view. Now, I start a thread, which gets a random vehicle after every 2 seconds, and adds it to the adapter, and calls the adapter to notify that the data has changed.

There are a few methods, in the ExpandableListAdapter, which you should go through carefully. I have two layout files, group_layout.xml and child_layout.xml which are used as the layout for the group views and the child views of the ExpandableListView.

There you go, you have a custom ExpandableListView. You can find the full source code for this example here, ready to run.

There are some more methods that you might be interested in, like, how to change the "arrow icon" for the group views(official doc link), or how to expand or collapse a group at will, or how to handle specific events like group collapsed or group expanded. Read the docs on ExpandableListView.

Monday, September 13, 2010

SDK Tools - ADT Plugin updated

Here comes a new update to the ADT plugin for eclipse and SDK tools.

After you update the SDK tools(r7) from the Android SDK manager or otherwise, you would be prompted to update the ADT plugin(0.9.8) as well. Go for it, and you will see quite a few changes.

In the SDK Tools:
http://developer.android.com/sdk/tools-notes.html 

In the ADT Plugin:
http://developer.android.com/sdk/eclipse-adt.html

For SDK Tools
- Support for library projects.
- Support for aidl files in library projects
- Support for extension targets in Ant build to perform tasks between the normal tasks
- Support for "headless" SDK update 
- Fixes location control in DDMS to work in any locale not using '.' as a decimal point

For ADT Plugin
- Rename package : One click change of project's package
- Support for library projects that don't have a source folder called src/
- Support for library projects that depend on other library projects
- Support for additional resource qualifiers: car/desk, night/notnight and navexposed/navhidden
- Adds more device screen types in the layout editor. (Nice)
- Fixes problems with handling of library project names

The most important update for me is the inclusion of more layout editor configurations by default. My last post on creating different configurations for different screen sizes here was just an example to create a configuration for working with different screen sizes at design time. It's a bonus that they are now included in there by default.

Once click package renaming is also a nice feature, in case you want to fork your project into different versions. Check them out.


Sunday, September 5, 2010

Getting started with Tablets - They are coming

So, Android phones are done. Now's the time to give the iPads a rest. If you would like to develop applications for Android Tablets, you need to get a few things up first.

First of all, you need to setup a few tablet emulators for your apps. This bit is rather simple. Create a new AVD. I guess this screenshot explains the rest. For some reasons, I have not been able to create Tablet AVDs on 1.6. But it definitely works on Android 2.2 (Froyo).

After you create this AVD, fire it up. And you should be able to see the Droid up and jumping.

You can now test out your apps on this tablet emulator. Well, since tablet devices will obviously come under large-screens, to make your designing layouts for large screens easy, you need to tweak up a few things on eclipse. Since, large screen devices take up the xml layouts from the "layout-large" and "layout-large-land" folders, if it all you have different layouts for large screens, to visualize your designs during your development, you need to add one more configuration for large screens.

Steps to add a new configuration

- Open eclipse. Open any layout xml, so that you can get to the screen where you can add a custom configuration.


- In the devices drop down, select custom to create a new configuration. Click on "Create" which will open up the configuration screen.

- On the new configuration screen, select "Nexus One" and click on "Copy" to create a copy which you can edit later.


- Once you get a copy, you can rename it to your own configuration name.

- Before you are done, you need to change a few values for both the layouts, landscape and portrait. Follow the screen shot.


Change x dpi, y dpi, Screen Size, Resolution and set it to values as shown above. Do it for both the orientations.

- Save the configuration. Now, whenever you change the configuration to your custom config, since this custom config is for the large screen devices, it would show you how your layouts will look on a real device. It will pick up the xml files correctly whenever you switch your configuration to large screens.