Java Code Examples for android.support.v4.widget.DrawerLayout#openDrawer()

The following examples show how to use android.support.v4.widget.DrawerLayout#openDrawer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From wshell with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case R.id.menu_start:
		start(false);
		printStatus();
		break;
	case R.id.menu_stop:
		stop();
		printStatus();
		break;
       case android.R.id.home:
           DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
           if (drawer.isDrawerOpen(GravityCompat.START)) {
               drawer.closeDrawer(GravityCompat.START);
           } else {
               drawer.openDrawer(GravityCompat.START);
           }
           break;
       default:
           return super.onOptionsItemSelected(item);
	}
	return false;
}
 
Example 2
Source File: MainActivity.java    From SystemUITuner2 with MIT License 5 votes vote down vote up
@Override
public void onBackPressed() { //the drawer is annoying to drag out, so let's open it on back press instead
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else if (!drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.openDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}
 
Example 3
Source File: MainActivity.java    From BlackList with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            drawer.openDrawer(GravityCompat.START);
        }
        return true;
    }
    return super.onKeyUp(keyCode, event);
}
 
Example 4
Source File: HomeActivity.java    From GSoC-Info-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onBackPressed() {
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        drawer.openDrawer(GravityCompat.START);
    }
}
 
Example 5
Source File: ComponentsFragment.java    From fogger with Apache License 2.0 4 votes vote down vote up
@OnClick(R.id.openDrawerButton)
protected void onOpenDrawerButtonClick() {
    DrawerLayout drawerLayout = ((ComponentsActivity) getActivity()).drawerLayout;
    drawerLayout.openDrawer(Gravity.LEFT);
}
 
Example 6
Source File: DrawService.java    From Float-Bar with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 更新悬浮窗布局等
 */
private void updateUi() {
	RIGHT_MODE = !prefs.getDrawMode();
	DRAW_COLOR = prefs.getDrawColor();
	ALPHA = prefs.getDrawAlpha();

	// 获取浮动窗口视图所在布局
	layout = (LinearLayout) inflater.inflate(RIGHT_MODE ? R.layout.draw_right : R.layout.draw_left, null);
	// 添加悬浮窗的视图
	mWindowManager.addView(layout, wmParams);

	/**
	 * 设置抽屉控件的打开方向和监听器
	 */
	mDrawerLayout = (DrawerLayout) layout.findViewById(R.id.drawer_layout);
	mDrawerLayout.setDrawerListener(new MyDrawListener());
	mDrawerLayout.openDrawer(RIGHT_MODE ? Gravity.RIGHT : Gravity.LEFT);

	/**
	 * 设置上方的home键
	 */
	Button home = (Button) layout.findViewById(R.id.home_key);
	home.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			Util.virtualHome(getBaseContext());
			stopSelf();
		}
	});

	/**
	 * 设置抽屉控件内的背景
	 */
	drawContent = (LinearLayout) layout.findViewById(R.id.drawer_content);
	drawContent.setBackgroundColor(DRAW_COLOR);
	drawContent.getBackground().setAlpha(ALPHA);

	/**
	 * 设置最近任务list中item的个数:20
	 */
	Util.reloadButtons(this, appInfos, 20);
	ListView listView = (ListView) layout.findViewById(R.id.drawer_list);
	listView.setAdapter(new AppAdapter(this, mWindowManager, layout, mDrawerLayout, appInfos));

	// 悬浮窗显示确定右上角为起始坐标
	wmParams.gravity = RIGHT_MODE ? Gravity.RIGHT : Gravity.LEFT | Gravity.TOP;
	// 以屏幕右上角为原点,设置x、y初始值,确定显示窗口的起始位置
	// 添加动画。参考自:http://bbs.9ria.com/thread-242912-1-1.html
	wmParams.windowAnimations = (RIGHT_MODE) ? R.style.right_anim : R.style.left_anim;

	mWindowManager.updateViewLayout(layout, wmParams);
}
 
Example 7
Source File: ColorPaletteActivity.java    From MaterialDesignColorPalette with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ((BaseApplication)getApplication()).inject(this);
    mBus.register(this);

    setContentView(R.layout.activity_color_palette);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mColorList = mService.getPaletteColorSectionsList();

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.navigation_drawer);
    setupNavigationDrawer(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    if (savedInstanceState != null) {
        mFragment = (PaletteFragment) getFragmentManager()
                .getFragment(savedInstanceState, FRAGMENT_KEY);
        mPosition = savedInstanceState.getInt(POSITION_KEY);
        mDrawerTitle = savedInstanceState.getCharSequence(DRAWER_TITLE_KEY);
        mTitle = savedInstanceState.getCharSequence(TITLE_KEY);
    }
    if (mDrawerTitle == null) {
        mDrawerTitle = getTitle();
    }
    if (mTitle == null) {
        mTitle = getTitle();
    }

    selectItem(mPosition, mFragment, false);

    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    final boolean firstRun
            = prefs.getBoolean(FIRST_RUN, true);
    if (firstRun) {
        prefs.edit().putBoolean(FIRST_RUN, false).apply();
        mDrawerLayout.openDrawer(mDrawerList);
    }
}
 
