private void showCheatsheet ( Context context , View view ) { final int [ ] screenPos = new int [ 2 ] ; final Rect displayFrame = new Rect ( ) ; view . getLocationOnScreen ( screenPos ) ; view . getWindowVisibleDisplayFrame ( displayFrame ) ; final int width = view . getWidth ( ) ; final int height = view . getHeight ( ) ; final int midy = screenPos [ 1 ] + ( height / 2 ) ; final int screenWidth = context . getResources ( ) . getDisplayMetrics ( ) . widthPixels ; Toast cheatSheet = Toast . makeText ( context , title , Toast . LENGTH_SHORT ) ; if ( midy < displayFrame . height ( ) ) { // Show along the top; follow action buttons cheatSheet . setGravity ( Gravity . TOP | Gravity . RIGHT , screenWidth - screenPos [ 0 ] - ( width / 2 ) , height ) ; } else { // Show along the bottom center cheatSheet . setGravity ( Gravity . BOTTOM | Gravity . CENTER_HORIZONTAL , 0 , height ) ; } cheatSheet . show ( ) ; }
Slightly modified code from Jake Wharton s ABS