Java Code Examples for android.support.v4.widget.DrawerLayout#LayoutParams

The following examples show how to use android.support.v4.widget.DrawerLayout#LayoutParams . 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: SideBarWithBg.java    From FantasySlide with Apache License 2.0 6 votes vote down vote up
private void init(SideBar sideBar) {
    setLayoutParams(sideBar.getLayoutParams());

    int parentLayoutGravity = ((DrawerLayout.LayoutParams) getLayoutParams()).gravity;
    this.sideBar = sideBar;
    bgView = new SideBarBgView(getContext());
    bgView.setParentLayoutGravity(parentLayoutGravity);
    addView(bgView, 0, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    // 将背景色转移给 bgView, 并清除 sideBar 自身的背景色
    bgView.setDrawable(sideBar.getBackground());
    sideBar.setBackgroundColor(Color.TRANSPARENT);
    sideBar.setParentLayoutGravity(parentLayoutGravity);

    addView(sideBar, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
 
Example 2
Source File: MainActivity.java    From KA27 with Apache License 2.0 6 votes vote down vote up
/**
 * A function to set Navigation Drawer Parameters
 *
 * @return the LayoutParams for the Drawer
 */
private DrawerLayout.LayoutParams getDrawerParams() {
    DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mScrimInsetsFrameLayout.getLayoutParams();
    int width = getResources().getDisplayMetrics().widthPixels;

    boolean tablet = Utils.isTablet(this);
    int actionBarSize = Utils.getActionBarHeight(this);
    if (Utils.getScreenOrientation(this) == Configuration.ORIENTATION_LANDSCAPE) {
        params.width = width / 2;
        if (tablet)
            params.width -= actionBarSize + (35 * getResources().getDisplayMetrics().density);
    } else params.width = tablet ? width / 2 : width - actionBarSize;

    // Allow configuration of the Navigation drawer to the right side rather than the left
    if (Utils.getBoolean("Navbar_Position_Alternate", false, this)) {
        params.gravity = Gravity.END;
    }

    return params;
}
 
Example 3
Source File: DrawerLayoutInstaller.java    From TvRemoteControl with Apache License 2.0 6 votes vote down vote up
private DrawerLayout createDrawerLayout(){
    if (drawerRootResId != 0){
        return (DrawerLayout) LayoutInflater.from(activity).inflate(drawerRootResId, null);
    }else {
        DrawerLayout drawerLayout = new DrawerLayout(activity);

        FrameLayout contentView = new FrameLayout(activity);
        drawerLayout.addView(contentView, new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        FrameLayout leftDrawer = new FrameLayout(activity);
        int drawerWidth = drawerLeftWidth != 0 ? drawerLeftWidth : DEFAULT_LEFT_DRAWER_WIDTH_DP;

        final ViewGroup.LayoutParams leftDrawerParams = new DrawerLayout.LayoutParams(
                (int)(drawerWidth * Resources.getSystem().getDisplayMetrics().density),
                ViewGroup.LayoutParams.MATCH_PARENT,
                Gravity.START
        );
        drawerLayout.addView(leftDrawer, leftDrawerParams);
        return drawerLayout;
    }
}
 
Example 4
Source File: MainActivity.java    From KA27 with Apache License 2.0 5 votes vote down vote up
public static void reconfigureNavigationDrawer(Context context) {
    if (mScrimInsetsFrameLayout != null) {
        DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mScrimInsetsFrameLayout.getLayoutParams();
        // Allow configuration of the Navigation drawer to the right side rather than the left
        if (Utils.getBoolean("Navbar_Position_Alternate", false, context)) {
            params.gravity = Gravity.END;
        } else {
            params.gravity = Gravity.START;
        }
    }
}
 
Example 5
Source File: MainActivity.java    From kernel_adiutor with Apache License 2.0 5 votes vote down vote up
/**
 * A function to calculate the width of the Navigation Drawer
 * Phones and Tablets have different sizes
 *
 * @return the LayoutParams for the Drawer
 */
private DrawerLayout.LayoutParams getDrawerParams() {
    DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mScrimInsetsFrameLayout.getLayoutParams();
    int width = getResources().getDisplayMetrics().widthPixels;

    boolean tablet = Utils.isTablet(this);
    int actionBarSize = Utils.getActionBarHeight(this);
    if (Utils.getScreenOrientation(this) == Configuration.ORIENTATION_LANDSCAPE) {
        params.width = width / 2;
        if (tablet)
            params.width -= actionBarSize + (35 * getResources().getDisplayMetrics().density);
    } else params.width = tablet ? width / 2 : width - actionBarSize;

    return params;
}
 
Example 6
Source File: DebugDrawer.java    From DebugDrawer with Apache License 2.0 5 votes vote down vote up
/**
 * helper to extend the layoutParams of the drawer
 *
 * @param params
 * @return
 */
private DrawerLayout.LayoutParams processDrawerLayoutParams(DrawerLayout.LayoutParams params) {
    if (params != null) {
        if (drawerGravity != 0 && (drawerGravity == Gravity.RIGHT || drawerGravity == Gravity.END)) {
            params.rightMargin = 0;
            if (Build.VERSION.SDK_INT >= 17) {
                params.setMarginEnd(0);
            }

            params.leftMargin = activity.getResources().getDimensionPixelSize(R.dimen.dd_debug_drawer_margin);
            if (Build.VERSION.SDK_INT >= 17) {
                params.setMarginEnd(activity.getResources().getDimensionPixelSize(R.dimen.dd_debug_drawer_margin));
            }
        }

        if (drawerWidth > -1) {
            params.width = drawerWidth;
        } else {
            params.width = UIUtils.getOptimalDrawerWidth(activity);
        }
    }

    return params;
}
 
Example 7
Source File: MainActivity.java    From Overchan-Android with GNU General Public License v3.0 5 votes vote down vote up
private void updateTabPanelTabletWeight() {
    if (MainApplication.getInstance().settings.isRealTablet() && !MainApplication.getInstance().settings.showSidePanel()) {
        DrawerLayout.LayoutParams sidebarLayoutParams = (DrawerLayout.LayoutParams) findViewById(R.id.sidebar).getLayoutParams();
        Point displaySize = AppearanceUtils.getDisplaySize(getWindowManager().getDefaultDisplay());
        sidebarLayoutParams.width = (int) (displaySize.x * MainApplication.getInstance().settings.getTabPanelTabletWeight());
        findViewById(R.id.sidebar).setLayoutParams(sidebarLayoutParams);
    }
}
 
Example 8
Source File: DrawerBuilder.java    From MaterialDrawer-Xamarin with Apache License 2.0 5 votes vote down vote up
/**
 * build the drawers content only. This will still return a Result object, but only with the content set. No inflating of a DrawerLayout.
 *
 * @return Result object with only the content set
 */
public Drawer buildView() {
    // get the slider view
    mSliderLayout = (RelativeLayout) mActivity.getLayoutInflater().inflate(R.layout.material_drawer_slider, mDrawerLayout, false);
    mSliderLayout.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(mActivity, R.attr.material_drawer_background, R.color.material_drawer_background));
    // get the layout params
    DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mSliderLayout.getLayoutParams();
    if (params != null) {
        // if we've set a custom gravity set it
        params.gravity = mDrawerGravity;
        // if this is a drawer from the right, change the margins :D
        params = DrawerUtils.processDrawerLayoutParams(this, params);
        // set the new layout params
        mSliderLayout.setLayoutParams(params);
    }

    //create the content
    createContent();

    //create the result object
    Drawer result = new Drawer(this);
    //set the drawer for the accountHeader if set
    if (mAccountHeader != null) {
        mAccountHeader.setDrawer(result);
    }

    //toggle selection list if we were previously on the account list
    if (mSavedInstance != null && mSavedInstance.getBoolean(Drawer.BUNDLE_DRAWER_CONTENT_SWITCHED, false)) {
        mAccountHeader.toggleSelectionList(mActivity);
    }

    //handle if the drawer should be shown on first launch
    handleShowOnFirstLaunch();

    //forget the reference to the activity
    mActivity = null;

    return result;
}
 
Example 9
Source File: DrawerUtils.java    From MaterialDrawer-Xamarin with Apache License 2.0 5 votes vote down vote up
/**
 * helper to extend the layoutParams of the drawer
 *
 * @param params
 * @return
 */
public static DrawerLayout.LayoutParams processDrawerLayoutParams(DrawerBuilder drawer, DrawerLayout.LayoutParams params) {
    if (params != null) {
        if (drawer.mDrawerGravity != null && (drawer.mDrawerGravity == Gravity.RIGHT || drawer.mDrawerGravity == Gravity.END)) {
            params.rightMargin = 0;
            if (Build.VERSION.SDK_INT >= 17) {
                params.setMarginEnd(0);
            }

            params.leftMargin = drawer.mActivity.getResources().getDimensionPixelSize(R.dimen.material_drawer_margin);
            if (Build.VERSION.SDK_INT >= 17) {
                params.setMarginEnd(drawer.mActivity.getResources().getDimensionPixelSize(R.dimen.material_drawer_margin));
            }
        }

        if (drawer.mDisplayBelowStatusBar != null && drawer.mDisplayBelowStatusBar && Build.VERSION.SDK_INT >= 19) {
            params.topMargin = UIUtils.getStatusBarHeight(drawer.mActivity, true);
        }

        if (drawer.mDrawerWidth > -1) {
            params.width = drawer.mDrawerWidth;
        } else {
            params.width = DrawerUIUtils.getOptimalDrawerWidth(drawer.mActivity);
        }
    }

    return params;
}
 
Example 10
Source File: MainDrawerMenu.java    From 4pdaClient-plus with Apache License 2.0 5 votes vote down vote up
public MainDrawerMenu(MainActivity activity, SelectItemListener listener) {
    prefs = App.getInstance().getPreferences();
    DisplayMetrics displayMetrics = App.getInstance().getResources().getDisplayMetrics();
    float dpWidth = displayMetrics.widthPixels;
    if (dpWidth > displayMetrics.density * 400) {
        dpWidth = displayMetrics.density * 400;
    }
    dpWidth -= 80 * displayMetrics.density;
    mActivity = activity;
    mSelectItemListener = listener;
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);


    mDrawer = (NavigationView) findViewById(R.id.left_drawer);
    mDrawer.setNavigationItemSelectedListener(this);
    DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawer.getLayoutParams();
    params.width = (int) dpWidth;
    if ("right".equals(Preferences.System.getDrawerMenuPosition())) {
        params.gravity = Gravity.RIGHT;
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow_end, GravityCompat.END);
    } else {
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow_start, GravityCompat.START);
    }
    mDrawer.setLayoutParams(params);

    setNavigationItems();

    mDrawerToggle = new ActionBarDrawerToggle(mActivity, mDrawerLayout, mActivity.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            mActivity.hidePopupWindows();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();
}
 
Example 11
Source File: SlidingDebugMenu.java    From slidingdebugmenu with Apache License 2.0 5 votes vote down vote up
public SlidingDebugMenu(Context context,  AttributeSet attrs) {
    super(context, attrs);
    mModules = new ArrayList<>();
    mModuleViews = new HashMap<>();

    DrawerLayout.LayoutParams params = new DrawerLayout.LayoutParams(context, attrs);
    params.gravity = Gravity.RIGHT;
    setLayoutParams(params);
}
 
Example 12
Source File: GravityUtil.java    From FantasySlide with Apache License 2.0 4 votes vote down vote up
static int getGravity(View view) {
    if (view.getLayoutParams() instanceof DrawerLayout.LayoutParams) {
        return ((DrawerLayout.LayoutParams) view.getLayoutParams()).gravity;
    }
    throw new IllegalArgumentException("Not child of DrawerLayout");
}
 
Example 13
Source File: DrawerBuilder.java    From MaterialDrawer-Xamarin with Apache License 2.0 4 votes vote down vote up
/**
 * Call this method to append a new DrawerBuilder to a existing Drawer.
 *
 * @param result the Drawer.Result of an existing Drawer
 * @return
 */
public Drawer append(@NonNull Drawer result) {
    if (mUsed) {
        throw new RuntimeException("you must not reuse a DrawerBuilder builder");
    }
    if (mDrawerGravity == null) {
        throw new RuntimeException("please set the gravity for the drawer");
    }

    //set that this builder was used. now you have to create a new one
    mUsed = true;
    mAppended = true;

    //get the drawer layout from the previous drawer
    mDrawerLayout = result.getDrawerLayout();

    // get the slider view
    mSliderLayout = (RelativeLayout) mActivity.getLayoutInflater().inflate(R.layout.material_drawer_slider, mDrawerLayout, false);
    mSliderLayout.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(mActivity, R.attr.material_drawer_background, R.color.material_drawer_background));
    // get the layout params
    DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mSliderLayout.getLayoutParams();
    // set the gravity of this drawerGravity
    params.gravity = mDrawerGravity;
    // if this is a drawer from the right, change the margins :D
    params = DrawerUtils.processDrawerLayoutParams(this, params);
    // set the new params
    mSliderLayout.setLayoutParams(params);
    //define id for the sliderLayout
    mSliderLayout.setId(R.id.material_drawer_slider_layout);
    // add the slider to the drawer
    mDrawerLayout.addView(mSliderLayout, 1);

    //create the content
    createContent();

    //create the result object
    Drawer appendedResult = new Drawer(this);

    //toggle selection list if we were previously on the account list
    if (mSavedInstance != null && mSavedInstance.getBoolean(Drawer.BUNDLE_DRAWER_CONTENT_SWITCHED_APPENDED, false)) {
        mAccountHeader.toggleSelectionList(mActivity);
    }

    //forget the reference to the activity
    mActivity = null;

    return appendedResult;
}
 
