Here is the solution. Thank you Mark Murphy for you guidance and for
the excerpt from your book (found on the link you provided above).

The purpose of this exercise is to use ListView for app navigation
instead of a menu.

In /res/layout create menu_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="6dip" />

    <TextView
        android:id="@+id/summary"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_toRightOf="@id/icon"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:textAppearance="@style/TextAppearance.Summary" />

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/icon"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_above="@id/summary"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_vertical"
        android:textAppearance="@style/TextAppearance.Title" />

</RelativeLayout>

In /res/xml create file main_screen_actions.xml that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android";>
        <item icon="@drawable/icon1" title="Guest" summary="Perform guest
operations." />
        <item icon="@drawable/icon2" title="Login" summary="Login to your
account." />
        <item icon="@drawable/icon3" title="Register" summary="Create new
account." />
</resources>


Create class:
public class ListMenuDescriptor {
        public int iconId;
        public String title;
        public String summary;

    /**
     * Constructor
     *
     * @param iconId Id of the icon resource.
     * @param title Menu item title.
     * @param summary Short menu description.
     */
        public ListMenuDescriptor(int iconId, String title, String summary) {
                this.iconId = iconId;
                this.title = title;
                this.summary = summary;
        }
}


Create class:
public class ListMenuArrayAdapter extends
ArrayAdapter<ListMenuDescriptor> {
        private LayoutInflater _inflater;

        /**
         * Item tag for list menu items describing xml resource file.
         */
        public static final String ITEM_TAG = "item";
        /**
         * Icon resource attribute.
         */
        public static final String ICON_ATTR = "icon";
        /**
         * Title attribute.
         */
        public static final String TITLE_ATTR = "title";
        /**
         * Summary attribute.
         */
        public static final String SUMMARY_ATTR = "summary";

        /**
         * Constructor
         *
         * @param context
         *            The current context.
         * @param items
         *            List menu items.
         */
        public ListMenuArrayAdapter(Context context,
                        ArrayList<ListMenuDescriptor> items) {
                super(context, R.layout.menu_list_item, items);

                _inflater = (LayoutInflater) context
                                
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        /**
         * Creates ListMenuArrayAdapter from xml resource.
         *
         * @param context
         *            The context.
         * @param listMenuItemsResId
         *            Id of the resource describing list menu items.
         * @return ListMenuArrayAdapter
         */
        public static ListMenuArrayAdapter createFromResource(Context
context,
                        int listMenuItemsResId) {

                ArrayList<ListMenuDescriptor> items = new
ArrayList<ListMenuDescriptor>();

                // inflate and create the list
                XmlResourceParser parser = context.getResources().getXml(
                                listMenuItemsResId);
                try {
                        while (parser.getEventType() != 
XmlResourceParser.END_DOCUMENT) {
                                if (parser.getEventType() == 
XmlResourceParser.START_TAG) {
                                        String tag = parser.getName();
                                        if (tag.equals(ITEM_TAG)) {
                                                items.add(new
ListMenuDescriptor(parser.getAttributeResourceValue(null, ICON_ATTR,
0),
                                                                
parser.getAttributeValue(null, TITLE_ATTR),
                                                                
parser.getAttributeValue(null, SUMMARY_ATTR)));
                                        }
                                }
                                parser.next();
                        }
                } catch (XmlPullParserException xpe) {
                        Log.e(ListMenuArrayAdapter.class.getName(),
Log.getStackTraceString(xpe));
                        throw new RuntimeException(xpe);
                } catch (IOException ioe) {
                        Log.e(ListMenuArrayAdapter.class.getName(),
Log.getStackTraceString(ioe));
                        throw new RuntimeException(ioe);
                } finally {
                        parser.close();
                }

                return new ListMenuArrayAdapter(context, items);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
{
                View row = convertView;

                if (row == null) {
                        row = _inflater.inflate(R.layout.menu_list_item, 
parent, false);
                }

                ListMenuDescriptor descriptor = this.getItem(position);

                TextView txt = (TextView) row.findViewById(R.id.title);
                txt.setText(descriptor.title);

                txt = (TextView) row.findViewById(R.id.summary);
                txt.setText(descriptor.summary);

                ImageView ico = (ImageView) row.findViewById(R.id.icon);
                ico.setImageResource(descriptor.iconId);

                return row;
        }
}

And finally in your ListActivity wire it up like so:
this.setListAdapter(ListMenuArrayAdapter.createFromResource(this,
R.xml.main_screen_actions));

I hope somebody will find that useful.

On Aug 9, 7:53 am, gnugu <[email protected]> wrote:
> Great Mark, thank you!
> I didn't know that one can do xml-de. I thought that only values could
> be localized.
>
> I'll try to put it together and post the solution here for others.
>
> Thank you again!
>
> On Aug 9, 7:47 am, Mark Murphy <[email protected]> wrote:
>
>
>
> > On Mon, Aug 9, 2010 at 10:41 AM, gnugu <[email protected]> wrote:
> > > And one more question. If I define my own plain XML resource like in
> > > my post above, what about the localization? Would I put the xml file
> > > in "values" and later "values-DE" and it would work just like with the
> > > strings?
>
> > Well, I'd put the XML in res/xml/ and res/xml-de/. But, otherwise,
> > yes, it just works.
>
> > --
> > Mark Murphy (a Commons 
> > Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>
> > _The Busy Coder's Guide to *Advanced* Android Development_ Version 1.9
> > Available!

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to