SlideShare una empresa de Scribd logo
1 de 44
Android Programming




           Lesson 2
Basic Widgets and Containers

         NGUYEN The Linh
Android Programming


Contents

     1     Using XML-Based Layouts

     2     Employing Basic Widgets

     3     Working with Containers


     4     Exercise 2




                         2
Android Programming


Basic Widgets and Containers




       Using XML-Based Layouts




                 3
Android Programming


Using XML-Based Layouts

                           What is an
                           XML-Based
                            Layout?




                   4
Android Programming


Using XML-Based Layouts

 Why Use XML-Based Layouts?
   Most everything you do using XML layout files can be achieved
    through Java code.
   To assist in the creation of tools for view definition, such as a GUI
    builder in an IDE like Eclipse or a dedicated Android GUI designer
    like DroidDraw.
   XML forms a nice middle ground between something that is easy
    for tool-writers to use and easy for programmers to work with by
    hand as needed.
   XML as a GUI definition format is becoming more commonplace.

                                  5
Android Programming


Using XML-Based Layouts

                           What Does
                            it Look
                             Like?




                   6
Android Programming


Using XML-Based Layouts

 What Does It Look Like?
   The root element needs to declare the Android XML namespace:
       • xmlns:android="http://schemas.android.com/apk/res/android"
    All other elements will be children of the root and will inherit that
     namespace declaration.
    Because we want to reference this button from our Java code, we
     need to give it an identifier via the android:id attribute.
    android:layout_width and android:layout_height tell Android to
     have the button’s width and height fill the “parent”



                                      7
Android Programming


Using XML-Based Layouts

 We Attach These to the Java . . . How?
   All you need is one statement in your activity’s onCreate()
    callback to use that layout:
       • setContentView(R.layout.main)
    All of the layouts are accessible under R.layout, keyed by the
     base name of the layout file—main.xml results in
     R.layout.main.
    To access our identified widgets, use findViewById().
    That numeric identifier was generated by Android in the R class
     as R.id.something (where something is the specific widget you
     are seeking).
                                     8
Android Programming


Basic Widgets and Containers




       Employing Basic Widgets




                 9
Android Programming


