1 / 6

Android Drawing Units and The AlertDialog Builder

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.

madge
Download Presentation

Android Drawing Units and The AlertDialog Builder

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android Drawing UnitsandThe AlertDialog Builder

  2. 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.

  3. 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.

  4. 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.

  5. 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();

  6. 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();

More Related