Reputation: 307
i am trying to develop the custom navigation drawer with out using action bar and tool bar ,when user clicks the button the navigation will open.if you have any sample please share
Upvotes: 0
Views: 14602
Reputation: 467
With just a simple button or any view with a click listener attached, you can just use the code snippet below:
if(bi.drawerLayout.isDrawerOpen(Gravity.LEFT)){
bi.drawerLayout.closeDrawer(Gravity.LEFT);
}else{
bi.drawerLayout.openDrawer(Gravity.LEFT);
}
Upvotes: 0
Reputation: 2032
You set DrawerLayout below My Toolbar.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/toolbar" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_below="@+id/toolbar"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Content Your Main"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Upvotes: 0
Reputation: 2032
this is a example
Create a Fragment is NavigationDrawerFragment.class
public class NavigationDrawerFragment extends Fragment {
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private int mFragmentId;
private View mFragmentContainerView;
public interface EventDrawerListener {
void someEvent(String s);
}
private EventDrawerListener mEventDrawerListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.slide_menu, container, false);
return v;
}
public void setUp(int fragmentID, DrawerLayout drawerLayout, EventDrawerListener eventDrawerListener) {
this.mFragmentId = fragmentID;
this.mEventDrawerListener = eventDrawerListener;
mFragmentContainerView = getActivity().findViewById(mFragmentId);
mDrawerLayout = drawerLayout;
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, R.string.open, R.string.close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActivity().invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private ActionBar getActionBar() {
return ((AppCompatActivity) getActivity()).getSupportActionBar();
}
}
in activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/toolbar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Content Your Main"/>
</RelativeLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="your.package.NavigationDrawerFragment"
android:layout_width="@dimen/width_menu"
android:layout_height="match_parent"
android:layout_gravity="start"
android:splitMotionEvents="false"
tools:layout="@layout/slide_menu" />
</android.support.v4.widget.DrawerLayout>
in MainActivity.class
public class MainActivity extends Activity{
private NavigationDrawerFragment mNavigationDrawerFragment;
private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpDrawer();
ImageView image=(ImageView) findViewById(R.id.image_home);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
} else {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
}
});
}
private void setUpDrawer() {
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mNavigationDrawerFragment.setUp(R.id.relative_category, (DrawerLayout) findViewById(R.id.drawer_layout), this);
mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
}
}
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<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/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="@color/color_bg_splash"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/image_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:gravity="center"
android:paddingBottom="@dimen/padding16dp"
android:paddingRight="@dimen/padding16dp"
android:paddingTop="@dimen/padding16dp"
android:src="@drawable/icon_menu" />
</android.support.v7.widget.Toolbar>
Hope It help you !!!
Upvotes: 1
Reputation: 722
Use the DrawerLayout
mDrawerLayout = (DrawerLayout) getView().findViewById(R.id.drawer_layout);
//To Open:
mDrawerLayout.openDrawer(Gravity.START);
//To Close:
mDrawerLayout.closeDrawer(Gravity.END);
Upvotes: 10
Reputation: 209
you need to use a 'DrawerToggle', like this
//initialisation left drawer
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
mToolbar, R.string.app_name, R.string.app_name) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
//syncState();
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
//syncState();
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
Upvotes: -1
Reputation: 75629
If you use DrawerLayout
from support library you got opneDrawer()
methods there:
See docs: https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
Upvotes: 1
Reputation: 401
You can use drawerLayout.openDrawer(GravityCompat.START);
Implementation
In you activity
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
XML for navigation view (should be inside Drawerlayout)
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:fitsSystemWindows="true"
android:overScrollMode="always"
app:headerLayout="@layout/drawer_header"
app:itemBackground="@@android:color/white"
app:itemIconTint="@color/app_primary"
app:itemTextColor="@color/app_primary"
app:menu="@menu/drawer_menu">
Upvotes: 1