Reputation: 23
I want this drawerLayout(bellow photo) to always open like Samsung multiwindow sidebar.
Till now I am able to open this drawerLayout(sidebar) within the app. please give the instruction to be able to open this draweLayout(sidebar) from anywhere(I mean from any other app or even from home screen of a device).
Thanks in advance.
Here is my drayerLayout XML file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:background="@color/cardview_light_background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Swipe Right"
android:textSize="15dp"
android:textColor="#D50000"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open App In\nMultiWIndow Mode"
android:textSize="30sp"
android:gravity="center"
android:textStyle="bold"
android:textColor="@color/black"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
tools:context=".MainActivity2">
<ListView
android:id="@+id/listView"
android:layout_width="280dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
Upvotes: -1
Views: 251
Reputation: 320
This cannot be achieved by making something global you to do some modification in every activity
you can create a new class that will have a method to initialize the navigation, move navigation view to separate xml file and include that file in every activity, and call initialization method in every activity
You can try this library https://github.com/mikepenz/MaterialDrawer to move most xml code to java so you won't have to add DrawerLayout in every activity but choice is your, it always better to skip using 3rd party libraries if you can
if you are using library than here's some example you can follow
Constant.java
private void setupNavigationDrawer() {
ProfileDrawerItem section = new ProfileDrawerItem()
.withSetSelected(false)
.withSetSelected(false)
.withEnabled(false);
PrimaryDrawerItem joyType0DrawerItem = new PrimaryDrawerItem().withName("Basic Gamepad").withTag(KEY_BASIC_GAMEPAD_1).withLevel(2);
PrimaryDrawerItem joyType1DrawerItem = new PrimaryDrawerItem().withName("Assault Horizon").withTag(KEY_ASSAULT_HORIZON).withLevel(2);
PrimaryDrawerItem joyType2DrawerItem = new PrimaryDrawerItem().withName("Awesomenauts").withTag(KEY_AWESOMENAUTS).withLevel(2);
PrimaryDrawerItem joyType3DrawerItem = new PrimaryDrawerItem().withName("BombSquad").withTag(KEY_BOMBSQUAD).withLevel(2);
PrimaryDrawerItem joyType4DrawerItem = new PrimaryDrawerItem().withName("Final Fantasy XIII").withTag(KEY_FINAL_FANTASY_XIII).withLevel(2);
SectionDrawerItem sectionDrawerItem = new SectionDrawerItem().withName("Joystick Type").withTextColorRes(R.color.colorAccent);
PrimaryDrawerItem deviceDrawerItem = new PrimaryDrawerItem().withName("Device Connection").withTag(KEY_DEVICE_CONNECTION).withIcon(GoogleMaterial.Icon.gmd_devices);
PrimaryDrawerItem settingsDrawerItem = new PrimaryDrawerItem().withName("Settings").withTag("settings").withIcon(GoogleMaterial.Icon.gmd_settings);
ndMenu = new DrawerBuilder()
.withActivity(this)
.withToolbar(tbMain)
.withCloseOnClick(true)
.withFireOnInitialOnClick(true)
.withSelectedItemByPosition(6)
.withDelayDrawerClickEvent(300)
.withDisplayBelowStatusBar(true)
.withTranslucentStatusBar(false)
.withOnDrawerItemClickListener(this)
.addDrawerItems(section, deviceDrawerItem, settingsDrawerItem, sectionDrawerItem, joyType0DrawerItem, joyType1DrawerItem, joyType2DrawerItem, joyType3DrawerItem, joyType4DrawerItem)
.build();
findViewById(R.id.nav_button).setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// Check these functions from library example to check drawer state and open close drawer
if(ndMenu.isDrawerOpen){
ndMenu.closeDrawer();
} else {
ndMenu.openDrawer();
}
});
}
MainActivity.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<include
layout="@layout/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout>
nav_view.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
orientation="horizontal">
<Image
id="@+id/nav_button"
android:layout_width="30dp"
android:layout_height="30dp"/>
<LinearLayout>
Upvotes: 0