-
-
Save Fuhrmann/384503916f24b931e02b932fc3d6f6b6 to your computer and use it in GitHub Desktop.
Navigation Drawer Sizing according to Material Design
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v4.widget.DrawerLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/main_activity_rootLayout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:fitsSystemWindows="true"> | |
<!-- The main content view --> | |
{...} | |
<!-- The navigation drawer --> | |
<include | |
layout="@layout/navigation_drawer"/> | |
</android.support.v4.widget.DrawerLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{...} | |
import android.app.Fragment; | |
import android.app.FragmentTransaction; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.graphics.PorterDuff; | |
import android.os.Bundle; | |
import android.support.v4.widget.DrawerLayout; | |
import android.support.v7.app.ActionBarDrawerToggle; | |
import android.support.v7.widget.Toolbar; | |
import android.view.Gravity; | |
import android.view.View; | |
import android.widget.FrameLayout; | |
import android.widget.ImageView; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
import com.android.volley.toolbox.NetworkImageView; | |
{...} | |
/** | |
* This is the main activity of the application. | |
* Top hierarchy level. | |
* Holds the navigation drawer. | |
* | |
* Uses ScrimInsetsFrameLayout from Google http://goo.gl/07TJnm | |
* Uses Google Volley from Google http://goo.gl/b9sqAD | |
* Uses CircleImageView http://goo.gl/mV22sU | |
* | |
* @author https://plus.google.com/+PabloCostaTirado/posts | |
*/ | |
public class MainActivity extends ActionBarActivity | |
{ | |
private final static double sNAVIGATION_DRAWER_ACCOUNT_SECTION_ASPECT_RATIO = 9d/16d; | |
private DrawerLayout mDrawerLayout; | |
private FrameLayout mFrameLayout_AccountView; | |
private ScrimInsetsFrameLayout mScrimInsetsFrameLayout; | |
{...} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main_activity); | |
initialization(); | |
} | |
/** | |
* Bind, create, initialise and set up the resources | |
*/ | |
private void initialization() | |
{ | |
{...} | |
// Navigation Drawer | |
mFrameLayout_AccountView = (FrameLayout) findViewById(R.id.navigation_drawer_account_view); | |
mDrawerLayout = (DrawerLayout) findViewById(R.id.main_activity_rootLayout); | |
mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primaryDark)); | |
mScrimInsetsFrameLayout = (ScrimInsetsFrameLayout) findViewById(R.id.main_activity_navigation_drawer_rootLayout); | |
{...} | |
// Navigation Drawer layout width | |
final DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mScrimInsetsFrameLayout.getLayoutParams(); | |
int possibleMinDrawerWidth = UtilsDevice.getScreenWidth(this) - | |
UtilsMisc.getThemeAttributeDimensionSize(this, android.R.attr.actionBarSize); | |
int maxDrawerWidth = getResources().getDimensionPixelSize(R.dimen.navigation_drawer_max_width); | |
params.width = Math.min(possibleMinDrawerWidth, maxDrawerWidth); | |
mScrimInsetsFrameLayout.setLayoutParams(params); | |
mFrameLayout_AccountView.getLayoutParams().height = (int) (params.width | |
* sNAVIGATION_DRAWER_ACCOUNT_SECTION_ASPECT_RATIO); | |
{...} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{...} | |
import android.content.Context; | |
import android.util.DisplayMetrics; | |
/** | |
* This utility class is for device related stuff. | |
* | |
* @author https://plus.google.com/+PabloCostaTirado/posts | |
*/ | |
public class UtilsDevice | |
{ | |
/** | |
* Returns the screen width in pixels | |
* | |
* @param context is the context to get the resources | |
* | |
* @return the screen width in pixels | |
*/ | |
public static int getScreenWidth(Context context) | |
{ | |
DisplayMetrics metrics = context.getResources().getDisplayMetrics(); | |
return metrics.widthPixels; | |
} | |
/** | |
* Returns the size in pixels of an attribute dimension | |
* | |
* @param context the context to get the resources from | |
* @param attr is the attribute dimension we want to know the size from | |
* | |
* @return the size in pixels of an attribute dimension | |
*/ | |
public static int getThemeAttributeDimensionSize(Context context, int attr) | |
{ | |
TypedArray a = null; | |
try | |
{ | |
a = context.getTheme().obtainStyledAttributes(new int[] { attr }); | |
return a.getDimensionPixelSize(0, 0); | |
} | |
finally | |
{ | |
if(a != null) | |
{ | |
a.recycle(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment