Styles dialog boxes and menus
1 / 28

Styles, Dialog Boxes, and Menus - PowerPoint PPT Presentation

  • Uploaded on

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Styles, Dialog Boxes, and Menus' - forest

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript


  • 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


In styles.xml:

<?xml version="1.0" encoding="utf-8"?>


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



In Activity’s corresponding .xml file:



android:text="I am using a style template"

android:id="@+id/tvStyling "


Example of inheritance
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>


In Activity’s corresponding .xml file:



android:text="I am using an inheriting style template"

android:id="@+id/tvInheriting "


Example of a theme
Example of a theme

In styles.xml:

<style name="myCustomTheme" parent="android:Theme.Light">

<item name="android:colorBackground">#0F0F0F</item>


In Manifest file (as an attribute of either the Activity or Application):


Dialog boxes1
Dialog boxes

  • Presents information to the screen

    • error message

    • confirmation of a process

    • other

  • Overlays another Activity

    • usually translucent and modal

Dialog boxes2
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

Creating a dialogbox via xml

Creating a DialogBox via xml

Creating dialogbox via xml
Creating DialogBox via xml

  • Create an Activity with DialogBox theme

    • In Manifest file:



      android:label="Data Entry Error"



Creating dialogbox via xml1
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

Creating dialogbox via xml2
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);



Creating a dialogbox programmatically

Creating a DialogBox programmatically

Creating dialogbox programmatically
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

Creating dialogbox programmatically1
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

Creating dialogbox programmatically2
Creating DialogBox programmatically

public void inputError() {

AlertDialog.Builder builder = new AlertDialog.Builder(this);


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




AlertDialog alert = builder.create();;



  • 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

Creating a menu using xml
Creating a menu using xml

  • This xml file would reside in the res\menu folder

    <menu xmlns:android="">














Creating a menu using xml1
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


      public booleanonCreateOptionsMenu(Menu menu) {

      getMenuInflater().inflate(, menu);

      return true;



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


      return false;

Creating a menu using xml2
Creating a menu using xml

  • onOptionsItemSelected() overridden in the corresponding Activity

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


      public booleanonOptionsItemSelected(MenuItem item) {

      switch(item.getItemId()) {

      case (

      //Functionality here

      case (

      //Functionality here


      return true;



  • 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

Creating a menu programmatically1
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;


Creating a menu programmatically2
Creating a menu programmatically

  • onOptionsItemSelected() overridden in the corresponding Activity

    • getItemId() matches those set in onCreateOptionsMenu()


      public booleanonOptionsItemSelected(MenuItem item) {

      switch(item.getItemId()) {

      case 1:

      //Functionality here

      case 2:

      //Functionality here


      return true;