0

i want to display a list or some kind of menu in navigation drawer at runtime. I have one single activity in which i put fragment using frame layout. This fragment contains Webview. Whenever user clicks on navigation item, then webview opens its alternative link. I successfully received response from Retrofit 2.3, but now i dont know how to achieve this

            public void onResponse(Call<List<NavBarResult>> call, Response<List<NavBarResult>> response) {
                List<Result> result = response.body();
//what to do here ?
            }
            @Override
            public void onFailure(Call<List<NavBarResult>> call, Throwable t) {
//error messages here
            }

My JSON API look like:

[
{
menulist_id: "1",
mname: "Item 1",
mlink: "http://example.com/technology/",
},
{
menulist_id: "2",
mname: "Item 2",
mlink: "http://example.com/sports/",
}
]

I want to work like that all menu's items work automatically even if we change the data from API. Thanks in advance.

Rohit Singh
  • 411
  • 3
  • 15

2 Answers2

1

Possible duplicate.

Android - Add bottom navigation view dynamically

How can I add a menu dynamically to bottom navigation view?

You can dynamically get the menu from your navigation view, then add a new menu item with you JSON response.

In your OnNavigationItemSelectedListener you can change the URL of your webview.

EDIT 1

Maybe I misunderstood something. Do you want to populate a Navigation Bar with your JSON response ? In that case, my links above can help you.

Do you want to populate a Navigation Drawer with your JSON response ? In that case you should begin read this : https://developer.android.com/training/implementing-navigation/nav-drawer.html

Edit your activity layout :

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
  • content_frame will contain your fragment with a WebView.
  • left_drawer will contain your dynamics links.

Get the left_drawer :

mDrawerList = (ListView) findViewById(R.id.left_drawer);

When you get your JSON response just set a adapter to your mDrawerList :

public void onResponse(Call<List<NavBarResult>> call, Response<List<NavBarResult>> response) {
    // result is a private List<Result> attribute of the class.
    result = response.body();

    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<Result>(this,
    android.R.layout.simple_list_item_1, result));

    // Set the list's click listener
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}

This way, your Drawer Layout will be populated with your JSON response dynamically.

Then you just have to define your OnItemClickListener to call a new fragment when you click on an item of your list.

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

MyFragment is not developp here but it would contain a webView and wait for a ARG_LINK that should be an URL to display in its webView.

private void selectItem(int position) {
    // Create a new fragment and specify the link to show based on position
    Fragment fragment = new MyFragment();
    Bundle args = new Bundle();
    args.putString(MyFragment.ARG_LINK, result.get(position).getUrl());
    fragment.setArguments(args);

    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
                   .replace(R.id.content_frame, fragment)
                   .commit();

    // Highlight the selected item and close the drawer
    mDrawerList.setItemChecked(position, true);
    mDrawerLayout.closeDrawer(mDrawerList);
}

Hope it help you this time !

olivejp
  • 900
  • 1
  • 9
  • 14
0

You can add a navigation view in your navigation drawer then when you are receiving the items from the API you can add Items to the menu see below code to add the items from the menu.

final Menu menu = navigationView.getMenu();
//Here you should add the length of your json array instead 'i' .
for (int i = 1; i <= 3; i++) {
   menu.add("Runtime item "+ i);
}
Android Geek
  • 8,956
  • 2
  • 21
  • 35