Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup a main menu layout in Android?

For the app that I'm making, I plan on having a main menu composed of 6 different icons, with 2 per line. This is very similar to the Twitter main menu layout seen here: nothx

So basically... how should I go about setting up the XML? LinearLayout, TableLayout? And then, what do I actually do to get the icons and text to be evenly spaced and such? I've tried everything I can think of so far and to no avail.

like image 788
Kyle Hughes Avatar asked Aug 21 '10 02:08

Kyle Hughes


1 Answers

Yes use GridView & TextView (with CompoundDrawables) -- I did this before:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <GridView android:id="@+id/grid" android:numColumns="2"
        android:horizontalSpacing="20dip" android:verticalSpacing="20dip"
        android:stretchMode="columnWidth" android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>

MainActivity:

GridView grid = (GridView) findViewById(R.id.grid);
        grid.setAdapter(new HomeScreenShortcutAdapter());
        grid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id) {

                startActivity(i); // Specify activity through Intent i
            }
        });

public class HomeScreenShortcutAdapter extends BaseAdapter {



        HomeScreenShortcutAdapter() {

        }

        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv;
            final Object data = getItem(position);

            if (convertView == null) {

                tv = new TextView(getApplicationContext());
                tv.setGravity(Gravity.CENTER);

            } else {
                tv = (TextView) convertView;
            }

            Drawable icon = data.icon;
            CharSequence title = data.title;

            tv.setCompoundDrawablesWithIntrinsicBounds(
                    null, icon, null, null);
            tv.setText(title);
            tv.setTag(data);

            return tv;
        }

    }
like image 149
Sameer Segal Avatar answered Nov 09 '22 23:11

Sameer Segal