70 likes | 270 Views
Android Drawing Units and The AlertDialog Builder. Drawing Units. There are six basic units to use when identifying distance on the Android Screen: px, in, mm, pt, dp, and sp px - Pixels - corresponds to actual pixels on the screen. in - Inches - based on the physical size of the screen.
E N D
Drawing Units • There are six basic units to use when identifying distance on the Android Screen: px, in, mm, pt, dp, and sp • px - Pixels - corresponds to actual pixels on the screen. • in - Inches - based on the physical size of the screen. • mm - Millimeters - based on the physical size of the screen. • pt - Points - 1/72 of an inch based on the physical size of the screen.
Drawing Units • dp - Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp". • sp - Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen densityand user's preference.
AlertDialog Builder • AlertDialog is a class that gives you a fast way to build dialogs with or without your own custom layout. • You can use the AlertDialog standard layout, which includes a Title, a Message and up to three Buttons: one Positive, one Negative and one Neutral. When you use this approach you just set the Title, and then set the Message, and give the buttons an onClick method to tell them what to do. Although three buttons are available, you don’t need to use all three of the buttons. Only the buttons you set up will be seen. • You can also use the AlertDialong with a custom layout of your own making, and in that case you must instantiate all of your layouts components as usual, and in addition to this, you must Inflate your layout using the LayoutInflater class. To Inflate a layout is the Android term for instantiating a View object from the layout.
Standard AlertDialog Builder Layout AlertDialog.Builder qBuilder = new AlertDialog.Builder(this); qBuilder.setTitle(R.string.titlestring); qBuilder.setMessage(R.string.messagestring); qBuilder.setPositiveButton(R.string.yes,new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // positive actions } }); qBuilder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // negative actions } }); qBuilder.setNeutralButton(R.string.cancel,new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // neutral actions } }); AlertDialog alertDialog = qBuilder.create(); alertDialog.show();
Creating a Custom AlertDialog From a Previously Defined Layout LayoutInflater li = LayoutInflater.from(getContext()); View layoutView = li.inflate(R.layout.layoutname, null); // using layountname’s layout AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext()); alertDialogBuilder.setView(layoutView).setCancelable(false); // will act like modal dialog // Then set up the rest of the layouts fields as usual usernameText = (EditText) layoutView.findViewById(R.id.UserName); btnOK = (Button) layoutView.findViewById(R.id.okButton); (etc.) // Then create the alert and show the alert alertDialog = alertDialogBuilder.create(); alertDialog.show();