0

Here's my code -

MainActivity.java

public class MainActivity extends AppCompatActivity 
    {
        DrawerLayout mDrawerLayout;
        NavigationView mNavigationView;
        FragmentManager mFragmentManager;
        FragmentTransaction mFragmentTransaction;

        ProgressDialog pd;
        JSONArray fetchedResponse;

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

            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
            mNavigationView = (NavigationView) findViewById(R.id.somestuff);

            new GetDataFromService().execute(getString(R.string.api_url);

            mNavigationView.setNavigationItemSelectedListener(new                     
            NavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                menuItem.setChecked(true);
                mDrawerLayout.closeDrawers();

                //Do rest of the stuff
            }
        });
    }

    public class GetDataFromService extends AsyncTask<String, Void, String> {

       @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);

                pd.dismiss();

                try {

                    fetchedResponse = new JSONArray(s);

                    if (fetchedResponse != null) {
                        final Menu menu = mNavigationView.getMenu();
                            for (int i = 0; i < fetchedResponse.length(); i++) {
                                String menu_title = fetchedResponse.getJSONObject(i).getString("menu_title");
                                int menu_id = fetchedResponse.getJSONObject(i).getInt("menu_id");
                                menu.add(Menu.NONE, menu_id, Menu.NONE, menu_title);
                            }
                    }
                    else{
                        //do error related stuff
                    }

                }catch (JSONException e) {
                    StringWriter sw = new StringWriter();
                    e.printStackTrace(new PrintWriter(sw));
                    String exceptionAsString = sw.toString();

                    System.out.println("Unexpected Error :");
                    System.out.println(exceptionAsString);
                }               
            }



    }

activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/orange"
        android:id="@+id/toolbar"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:title="@string/app_name" />

    <!--<include layout="@layout/toolbar"/>-->

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/drawerLayout"
        >



        <FrameLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/containerView">
        </FrameLayout>



        <android.support.design.widget.NavigationView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:id="@+id/somestuff"
            app:itemTextColor="@color/black"
            app:menu="@menu/drawermenu"
            android:layout_marginTop="-24dp"
            android:theme="@style/MyNavigationView"
            />



    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

drawermenu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
</menu>

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <!-- colorPrimary is used for the default action bar background -->
        <item name="windowActionModeOverlay">true</item>
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/orange</item>
        <item name="colorPrimaryDark">@android:color/holo_orange_dark</item>
    </style>

I am able to populate the MenuItems but I don't know how to add separators between each items (as they are dynamic) and how to highlight each item on selection?

John Doe
  • 63
  • 1
  • 1
  • 6

1 Answers1

1

Solved it 2 days ago, but sorry to post the answer too late. Hope the solution helps someone. For highlighting the drawer menu item, enable the group checkable property of menu while creating it dynamically.

So in MainActivity.java where the menu items are getting created, just add the following code

//Enable the group checkable property
menu.setGroupCheckable(<any integer representing group id>,true,true);

//Then highlight the default menu item 
menu.getItem(0).setChecked(true);

Then in activity_main.xml inside the "android.support.design.widget.NavigationView" tag, add the following line -

android:theme="@style/MyNavigationView"

And then finally, in styles.xml create the above mentioned theme in style tag as follows -

<style name="MyNavigationView" parent="Widget.Design.NavigationView">
    <item name="android:dividerHeight">1dp</item>
    <item name="android:layout_marginTop">10dp</item>
    <item name="colorControlHighlight">#3dfdfd</item>
</style>

Secondly, as it turns out, I'm satisfied with way it looks hence no need for separator as at any given time at least one item is selected.

Hope this helps someone :)

John Doe
  • 63
  • 1
  • 1
  • 6