Simple ExpandableList in android
June 14, 2010 at 4:48 am | Posted in Uncategorized | Leave a commentIt took me quite some time to implement the use of expandable list. Things that I had to keep in mind were to make the list look presentable when expanded along with the proper functionality and secondly fetching the data fro m the database in the list.
The database operations were quite easy but understanding and writing the code for the list was a challenging task for me. Below I have a code snippet which will help u make the expandable list.
/*make 2 arraylists*/
ArrayList groupList=new ArrayList();
ArrhayList childList=new ArrioayList();
/*make 2 maps*/
HashMap groupMap=new HashMap();
HashMap childMap=new HashMap();
/*add items to groupMap*/
groupMap.put(“names”,Name);
groupMap.put(“age”,Age);
groupMap.put(“sex”,Sex);
groupMap.put(“time”,Time);
groupMap.put(“cc”,CC);
/*add groupMap to groupList*/
groupList.add(groupMap);
/*make an inner list for child*/
ArrayList inner=new ArrayList();
/*add items to childMap*/
childMap.put(“allergie”,”Aspirin”);
/*add childMap to inner list*/
inner.add(childMap);
/*add inner childList to main childList*/
childList.add(inner);
/*add these lists to expandable listadapter*/
this.setListAdapter(new SimpleExpandableListAdapter(this,
groupList,R.layout.simple_group_list, new String[]{“names”,”age”,”sex”,”time”,”cc”},new int[]{R.id.name,R.id.age,R.id.sex,R.id.time,R.id.cc},
childList,R.layout.simple_child_list, new String[]{“allergie”},new int[]{R.id.allergie}));
/*Here is a quick explanation of the arguments of SimpleExpandableListAdapter:
this: context
groupList: The list created which contains all items to be displayed in the group.
R.layout.simple_group_list: This is the xml which contains the layout for group.
new String[]{“names”,”age”,”sex”,”time”,”cc”}: Tags for the group items
new int[]{R.id.name,R.id.age,R.id.sex,R.id.time,R.id.cc}: Tags with which the group items are associated with in the simple_group_list xml.
childList: The list created which contains all items to be displayed in the child
R.layout.simple_child_list: This is the xml which contains the layout for child.
new String[]{“allergie”}: Tags for the child items
new int[]{R.id.allergie}: Tags with which the child items are associated with in the simple_child_list xml.
*/
This expandable list still lacks some features. What if you want to have dissimilar items in every child of one particular group? This feature can be nicely implemented if you use heterogeneous expandable list which is provided in android’s latest sdk release- 2.2 version.
Dealing with different screen resolutions
June 3, 2010 at 5:26 pm | Posted in Uncategorized | Leave a commentIn android programming, what most of the developers often miss out is thinking about the screen resolution. Most often applications are developed without taking into consideration the density or size and width of the device. This approach is fine till we are dealing with a single device. But once your app is in the market , its obvious that people wont be having the same device. Also you cant restrain them on buying only a fixed kind of device. So what to do in such a situation :
In order to test for different screen resolutions, we will create three emulators of different sizes. If you are using Eclipse IDE, then follow these steps:
Go to Window –> Android SDK and AVD Manager –> Create AVD –> Choose the target and select resolution as 320 by 480.
Create another avd device of size 480 by 600 and third avd device of size 600 by 800.
Now we have our three emulators ready , we can test the same application on these emulators.
Now we will see how we can write an efficient program which will appear the same on all the emulators. Lets follow these steps:
- While designing for the UI , all our widgets are created in the xml. Avoid giving padding in pixels. Also avoid giving fixed dimensions i.e setting the layout_width and height to rigid values(15px etc). These values will make the layout look differently when tested on different devices. The solution to this is that we must use dip padding wherever necessary. dip means density independent. Hence the widgets with dip dimensions will get adjusted on all devices.
- Next try to give dimensions, use wrap_content and fill_parent at most of the places.
- Avoid using RelativeLayout. Creating xml using Linear and TableLayout is a good practice.
Once this compatible layout is ready, start testing it on the three created emulators.
All the best !
Blog at WordPress.com.
Entries and comments feeds.
