styles, dialog boxes, and menus

28
Styles, Dialog Boxes, and Menus

Upload: forest

Post on 24-Feb-2016

91 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Styles, Dialog Boxes, and Menus

Styles, Dialog Boxes, and Menus

Page 2: Styles, Dialog Boxes, and Menus

Styles

Page 3: Styles, Dialog Boxes, and Menus

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

Page 4: Styles, Dialog Boxes, and Menus

ExampleIn 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 "/>

Page 5: Styles, Dialog Boxes, and Menus

Example of InheritanceAdded 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 "/>

Page 6: Styles, Dialog Boxes, and Menus

Example of a themeIn 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"

Page 7: Styles, Dialog Boxes, and Menus

Dialog Boxes

Page 8: Styles, Dialog Boxes, and Menus

Dialog boxes

• Presents information to the screen– error message– confirmation of a process– other

• Overlays another Activity– usually translucent and modal

Page 9: Styles, Dialog Boxes, and Menus

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

Page 10: Styles, Dialog Boxes, and Menus

Creating a DialogBox via xml

Page 11: Styles, Dialog Boxes, and Menus

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>

Page 12: Styles, Dialog Boxes, and Menus

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

Page 13: Styles, Dialog Boxes, and Menus

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);

}

Page 14: Styles, Dialog Boxes, and Menus

Creating a DialogBox programmatically

Page 15: Styles, Dialog Boxes, and Menus

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

Page 16: Styles, Dialog Boxes, and Menus

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

Page 17: Styles, Dialog Boxes, and Menus

Creating DialogBox programmaticallypublic 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();}

Page 18: Styles, Dialog Boxes, and Menus

Menus

Page 19: Styles, Dialog Boxes, and Menus

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

Page 20: Styles, Dialog Boxes, and Menus

Creating a menu via xml

Page 21: Styles, Dialog Boxes, and Menus

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>

Page 22: Styles, Dialog Boxes, and Menus

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

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.choices, menu);

return true;}

Page 23: Styles, Dialog Boxes, and Menus

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 otherwiseif (condition met) return true;else return false;

Page 24: Styles, Dialog Boxes, and Menus

Creating a menu using xml• onOptionsItemSelected() overridden in the corresponding Activity

– Here ‘miX’ reflects the name of the menu item in the xml file

@Overridepublic boolean onOptionsItemSelected(MenuItem item) {

switch(item.getItemId()) {case (R.id.mi1)://Functionality here

case (R.id.mi2)://Functionality here}

return true;}

Page 25: Styles, Dialog Boxes, and Menus

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

Page 26: Styles, Dialog Boxes, and Menus

Creating a menu programmatically

Page 27: Styles, Dialog Boxes, and Menus

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 boolean onCreateOptionsMenu(Menu menu) {menu.add(Menu.NONE, 1, 1, "First");menu.add(Menu.NONE, 2, 2, "Second");

return true;}

Page 28: Styles, Dialog Boxes, and Menus

Creating a menu programmatically• onOptionsItemSelected() overridden in the corresponding Activity

– getItemId() matches those set in onCreateOptionsMenu()

@Overridepublic boolean onOptionsItemSelected(MenuItem item) {

switch(item.getItemId()) {case 1://Functionality here

case 2://Functionality here}

return true;}