Employing Basic Widgets

 TextView
    Text not editable directly by users. Typically, they are used to
     identify adjacent widgets.
    Properties of relevance for TextView
        • android:text to set the value of the label itself.
        • android:typeface to set the typeface to use for the label (e.g., monospace)
        • android:textStyle to indicate that the typeface should be made bold (bold),
          italic (italic), or bold and italic (bold italic).
        • android:textColor to set the color of the label’s text, in RGB hex format
          (e.g., #FF0000 for red).



                                        10
Android Programming


Employing Basic Widgets

 Button
    Button is a subclass of TextView, so everything discussed in the
     preceding section in terms of formatting the face of the button
     still holds.

 Images
    Android has two widgets to help you embed images in your
     activities: ImageView and ImageButton.
    As the names suggest, they are image-based analogues to
     TextView and Button, respectively.

                                 11
Android Programming


Employing Basic Widgets

 Images (cont.)
    Properties of relevance for ImageView and ImageButton
       • android:src to specify what picture to use.
    ImageButton, a subclass of ImageView, mixes in the standard
     Button behaviors, for responding to clicks.

 EditText
    EditText is a subclass of the TextView.




                                       12
Android Programming


Employing Basic Widgets

 EditText (cont.)
    Along with the standard TextView properties (e.g.,
     android:textStyle), EditText has many others that will be useful
     for you in constructing fields, including:
        • android:autoText to control if the field should provide automatic spelling
          assistance
        • android:capitalize to control if the field should automatically capitalize the
          first letter of entered text (e.g., first name, city)
        • android:digits to configure the field to accept only certain digits
        • android:singleLine to control if the field is for single-line input or multiple-
          line input



                                           13
Android Programming


Employing Basic Widgets

 EditText (cont.)
    Beyond those, you can configure fields to use specialized input
     methods, such as android:numeric for numeric-only input,
     android:password for shrouded password input, and
     android:phoneNumber for entering in phone numbers.

 CheckBox
    It has TextView as an ancestor, so you can use TextView
     properties like android:textColor to format the widget.



                                 14
Android Programming


Employing Basic Widgets

 CheckBox (cont.)
    It has TextView as an ancestor, so you can use TextView
     properties like android:textColor to format the widget.
    Within Java, you can invoke:
       • isChecked() to determine if the checkbox has been checked
       • setChecked() to force the checkbox into a checked or unchecked state
       • toggle() to toggle the checkbox as if the user checked it




                                      15
Android Programming


Employing Basic Widgets

 RadioButton
    Like CheckBox, RadioButton inherits from Button, which in
     turn inherits from TextView. Hence, all the standard TextView
     properties for font face, style, color, cont., are available for
     controlling the look of radio buttons.
    Similarly, you can call isChecked() on a RadioButton to see if it
     is selected, toggle() to select it, and so on, like you can with a
     CheckBox.




                                  16
Android Programming


Employing Basic Widgets

 RadioGroup
    The RadioGroup indicates a set of radio buttons whose state is
     tied, meaning only one button out of the group can be selected at
     any time.
    Within Java, you can invoke:
       • check() to check a specific radio button via its ID (e.g.,
         group.check(R.id.radio1))
       • clearCheck() to clear all radio buttons, so none in the group are checked
       • getCheckedRadioButtonId() to get the ID of the currently-checked radio
         button (or -1 if none are checked)



                                       17
Android Programming


Basic Widgets and Containers




       Working with Containers




                 18
Android Programming


Working with Containers

 LinearLayout
    As noted already, LinearLayout is a box model—widgets or
     child containers are lined up in a column or row, one after the
     next.

     Concepts and Properties
        • android:orientation to set the value to be horizontal for a row or vertical for
          a column.
        • android:layout_width: wrap_content or match_parrent
        • android:layout_height: wrap_content or match_parrent
        • android:layout_weight: ?

                                         19
Android Programming


Working with Containers




                    20
Android Programming


Working with Containers




                    21
Android Programming


Working with Containers




                    22
Android Programming


Working with Containers




                    23
Android Programming


Working with Containers




                    24
Android Programming


Working with Containers

 LinearLayout (cont.)
    Concepts and Properties (cont.)
       • android:gravity to set the gravity of the content of the View its used on.
       • android:layout_gravity to set the gravity of the View or Layout in its parent

       • android:padding?
       • android:layout_margin?




                                        25
Android Programming


Working with Containers

                    Notes
        2              1 is android:paddingLeft of A and
                        android:layout_marginLeft of B
                       2 is android:paddingTop of A and
                        android:layout_marginTop of B
       B               3 is android:paddingRight of A and
1              3
                        android:layout_marginRight of B
                       4 is android:paddingBottom of A
                        and android:layout_marginBottom
        4               of B
A

                     26
Android Programming


Working with Containers

 LinearLayout (cont.)
    Example 2.1
      • Example 2.1.xml




                          27
Android Programming


Working with Containers

 RelativeLayout
    RelativeLayout, as the name suggests, lays out widgets based
     upon their relationship to other widgets in the container and in
     the parent container.

     Concepts and Properties
        • android:layout_alignParentTop says the widget’s top should align with the
          top of the container.
        • android:layout_alignParentBottom
        • android:layout_alignParentLeft
        • android:layout_alignParentRight

                                       28
Android Programming


Working with Containers

 RelativeLayout (cont.)
    Concepts and Properties (cont.)
       • android:layout_centerHorizontal says the widget should be positioned
         horizontally at the center of the container.

       • android:layout_centerVertical =

       • android:layout_centerInParent = android:layout_centerHorizontal +
         android:layout_centerVertical




                                     29
Android Programming


Working with Containers

 RelativeLayout (cont.)
    Concepts and Properties (cont.)
       • android:layout_above indicates that the widget should be placed above the
         widget referenced in the property.

       • android:layout_below

       • android:layout_toLeftOf indicates that the widget should be placed to the
         left of the widget referenced in the property.

       • android:layout_toRightOf


                                      30
Android Programming


Working with Containers

 RelativeLayout (cont.)
    Concepts and Properties (cont.)
       • android:layout_alignTop indicates that the widget’s top should be aligned
         with the top of the widget referenced in the property.

       • android:layout_alignBottom
       • android:layout_alignLeft
       • android:layout_alignRight

       • android:layout_alignBaseline indicates that the baselines of the two
         widgets should be aligned.


                                      31
Android Programming


Working with Containers

 RelativeLayout (cont.)
    Example 2.2
      • Example 2.2.xml




                           32
Android Programming


Working with Containers

 TableLayout
    It allows you to position your widgets in a grid to your
     specifications. You control the number of rows and columns,
     which columns might shrink or stretch to accommodate their
     contents, and so on.

    Concepts and Properties
       • android:layout_span indicate the number of columns the widget spans.
       • android:layout_column to put a widget into a different column.




                                     33
Android Programming


Working with Containers

 TableLayout (cont.)
    Concepts and Properties (cont.)
       • android:stretchColumns If marked as stretchable, it can expand in width to
         fit any extra space.

       • android:shrinkColumns If marked as shrinkable, the column width can be
         shrunk to fit the table into its parent object.

       • android:collapseColumns to hide a column.




                                      34
Android Programming


Working with Containers

 TableLayout (cont.)
    Concepts and Properties (cont.)
       • Table 2: value? = “*”
       • Table 3: value? = “0”
       • Table 4: value? = “1”




                                 35
Android Programming


Working with Containers

 TableLayout (cont.)
    Concepts and Properties (cont.)
       • Table 1: removed
       • Table 2: value? = “*”
       • Table 3: value? = “0”




                                 36
Android Programming


Working with Containers

 TableLayout (cont.)
    Example 2.3
      • Example 2.3.xml




                          37
Android Programming


Working with Containers

 GridLayout (Android 4.0 or higher)
    A layout that places its children in a rectangular grid.
    Concepts and Properties
        • android:columnCount, android:rowCount: The maximum number of
          columns/rows to create when automatically positioning children.

        • android:orientation, android:layout_row, android:layout_column

        • android:layout_gravity = center_horizontal, fill_horizontal, fill_vertical

        • android:layout_rowSpan and android:layout_columnSpan


                                          38
Android Programming


Working with Containers

 GridLayout
    Example 2.4
      • Example 2.4.xml




                          39
Android Programming


Working with Containers

 FrameLayout
    http://developer.android.com/reference/android/widget/FrameLay
     out.html




                                40
Android Programming


Working with Containers

 ScrollView
    ScrollView is a container that provides scrolling for its contents.
     You can take a layout that might be too big for some screens.

     User can see only part of your layout at one time; the rest is
      available via scrolling.




                                   41
Android Programming


Exercise 2
                         Writing Tip Calculator
   Now we will create a simple tip calculating application very similar to one
                                   over here:
               http://www.onlineconversion.com/tip_calculator.htm




                                    42
Android Programming


Exercise 2




             43
Android Programming

Más contenido relacionado

Similar a [Android] Basic Widgets and Containers

Basic android development
Basic android developmentBasic android development
Basic android development
Upanya Singh
 
Basic android development
Basic android developmentBasic android development
Basic android development
Upanya Singh
 
Os eclipse-androidwidget-pdf
Os eclipse-androidwidget-pdfOs eclipse-androidwidget-pdf
Os eclipse-androidwidget-pdf
weerabahu
 
Android Introduction
Android IntroductionAndroid Introduction
Android Introduction
aswapnal
 

Similar a [Android] Basic Widgets and Containers (20)

[Android] 2D Graphics
[Android] 2D Graphics[Android] 2D Graphics
[Android] 2D Graphics
 
Basic android development
Basic android developmentBasic android development
Basic android development
 
Basic android development
Basic android developmentBasic android development
Basic android development
 
Android
AndroidAndroid
Android
 
[Android] Intent and Activity
[Android] Intent and Activity[Android] Intent and Activity
[Android] Intent and Activity
 
Android Basic
Android BasicAndroid Basic
Android Basic
 
Android
Android Android
Android
 
Questions About Android Application Development
Questions About Android Application DevelopmentQuestions About Android Application Development
Questions About Android Application Development
 
Xamarin.Android Introduction
Xamarin.Android IntroductionXamarin.Android Introduction
Xamarin.Android Introduction
 
Android overview
Android overviewAndroid overview
Android overview
 
Android Tutorial
Android TutorialAndroid Tutorial
Android Tutorial
 
Android App Development Overview- HKInfoway Technologies.pdf
Android App Development Overview- HKInfoway Technologies.pdfAndroid App Development Overview- HKInfoway Technologies.pdf
Android App Development Overview- HKInfoway Technologies.pdf
 
Marakana Android User Interface
Marakana Android User InterfaceMarakana Android User Interface
Marakana Android User Interface
 
Java Meetup - 12-03-15 - Android Development Workshop
Java Meetup - 12-03-15 - Android Development WorkshopJava Meetup - 12-03-15 - Android Development Workshop
Java Meetup - 12-03-15 - Android Development Workshop
 
What is Android?
What is Android?What is Android?
What is Android?
 
Unit 2 LayoutTutorial.pptx
Unit 2 LayoutTutorial.pptxUnit 2 LayoutTutorial.pptx
Unit 2 LayoutTutorial.pptx
 
20MCE11_MAD_intro.pptx
20MCE11_MAD_intro.pptx20MCE11_MAD_intro.pptx
20MCE11_MAD_intro.pptx
 
Os eclipse-androidwidget-pdf
Os eclipse-androidwidget-pdfOs eclipse-androidwidget-pdf
Os eclipse-androidwidget-pdf
 
Android Introduction
Android IntroductionAndroid Introduction
Android Introduction
 
15 android libraries for app development
15 android libraries for app development15 android libraries for app development
15 android libraries for app development
 

Más de Nikmesoft Ltd

Más de Nikmesoft Ltd (11)

[iOS] Data Storage
[iOS] Data Storage[iOS] Data Storage
[iOS] Data Storage
 
[iOS] Multiple Background Threads
[iOS] Multiple Background Threads[iOS] Multiple Background Threads
[iOS] Multiple Background Threads
 
[iOS] Navigation
[iOS] Navigation[iOS] Navigation
[iOS] Navigation
 
[iOS] Basic UI Elements
[iOS] Basic UI Elements[iOS] Basic UI Elements
[iOS] Basic UI Elements
 
[Android] Multimedia Programming
[Android] Multimedia Programming[Android] Multimedia Programming
[Android] Multimedia Programming
 
[Android] Android Animation
[Android] Android Animation[Android] Android Animation
[Android] Android Animation
 
[Android] Services and Broadcast Receivers
[Android] Services and Broadcast Receivers[Android] Services and Broadcast Receivers
[Android] Services and Broadcast Receivers
 
[Android] Web services
[Android] Web services[Android] Web services
[Android] Web services
 
[Android] Multiple Background Threads
[Android] Multiple Background Threads[Android] Multiple Background Threads
[Android] Multiple Background Threads
 
[Android] Maps, Geocoding and Location-Based Services
[Android] Maps, Geocoding and Location-Based Services[Android] Maps, Geocoding and Location-Based Services
[Android] Maps, Geocoding and Location-Based Services
 
[Android] Widget Event Handling
[Android] Widget Event Handling[Android] Widget Event Handling
[Android] Widget Event Handling
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

[Android] Basic Widgets and Containers

  • 1. Android Programming Lesson 2 Basic Widgets and Containers NGUYEN The Linh
  • 2. Android Programming Contents 1 Using XML-Based Layouts 2 Employing Basic Widgets 3 Working with Containers 4 Exercise 2 2
  • 3. Android Programming Basic Widgets and Containers Using XML-Based Layouts 3
  • 4. Android Programming Using XML-Based Layouts What is an XML-Based Layout? 4
  • 5. Android Programming Using XML-Based Layouts  Why Use XML-Based Layouts?  Most everything you do using XML layout files can be achieved through Java code.  To assist in the creation of tools for view definition, such as a GUI builder in an IDE like Eclipse or a dedicated Android GUI designer like DroidDraw.  XML forms a nice middle ground between something that is easy for tool-writers to use and easy for programmers to work with by hand as needed.  XML as a GUI definition format is becoming more commonplace. 5
  • 6. Android Programming Using XML-Based Layouts What Does it Look Like? 6
  • 7. Android Programming Using XML-Based Layouts  What Does It Look Like?  The root element needs to declare the Android XML namespace: • xmlns:android="http://schemas.android.com/apk/res/android"  All other elements will be children of the root and will inherit that namespace declaration.  Because we want to reference this button from our Java code, we need to give it an identifier via the android:id attribute.  android:layout_width and android:layout_height tell Android to have the button’s width and height fill the “parent” 7
  • 8. Android Programming Using XML-Based Layouts  We Attach These to the Java . . . How?  All you need is one statement in your activity’s onCreate() callback to use that layout: • setContentView(R.layout.main)  All of the layouts are accessible under R.layout, keyed by the base name of the layout file—main.xml results in R.layout.main.  To access our identified widgets, use findViewById().  That numeric identifier was generated by Android in the R class as R.id.something (where something is the specific widget you are seeking). 8
  • 9. Android Programming Basic Widgets and Containers Employing Basic Widgets 9
  • 10. Android Programming Employing Basic Widgets  TextView  Text not editable directly by users. Typically, they are used to identify adjacent widgets.  Properties of relevance for TextView • android:text to set the value of the label itself. • android:typeface to set the typeface to use for the label (e.g., monospace) • android:textStyle to indicate that the typeface should be made bold (bold), italic (italic), or bold and italic (bold italic). • android:textColor to set the color of the label’s text, in RGB hex format (e.g., #FF0000 for red). 10
  • 11. Android Programming Employing Basic Widgets  Button  Button is a subclass of TextView, so everything discussed in the preceding section in terms of formatting the face of the button still holds.  Images  Android has two widgets to help you embed images in your activities: ImageView and ImageButton.  As the names suggest, they are image-based analogues to TextView and Button, respectively. 11
  • 12. Android Programming Employing Basic Widgets  Images (cont.)  Properties of relevance for ImageView and ImageButton • android:src to specify what picture to use.  ImageButton, a subclass of ImageView, mixes in the standard Button behaviors, for responding to clicks.  EditText  EditText is a subclass of the TextView. 12
  • 13. Android Programming Employing Basic Widgets  EditText (cont.)  Along with the standard TextView properties (e.g., android:textStyle), EditText has many others that will be useful for you in constructing fields, including: • android:autoText to control if the field should provide automatic spelling assistance • android:capitalize to control if the field should automatically capitalize the first letter of entered text (e.g., first name, city) • android:digits to configure the field to accept only certain digits • android:singleLine to control if the field is for single-line input or multiple- line input 13
  • 14. Android Programming Employing Basic Widgets  EditText (cont.)  Beyond those, you can configure fields to use specialized input methods, such as android:numeric for numeric-only input, android:password for shrouded password input, and android:phoneNumber for entering in phone numbers.  CheckBox  It has TextView as an ancestor, so you can use TextView properties like android:textColor to format the widget. 14
  • 15. Android Programming Employing Basic Widgets  CheckBox (cont.)  It has TextView as an ancestor, so you can use TextView properties like android:textColor to format the widget.  Within Java, you can invoke: • isChecked() to determine if the checkbox has been checked • setChecked() to force the checkbox into a checked or unchecked state • toggle() to toggle the checkbox as if the user checked it 15
  • 16. Android Programming Employing Basic Widgets  RadioButton  Like CheckBox, RadioButton inherits from Button, which in turn inherits from TextView. Hence, all the standard TextView properties for font face, style, color, cont., are available for controlling the look of radio buttons.  Similarly, you can call isChecked() on a RadioButton to see if it is selected, toggle() to select it, and so on, like you can with a CheckBox. 16
  • 17. Android Programming Employing Basic Widgets  RadioGroup  The RadioGroup indicates a set of radio buttons whose state is tied, meaning only one button out of the group can be selected at any time.  Within Java, you can invoke: • check() to check a specific radio button via its ID (e.g., group.check(R.id.radio1)) • clearCheck() to clear all radio buttons, so none in the group are checked • getCheckedRadioButtonId() to get the ID of the currently-checked radio button (or -1 if none are checked) 17
  • 18. Android Programming Basic Widgets and Containers Working with Containers 18
  • 19. Android Programming Working with Containers  LinearLayout  As noted already, LinearLayout is a box model—widgets or child containers are lined up in a column or row, one after the next.  Concepts and Properties • android:orientation to set the value to be horizontal for a row or vertical for a column. • android:layout_width: wrap_content or match_parrent • android:layout_height: wrap_content or match_parrent • android:layout_weight: ? 19
  • 25. Android Programming Working with Containers  LinearLayout (cont.)  Concepts and Properties (cont.) • android:gravity to set the gravity of the content of the View its used on. • android:layout_gravity to set the gravity of the View or Layout in its parent • android:padding? • android:layout_margin? 25
  • 26. Android Programming Working with Containers  Notes 2  1 is android:paddingLeft of A and android:layout_marginLeft of B  2 is android:paddingTop of A and android:layout_marginTop of B B  3 is android:paddingRight of A and 1 3 android:layout_marginRight of B  4 is android:paddingBottom of A and android:layout_marginBottom 4 of B A 26
  • 27. Android Programming Working with Containers  LinearLayout (cont.)  Example 2.1 • Example 2.1.xml 27
  • 28. Android Programming Working with Containers  RelativeLayout  RelativeLayout, as the name suggests, lays out widgets based upon their relationship to other widgets in the container and in the parent container.  Concepts and Properties • android:layout_alignParentTop says the widget’s top should align with the top of the container. • android:layout_alignParentBottom • android:layout_alignParentLeft • android:layout_alignParentRight 28
  • 29. Android Programming Working with Containers  RelativeLayout (cont.)  Concepts and Properties (cont.) • android:layout_centerHorizontal says the widget should be positioned horizontally at the center of the container. • android:layout_centerVertical = • android:layout_centerInParent = android:layout_centerHorizontal + android:layout_centerVertical 29
  • 30. Android Programming Working with Containers  RelativeLayout (cont.)  Concepts and Properties (cont.) • android:layout_above indicates that the widget should be placed above the widget referenced in the property. • android:layout_below • android:layout_toLeftOf indicates that the widget should be placed to the left of the widget referenced in the property. • android:layout_toRightOf 30
  • 31. Android Programming Working with Containers  RelativeLayout (cont.)  Concepts and Properties (cont.) • android:layout_alignTop indicates that the widget’s top should be aligned with the top of the widget referenced in the property. • android:layout_alignBottom • android:layout_alignLeft • android:layout_alignRight • android:layout_alignBaseline indicates that the baselines of the two widgets should be aligned. 31
  • 32. Android Programming Working with Containers  RelativeLayout (cont.)  Example 2.2 • Example 2.2.xml 32
  • 33. Android Programming Working with Containers  TableLayout  It allows you to position your widgets in a grid to your specifications. You control the number of rows and columns, which columns might shrink or stretch to accommodate their contents, and so on.  Concepts and Properties • android:layout_span indicate the number of columns the widget spans. • android:layout_column to put a widget into a different column. 33
  • 34. Android Programming Working with Containers  TableLayout (cont.)  Concepts and Properties (cont.) • android:stretchColumns If marked as stretchable, it can expand in width to fit any extra space. • android:shrinkColumns If marked as shrinkable, the column width can be shrunk to fit the table into its parent object. • android:collapseColumns to hide a column. 34
  • 35. Android Programming Working with Containers  TableLayout (cont.)  Concepts and Properties (cont.) • Table 2: value? = “*” • Table 3: value? = “0” • Table 4: value? = “1” 35
  • 36. Android Programming Working with Containers  TableLayout (cont.)  Concepts and Properties (cont.) • Table 1: removed • Table 2: value? = “*” • Table 3: value? = “0” 36
  • 37. Android Programming Working with Containers  TableLayout (cont.)  Example 2.3 • Example 2.3.xml 37
  • 38. Android Programming Working with Containers  GridLayout (Android 4.0 or higher)  A layout that places its children in a rectangular grid.  Concepts and Properties • android:columnCount, android:rowCount: The maximum number of columns/rows to create when automatically positioning children. • android:orientation, android:layout_row, android:layout_column • android:layout_gravity = center_horizontal, fill_horizontal, fill_vertical • android:layout_rowSpan and android:layout_columnSpan 38
  • 39. Android Programming Working with Containers  GridLayout  Example 2.4 • Example 2.4.xml 39
  • 40. Android Programming Working with Containers  FrameLayout  http://developer.android.com/reference/android/widget/FrameLay out.html 40
  • 41. Android Programming Working with Containers  ScrollView  ScrollView is a container that provides scrolling for its contents. You can take a layout that might be too big for some screens.  User can see only part of your layout at one time; the rest is available via scrolling. 41
  • 42. Android Programming Exercise 2 Writing Tip Calculator Now we will create a simple tip calculating application very similar to one over here: http://www.onlineconversion.com/tip_calculator.htm 42