Skip to content

Instantly share code, notes, and snippets.

@p-fischer
Last active October 26, 2018 08:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p-fischer/700fcdf5eff737ebf5ec4e64a0130e99 to your computer and use it in GitHub Desktop.
Save p-fischer/700fcdf5eff737ebf5ec4e64a0130e99 to your computer and use it in GitHub Desktop.
Create a fullscreen dialog with margins - either in normal or in immersive mode
package com.exmample.utils;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import static com.example.utils.DisplayDimensions.getAvailableScreenHeight;
import static com.example.utils.DisplayDimensions.getAvailableScreenWidth;
import static com.example.utils.DisplayDimensions.getPhysicalScreenHeight;
import static com.example.utils.DisplayDimensions.getPhysicalScreenWidth;
public class DialogUtils
{
public static Dialog setMargins( Dialog dialog, int marginLeft, int marginTop, int marginRight, int marginBottom, boolean immersive )
{
Window window = dialog.getWindow();
if ( window == null )
{
// dialog window is not available, cannot apply margins
return dialog;
}
Context context = dialog.getContext();
// set dialog to fullscreen
//RelativeLayout root = new RelativeLayout( context );
//root.setLayoutParams( new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ) );
dialog.requestWindowFeature( Window.FEATURE_NO_TITLE );
//dialog.setContentView( root );
// set background to get rid of additional margins
window.setBackgroundDrawable( new ColorDrawable( Color.WHITE ) );
// apply left and top margin directly
window.setGravity( Gravity.LEFT | Gravity.TOP );
LayoutParams attributes = window.getAttributes();
attributes.x = marginLeft;
attributes.y = marginTop;
window.setAttributes( attributes );
// set right and bottom margin implicitly by calculating width and height of dialog
int screenWidth = immersive ? getPhysicalScreenWidth( context ) : getAvailableScreenWidth( context );
int screenHeight = immersive ? getPhysicalScreenHeight( context ) : getAvailableScreenHeight( context );
int dialogWidth = screenWidth - marginLeft - marginRight;
int dialogHeight = screenHeight - marginTop - marginBottom;
window.setLayout( dialogWidth, dialogHeight );
return dialog;
}
}
package com.exmample.utils;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.WindowManager;
/**
* "physical" refers to the actual pixels the device can display
* "available" here means the usable space (minus status and navigation bar)
*/
public class DisplayDimensions
{
public static int getPhysicalScreenWidth( Context context )
{
return getRealMetrics( context ).widthPixels;
}
/**
* get physical pixel height - including status and navigation bar
*/
public static int getPhysicalScreenHeight( Context context )
{
return getRealMetrics( context ).heightPixels;
}
private static DisplayMetrics getRealMetrics( Context context )
{
DisplayMetrics displayMetrics = new DisplayMetrics();
// access physical pixels - some devices (e.g. Nexus 9) return a too small pixel height otherwise
getWindowManager( context ).getDefaultDisplay().getRealMetrics( displayMetrics );
return displayMetrics;
}
public static int getAvailableScreenWidth( Context context )
{
return getDisplayMetrics( context ).widthPixels;
}
/**
* get available pixel height - excluding status and navigation bar
*/
public static int getAvailableScreenHeight( Context context )
{
int screenHeight = getDisplayMetrics( context ).heightPixels;
// find out if status bar has already been subtracted from screenHeight
int physicalHeight = getPhysicalScreenHeight( context );
int statusBarHeight = getStatusBarHeight( context );
int navigationBarHeight = getNavigationBarHeight( context );
int heightDelta = physicalHeight - screenHeight;
if ( heightDelta == 0 || heightDelta == navigationBarHeight )
{
screenHeight -= statusBarHeight;
}
return screenHeight;
}
private static DisplayMetrics getDisplayMetrics( Context context )
{
DisplayMetrics displayMetrics = new DisplayMetrics();
// access physical pixels - some devices (e.g. Nexus 9) return a too small pixel height otherwise
getWindowManager( context ).getDefaultDisplay().getMetrics( displayMetrics );
return displayMetrics;
}
private static WindowManager getWindowManager( Context context )
{
return ( WindowManager ) context.getSystemService( Context.WINDOW_SERVICE );
}
public static int getStatusBarHeight( Context context )
{
Resources resources = context.getResources();
int resourceId = resources.getIdentifier( "status_bar_height", "dimen", "android" );
return ( resourceId > 0 ) ? resources.getDimensionPixelSize( resourceId ) : 0;
}
public static int getNavigationBarHeight( Context context )
{
Resources resources = context.getResources();
int resourceId = resources.getIdentifier( "navigation_bar_height", "dimen", "android" );
return ( resourceId > 0 ) ? resources.getDimensionPixelSize( resourceId ) : 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment