Navigation box and activity in Android

I have tried the navigation drawer (slide menu) provided in this tutorial.

The difference with the link above and mine is that instead of calling fragments, I am trying to invoke an action. When the app opens I cannot see the navigation drawer menu. I only see the action bar with active HOME activity.

Here is the code I changed: (Do I need to have a Fragment or can I use the Activity for my first screen in the navbox?)

    mTitle = mDrawerTitle = getTitle();

    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
    navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);    

    navDrawerItems = new ArrayList<NavDrawerItem>();

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(1, -1)));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(2, -1),true, "200"));

    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
    mDrawerList.setAdapter(adapter);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.drawer,
            R.string.drawer_open,
            R.string.drawer_close
            ) 
    {
        public void onDrawerClosed(View view) 
        {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) 
        {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) 
    {
        displayView(0);
    }
}

private class SlideMenuClickListener implements
ListView.OnItemClickListener
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        displayView(position);
    }
}
private void displayView(int position) 
{
    switch (position) 
    {
    case 0:
        //fragment = new HomeFragment();        

        Intent intent = new Intent(this, Home.class);
        startActivity(intent);

        return;

    case 1:
        //fragment = new FindPeopleFragment();

        Intent intent1 = new Intent(this, Profile.class);
        startActivity(intent1);
        break;

    case 2:
        //fragment = new PhotosFragment();

        Intent intent2 = new Intent(this, Details.class);
        startActivity(intent2);
        break;

    default:
        break;
    }

    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    setTitle(navMenuTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

      

How do I fix this to display the navigation drawer in my home activities?

Update:

I even tried the next option given in this link:

How can I call one of my actions using the navigation drawer? but I don't get the navigation slide menu yet.

+3


source to share


5 answers


You cannot do this ... The move box is a layout from an activity and you cannot show an activity inside another activity, you have to use fragments for that!

An activity is a screen, you cannot show a screen inside another screen, but a fragment can be a screen component and you can inflate a fragment inside an activity container and then show it to the user.



If you don't, you can create an abstract object and inherit, but you will have no activity in the fragment, you will have multiple actions with each one with your own navigation box.

+2


source


If you look at the example documentation for the Android Navigation drawer, it is explicitly defined to use snippets instead of actions to load different "pages" from the navigation bar. As the life cycle of an Android app goes on, Fragments are easier to load and have less impact on the Android system. they can also be easily backed up and replaced with other fragments.

Having said that, your best approach would be to convert your actions into fragments and use them to load the various pages you need in your application. This is not such a difficult task and I will show you how:

  • Change all actions to fragments

    public class nameOfFragment extends Fragment { }
    
          

  • Use onCreateView method instead of onCreate

    public View onCreateView() {
    
        View view = inflater.inflate(R.layout.activity_main, container, false);
    
        //any other elements in that view need to be included here in this manner.
        Button rate = (Button) rootView.findViewById(R.id.rate);
    
        return view;
    }
    
          



These changes should be enough to change your activity into fragments. You must do the same for all the fragments that are accessible through the navigation bar.

Please post your code for the actions you are using if you need more help. Hope this helps :)

+1


source


Do you have some code to create and display the navigation bar? Place your code above in the base class of the activity. Every action you want in the navbar must then inherit from this base class.

Google IO 2014 app does the same, look at the source here .

Once you have done this, you will need to find a way to preserve the state of the box as you move on to the next step. I'm not sure how to do this, but you will probably find the answer in the source code for the IO application.

+1


source


If you should have one instance of a crate, you need to go through the fragments.

If you have no other choice than activities, define a base class for all of your activities (say BaseActivity). And in BaseActivity - you can do the necessary coding to integrate the box. Every activity extending BaseActivity will now display the drawer menu (but the instances will be different)

+1


source


Now you need to use the toolbar. Android created the appcompat library to implement material design in earlier versions of android. You need to use compile dependency appcompat. Change style.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>

</resources>

      

Then we'll choose not to have the old action bar. After that, we will make a contract between the navigation drawer and the toolbar. Therefore, we don't need to use the old action bar now. You can provide the code below:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff6d7fe2"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp"
    ></android.support.v7.widget.Toolbar>

      

+1


source







All Articles