Example 14
Source File: TabDrawerMenu.java    From 4pdaClient-plus with Apache License 2.0 4 votes vote down vote up
TabDrawerMenu(Activity activity, SelectItemListener listener) {
    Resources resources = App.getInstance().getResources();
    DisplayMetrics displayMetrics = resources.getDisplayMetrics();
    float dpWidth = displayMetrics.widthPixels;
    if (dpWidth > displayMetrics.density * 400) {
        dpWidth = displayMetrics.density * 400;
    }
    dpWidth -= 80 * displayMetrics.density;
    mActivity = activity;
    mSelectItemListener = listener;
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    Button closeAll = (Button) findViewById(R.id.closeAll);
    closeAll.setOnClickListener(v -> {
        if (App.getInstance().getTabItems().size() > 1)
            closeAllTabs();
        else {
            closeDialog();
            toggleOpenState();
        }
    });
    closeAll.setOnLongClickListener(v -> {
        toggleOpenState();
        closeDialog();
        return false;
    });


    mDrawer = (RelativeLayout) findViewById(R.id.tab_drawer);
    mListView = (ListView) findViewById(R.id.tab_list);
    mListView.setOnItemClickListener(new TabOnClickListener());
    mListView.setStackFromBottom(App.getInstance().getPreferences().getBoolean("tabsBottom", false));

    DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawer.getLayoutParams();
    params.width = (int) dpWidth;
    if ("right".equals(Preferences.System.getDrawerMenuPosition())) {
        params.gravity = Gravity.START;
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow_start, GravityCompat.START);
    } else {
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow_end, GravityCompat.END);
    }
    mDrawer.setLayoutParams(params);

    adapter = new TabAdapter(getContext(), R.layout.tab_drawer_item, App.getInstance().getTabItems());
    mListView.setAdapter(adapter);
}
 
Example 15
Source File: MainActivity.java    From DexMovingImageView with Apache License 2.0 4 votes vote down vote up
private void addSystemBarMargin(View view) {
    DrawerLayout.LayoutParams lp = (DrawerLayout.LayoutParams) view.getLayoutParams();
    lp.setMargins(lp.leftMargin, lp.topMargin + getStatusBarHeight() + getActionBarHeight(), lp.rightMargin, lp.bottomMargin + getNavigationBarHeight());
    view.setLayoutParams(lp);
}