Deepak Sharma
Deepak Sharma

Reputation: 5073

Create both sided drawer with customization

I want to create drawer on both sided. Below is the images :-

Left drawer

Right drawer

I have created the left drawer successfully. Below is the code :

private FragmentDrawer drawerFragment;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerFragment = (FragmentDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
    drawerFragment.setDrawerListener(this);
    displayView(0);


}

@Override
public void onDrawerItemSelected(View view, int position) {
    displayView(position);
}

private void displayView(int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) {
        case 0:
            fragment = new HomeFragment();
            title = getString(R.string.title_dashboard);
            break;

        case 8:

            AppSharedPreference.getInstance(HomeActivity.this).setLogin(false);
            startActivity(new Intent(HomeActivity.this, LoginIcoachActivity.class));
            finish();
            break;

    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }
}

}

XML File :-

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/container_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
    </LinearLayout>

    <include android:id="@+id/home"
        layout="@layout/content_home"/>

</LinearLayout>

<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="fragment.FragmentDrawer"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

I have follow the below link :-

android hive

Please help me to achieve this.

Upvotes: 0

Views: 53

Answers (2)

Geeky Singh
Geeky Singh

Reputation: 1013

Try below code in your XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/container_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
    </LinearLayout>

    <include android:id="@+id/home"
        layout="@layout/content_home"/>

</LinearLayout>

<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="fragment.FragmentDrawer"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

<fragment
    android:id="@+id/fragment_navigation_drawer_right"
    android:name="fragment.FragmentDrawer" // some other fragment
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

In your activity, get fragment instance using fragment_navigation_drawer_right id.

Upvotes: 0

Karakuri
Karakuri

Reputation: 38595

You can add a second drawer the same way as the first one, just use android:layout_gravity="end".

Upvotes: 2

Related Questions