Example 8
Source File: BaseDrawerFragment.java    From android-atleap with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mFragmentContainerView = getActivity().findViewById(mDrawerConfig.fragmentContainerId);
    View view = getActivity().findViewById(mDrawerConfig.drawerLayoutViewId);
    if (view != null && view instanceof DrawerLayout) {
        mDrawerLayout = (DrawerLayout)view;
    }

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    if (mDrawerLayout != null) {

        // set a custom shadow that overlays the main content when the drawer opens
        if (mDrawerConfig.drawerShadowResourceId != 0)
            mDrawerLayout.setDrawerShadow(mDrawerConfig.drawerShadowResourceId, GravityCompat.START);

        createActionBarDrawerToggle();

        // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
        // per the navigation drawer design guidelines.
        if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
            mDrawerLayout.openDrawer(mFragmentContainerView);
        }

        // Defer code dependent on restoration of previous instance state.
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    initUpIcon();

}
 
Example 9
Source File: NavDrawer.java    From smartcard-reader with GNU General Public License v3.0 4 votes vote down vote up
public NavDrawer(Activity activity, Bundle inState,
                 int resId, DrawerLayout drawerLayout, Toolbar toolbar) {
    mActivity = activity;
    mResId = resId;
    mDrawerLayout = drawerLayout;

    // this only takes effect on Lollipop, or when using translucentStatusBar on Kitkat
    drawerLayout.setStatusBarBackgroundColor(activity.getResources().getColor(R.color.primary));
    mParentItem = drawerLayout.findViewById(resId);
    mParentItem.setActivated(true);

    mDrawerToggle = new ActionBarDrawerToggle(activity, drawerLayout,
            toolbar, 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);
            // force call to onPrepareOptionsMenu()
            mActivity.invalidateOptionsMenu();
        }

        /** called when a drawer has settled in a completely open state */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            // force call to onPrepareOptionsMenu()
            mActivity.invalidateOptionsMenu();
        }
    };
    drawerLayout.setDrawerListener(mDrawerToggle);

    View appSelect = drawerLayout.findViewById(R.id.app_select);
    View batchSelect = drawerLayout.findViewById(R.id.batch_select);
    View emvRead = drawerLayout.findViewById(R.id.emv_read);
    View apps = drawerLayout.findViewById(R.id.apps);
    View settings = drawerLayout.findViewById(R.id.settings);

    appSelect.setOnClickListener(mClickListener);
    batchSelect.setOnClickListener(mClickListener);
    emvRead.setOnClickListener(mClickListener);
    apps.setOnClickListener(mClickListener);
    settings.setOnClickListener(mClickListener);

    if (inState != null) {
        if (inState.getBoolean("drawer_open")) {
            drawerLayout.openDrawer(Gravity.START|Gravity.LEFT);
        }
    }
}
 
Example 10
Source File: DrawService.java    From Float-Bar with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 更新悬浮窗布局等
 */
private void updateUi() {
	RIGHT_MODE = !prefs.getDrawMode();
	DRAW_COLOR = prefs.getDrawColor();
	ALPHA = prefs.getDrawAlpha();

	// 获取浮动窗口视图所在布局
	layout = (LinearLayout) inflater.inflate(RIGHT_MODE ? R.layout.draw_right : R.layout.draw_left, null);
	// 添加悬浮窗的视图
	mWindowManager.addView(layout, wmParams);

	/**
	 * 设置抽屉控件的打开方向和监听器
	 */
	mDrawerLayout = (DrawerLayout) layout.findViewById(R.id.drawer_layout);
	mDrawerLayout.setDrawerListener(new MyDrawListener());
	mDrawerLayout.openDrawer(RIGHT_MODE ? Gravity.RIGHT : Gravity.LEFT);

	/**
	 * 设置上方的home键
	 */
	Button home = (Button) layout.findViewById(R.id.home_key);
	home.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			Util.virtualHome(getBaseContext());
			stopSelf();
		}
	});

	/**
	 * 设置抽屉控件内的背景
	 */
	drawContent = (LinearLayout) layout.findViewById(R.id.drawer_content);
	drawContent.setBackgroundColor(DRAW_COLOR);
	drawContent.getBackground().setAlpha(ALPHA);

	/**
	 * 设置最近任务list中item的个数:20
	 */
	Util.reloadButtons(this, appInfos, 20);
	ListView listView = (ListView) layout.findViewById(R.id.drawer_list);
	listView.setAdapter(new AppAdapter(this, mWindowManager, layout, mDrawerLayout, appInfos));

	// 悬浮窗显示确定右上角为起始坐标
	wmParams.gravity = RIGHT_MODE ? Gravity.RIGHT : Gravity.LEFT | Gravity.TOP;
	// 以屏幕右上角为原点,设置x、y初始值,确定显示窗口的起始位置
	// 添加动画。参考自:http://bbs.9ria.com/thread-242912-1-1.html
	wmParams.windowAnimations = (RIGHT_MODE) ? R.style.right_anim : R.style.left_anim;

	mWindowManager.updateViewLayout(layout, wmParams);
}
 
Example 11
Source File: NavigationUI.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Handles the Up button by delegating its behavior to the given NavController. This should
 * generally be called from {@link AppCompatActivity#onSupportNavigateUp()}.
 * <p>If you do not have a {@link DrawerLayout}, you should call
 * {@link NavController#navigateUp()} directly.
 *
 * @param drawerLayout The DrawerLayout that should be opened if you are on the topmost level
 *                     of the app.
 * @param navController The NavController that hosts your content.
 * @return True if the {@link NavController} was able to navigate up.
 */
public static boolean navigateUp(@Nullable DrawerLayout drawerLayout,
        @NonNull NavController navController) {
    if (drawerLayout != null && navController.getCurrentDestination()
            == findStartDestination(navController.getGraph())) {
        drawerLayout.openDrawer(GravityCompat.START);
        return true;
    } else {
        return navController.navigateUp();
    }
}