0

I am currently looking at adding a gravity drawer to my app and have been following a tutorial. My Question is I have created a string array with a list of items in res/value/strings and need to know how to add them to the menu and get the items to open new activities. Can any one help code below.

Strings:

<string-array name="items">
    <item>Home</item>
    <item>About</item>
    <item>LinkedIn</item>
    <item>Twitter</item>
    <item>Facebook</item>
    <item>Contact</item>
</string-array>

Java code:

    public class MainActivity extends Activity {

        private String[] drawerListViewItems;
        private DrawerLayout drawerLayout;
        private ListView drawerListView;
        private ActionBarDrawerToggle actionBarDrawerToggle;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // get list items from strings.xml
            drawerListViewItems = getResources().getStringArray(R.array.items);
            // get ListView defined in activity_main.xml
            drawerListView = (ListView) findViewById(R.id.left_drawer);

            // Set the adapter for the list view
            drawerListView.setAdapter(new ArrayAdapter<String>(this,
                    R.layout.drawer_listview_item, drawerListViewItems));

            // 2. App Icon
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

            // 2.1 create ActionBarDrawerToggle
            actionBarDrawerToggle = new ActionBarDrawerToggle(
                    this,                  /* host Activity */
                    drawerLayout,         /* DrawerLayout object */
                    R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                    R.string.drawer_open,  /* "open drawer" description */
                    R.string.drawer_close  /* "close drawer" description */
            );

            // 2.2 Set actionBarDrawerToggle as the DrawerListener
            drawerLayout.setDrawerListener(actionBarDrawerToggle);

            // 2.3 enable and show "up" arrow
            getActionBar().setDisplayHomeAsUpEnabled(true);

            // just styling option

            drawerListView.setOnItemClickListener(new DrawerItemClickListener());
        }

        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            actionBarDrawerToggle.syncState();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            actionBarDrawerToggle.onConfigurationChanged(newConfig);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

            // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
            // then it has handled the app icon touch event

            if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
                return true;
                    }

            {

                }



            return super.onOptionsItemSelected(item);
        }

        private class DrawerItemClickListener implements ListView.OnItemClickListener {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, ((TextView)view).getText(), Toast.LENGTH_LONG).show();
                drawerLayout.closeDrawer(drawerListView);

            }
        }
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
Super Sasquatch
  • 373
  • 1
  • 3
  • 12
  • check this link: http://www.compiletimeerror.com/2013/08/androidoptions-menu-in-android-example.html#.Vji6uaIlKi4 – Rama Nov 03 '15 at 13:48
  • 1
    You are using ArrayAdapter, isnt that already drawing your string array? – Nanoc Nov 03 '15 at 14:08
  • Create your own custom class deriving from ArrayAdapter and override public void onItemClick(AdapterView> adapterView, View view, int position, long id) – Shilpi Nov 03 '15 at 14:18

1 Answers1

0

It seems you are trying to create a simple drawler with a list of items. Instead of using custom layout could you check it using a simpler layout like this.

    drawerListView.setAdapter(new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1,
            drawerListViewItems));
bhdrkn
  • 6,244
  • 5
  • 35
  • 42