1 / 28

Styles, Dialog Boxes, and Menus

Styles, Dialog Boxes, and Menus. Styles. Styles. Allow creation of a common format placed in res/values/styles.xml file name is incidental Can be applied to multiple widgets, layouts, etc. When applied to layout, contained widgets are not affected

forest
Download Presentation

Styles, Dialog Boxes, and Menus

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. Styles, Dialog Boxes, and Menus

  2. Styles

  3. Styles • Allow creation of a common format • placed in res/values/styles.xml • file name is incidental • Can be applied to multiple widgets, layouts, etc. • When applied to layout, contained widgets are not affected • If applied to different kinds of widgets, attributes that do not apply are simply ignored • One style can inherit another • attributes can be overridden • Can be used to set a theme at the application or activity level

  4. Example In styles.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <style name=“myStyle"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColorHint">#FF0000</item> </style> </resources> In Activity’s corresponding .xml file: <TextView style="@style/myStyle" android:text="I am using a style template" android:id="@+id/tvStyling " />

  5. Example of Inheritance Added to styles.xml: <style name="myInheritingStyle" parent="myStyle"> <item name="android:textColorHint">#0000FF</item> <item name="android:layout_marginTop">20dp</item> </style> In Activity’s corresponding .xml file: <TextView style="@style/myInheritingStyle" android:text="I am using an inheriting style template" android:id="@+id/tvInheriting " />

  6. Example of a theme In styles.xml: <style name="myCustomTheme" parent="android:Theme.Light"> <item name="android:colorBackground">#0F0F0F</item> </style> In Manifest file (as an attribute of either the Activity or Application): android:theme="myCustomTheme"

  7. Dialog Boxes

  8. Dialog boxes • Presents information to the screen • error message • confirmation of a process • other • Overlays another Activity • usually translucent and modal

  9. Dialog Boxes • Can be created in XML or programmatically • if created in XML: • use an Activity with the Dialog theme • onPause and onResume of parent fired off • advantage: can be as robust as needed • disadvantage: relatively unwieldy to pass information back from Dialog • if created programmatically • use AlertDialog class • no focus events fired off • advantage: low overhead and access to all class entities • disadvantage: interface is limited

  10. Creating a DialogBox via xml

  11. Creating DialogBox via xml • Create an Activity with DialogBox theme • In Manifest file: <activity android:name=".Dialog" android:label="Data Entry Error" android:theme="@android:style/Theme.Dialog"> </activity>

  12. Creating DialogBox via xml • Component is technically an Activity, and all requirements remain the same • Manifest file entry, .xml file, and .java file • launched via the startActivity() method in parent Activity • intent must be created • Dialog Activity presented modally ‘in front’ of parent activity • Using an Activity with the Dialog theme allows for added functionality

  13. Creating DialogBox via xml • openXMLDialog below is an example of a method within the main Activity public void openXMLDialog () { Intent i = new Intent(this, XMLDialog.class); startActivity(i); }

  14. Creating a DialogBox programmatically

  15. Creating DialogBox programmatically • Component is created within the current Activity • No Manifest file entry, .xml file, or .java file • launched via the show() method in AlertDialog class (from a method in the parent Activity) • Dialog Activity presented modally ‘in front’ of parent activity • setCancelable method determines if user can cancel the Dialog via the back button on the device • Minimal effort, minimal functionality

  16. Creating DialogBox programmatically • Can use one, two, or three buttons • all buttons automatically dismiss the dialog after corresponding code is executed • buttons appear right to left in following order • positive • neutral • negative • No difference between the three • If none are chosen, program will be stuck with the modal dialog box • no build errors

  17. Creating DialogBox programmatically public void inputError() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setCancelable(false); builder.setTitle("Data entry error"); builder.setMessage("Please enter a positive number in the number field"); builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //Functionality goes here } }); ((EditText)findViewById(R.id.etNumField)).requestFocus(); AlertDialog alert = builder.create(); alert.show(); }

  18. Menus

  19. Menus • Can be created in xml or programmatically • If created in .xml, placed in res\menu\ folder • Menu displayed by means of menu key on phone • Must override the following methods in Activity • onCreateOptionsMenu • creates the menu • ‘inflates’ xml code or programmatically creates menu • onOptionsItemSelected • provide the functionality to the menu

  20. Creating a menu via xml

  21. Creating a menu using xml • This xml file would reside in the res\menu folder <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/mi1" android:title="First" android:orderInCategory="1“ android:showAsAction="never" /> <item android:id="@+id/mi2" android:title="Second" android:orderInCategory="2“ android:showAsAction="never" /> </menu>

  22. Creating a menu using xml • onCreateOptionsMenu() overridden in the corresponding Activity • called only once – first time menu is displayed • onPrepareOptionsMenu is called every time menu is displayed • here ‘choices’ reflects the name of the menu xml file • a return value of true allows the menu to be shown @Override public booleanonCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.choices, menu); return true; }

  23. onCreateOptionsMenu • return value • true • menu can displayed • false • menu cannot display • Scenario • When Activity is displayed, may want the Menu available under certain conditions and not otherwise if (condition met) return true; else return false;

  24. Creating a menu using xml • onOptionsItemSelected() overridden in the corresponding Activity • Here ‘miX’ reflects the name of the menu item in the xml file @Override public booleanonOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case (R.id.mi1): //Functionality here case (R.id.mi2): //Functionality here } return true; }

  25. onOptionsMenuItem • return value • true • consume menu processing here • will not call super class’ onOptionsMenuItem • false • will call super class’ onOptionsMenuItem • Scenario • May have one activity that implements the menu, and others that subclass it • if subclass handles subset of cases, handle those and return false in any case that is not handled

  26. Creating a menu programmatically

  27. Creating a menu programmatically • onCreateOptionsMenu() overridden in the corresponding Activity • Each menu item added via the ‘add’ method in the Menu class • parameters are: • group id, item id, order, title • group id useful if actions need to be taken on a group of items • removeGroup(), setGroupVisible(), etc. • item id is the unique id (value returned by getItemId()) • items will appear in numerical order by order parameter public booleanonCreateOptionsMenu(Menu menu) { menu.add(Menu.NONE, 1, 1, "First"); menu.add(Menu.NONE, 2, 2, "Second"); return true; }

  28. Creating a menu programmatically • onOptionsItemSelected() overridden in the corresponding Activity • getItemId() matches those set in onCreateOptionsMenu() @Override public booleanonOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case 1: //Functionality here case 2: //Functionality here } return true; }

More Related