Android GUI Design and Event Handling with TableLayout, SeekBar, and EditText

 
T
i
p
C
a
l
c
u
l
a
t
o
r
 
A
p
p
 
(
C
h
a
p
t
e
r
 
4
 
o
f
A
n
d
r
o
i
d
 
h
o
w
 
t
o
 
P
r
o
g
r
a
m
 
b
o
o
k
)
 
O
b
j
e
c
t
i
v
e
s
Design a GUI using a 
TableLayout
.
Use the ADT Plugin’s Outline window in Eclipse
to add GUI components to a 
TableLayout
.
Directly edit the XML of a GUI layout to customize properties
that are not available through the Visual Layout Editor
and Properties window in Eclipse.
Use 
TextView
, 
EditText
 and 
SeekBar
 GUI components.
Use event handling to respond to user interactions with an
EditText
 and a 
SeekBar
.
 
Refer to 
TipCalculatorApp
 android project
 
T
i
p
C
a
l
c
u
l
a
t
o
r
 
A
p
p
 
(
C
o
n
t
i
n
u
e
d
)
 
S
e
e
k
B
a
r
 
A SeekBar
presents a draggable thumb
The user can touch the thumb and drag left or right to set
Current progress level
 
provides a
setOnSeekBarChangeListener(OnSeekBarChangeListener)
sets listener to receive notification of changes to progress level
 
 
E
d
i
t
 
T
e
x
t
 
C
o
m
p
o
n
e
n
t
:
T
e
x
t
 
W
a
t
c
h
e
r
 
Edit Text component provides
addTextChangeListener(TextWatcher) 
method
That adds a text watcher whose methods are called
Whenever the content of the edit text is changed
 
TextWatcher
The methods of this interface are called when text is changed
onTextChanged(CharSequence s,int start,int
before,int count)
Notifies that within 
s
, 
count
 characters beginning at 
start
 
Just replaced old text that had length 
before
 
I
n
t
r
o
d
u
c
i
n
g
 
t
h
e
 
A
p
p
l
i
c
a
t
i
o
n
M
a
n
i
f
e
s
t
 
F
i
l
e
 
 
E
x
p
a
n
d
i
n
g
 
y
o
u
r
 
E
x
p
e
r
i
e
n
c
e
:
L
e
t
 
u
s
 
M
a
k
e
 
a
 
T
o
a
s
t
!
 
Toast
Is a transient notification visible for a few seconds
Is perfect to inform users of events
Has a 
makeText
 method
Enabling you to create a standard Toast display window
Displaying a Toast:
 
 
 
 
Refer to 
makeToastApp
 android project
 
A
d
a
p
t
i
n
g
 
t
o
 
S
c
r
e
e
n
 
O
r
i
e
n
t
a
t
i
o
n
 
2 possible orientations, namely
Portrait and Landscape
 
When orientation is changed
Activity redraws its content
 
In landscape mode
Empty space on the right
Should be used wisely
 
Views at the bottom
May be hidden
 
T
e
c
h
n
i
q
u
e
s
 
f
o
r
 
H
a
n
d
l
i
n
g
O
r
i
e
n
t
a
t
i
o
n
 
C
h
a
n
g
e
s
 
2 techniques are available
Anchoring
Anchor views to edges of the screen
 
Can be done by means of RelativeLayouts
 
Resizing and repositioning
Resize every view as a function of the current orientation
 
Refer to 
AlternativeLayouts
 Android project
 
A
n
c
h
o
r
i
n
g
v
i
e
w
s
 
RelativeLayout
Does the trick
 
On orientation change
Buttons remain
Anchored to screen edges
 
O
u
t
p
u
t
 
R
e
s
i
z
i
n
g
 
a
n
d
 
P
o
s
i
t
i
o
n
i
n
g
 
Idea
Create a different layout for each mode
 
By creating a new subfolder under 
res
Named 
layout-land
 
This way, there will be
main.xml
 contained in layout
Defining UI for portrait mode
 
main.xm
l in layout-land
Handling UI in landscape mode
 
E
x
a
m
p
l
e
:
 
l
a
y
o
u
t
 
E
x
a
m
p
l
e
:
 
l
a
y
o
u
t
-
l
a
n
d
 
O
u
t
p
u
t
 
D
e
t
e
c
t
i
n
g
 
O
r
i
e
n
t
a
t
i
o
n
 
C
h
a
n
g
e
s
 
Device’s current orientation
Can be detected during runtime as follows:
 
C
o
n
t
r
o
l
l
i
n
g
 
O
r
i
e
n
t
a
t
i
o
n
 
o
f
 
A
n
A
c
t
i
v
i
t
y
 
A change in orientation
Can be forced programmatically as follows
 
A
l
t
e
r
n
a
t
i
v
e
 
M
e
t
h
o
d
 
f
o
r
C
o
n
t
r
o
l
l
i
n
g
 
O
r
i
e
n
t
a
t
i
o
n
 
Alternatively, orientation can be forced as follows
 
I
m
p
l
i
c
a
t
i
o
n
 
o
f
 
O
r
i
e
n
t
a
t
i
o
n
C
h
a
n
g
e
s
 
Problem
Changing screen orientation destroys activity and recreates it
On recreation, current state of activity could be lost
 
=> Need to preserve the state of an activity
 
Fixes
Implement the 
onSaveInstance()
 method
Implement 
onRetainNonConfigurationInstance()
 
Refer to 
PreservingStateApp
 Android project
 
F
i
x
#
1
:
 
o
n
S
a
v
e
I
n
s
t
a
n
c
e
S
t
a
t
e
 
Idea
Preserve state and restore it later
Use the 
Bundle
 object to save current state:
 
 
 
Upon recreation, retrieve state saved previously:
 
 
 
 
Limitation
Not adequate for recovering complex data structures
 
F
i
x
#
2
:
o
n
R
e
t
a
i
n
N
o
n
C
o
n
f
i
g
u
r
a
t
i
o
n
I
n
s
t
a
n
c
e
(
)
 
This method is fired
When an activity is about to be destroyed
So, save your data by returning it in the method
 
 
 
Then, extract the saved data
Slide Note
Embed
Share

Learn how to design a GUI using TableLayout in Android, customize properties through XML, utilize TextView, EditText, and SeekBar components, and handle user interactions with EditText and SeekBar. Explore the application manifest file, creating Toast notifications, adapting to screen orientation changes, and techniques for handling orientation changes effectively.

  • Android
  • GUI Design
  • Event Handling
  • TableLayout
  • SeekBar

Uploaded on Sep 25, 2024 | 3 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. TipCalculator App (Chapter 4 of Android how to Program book) Objectives Design a GUI using a TableLayout. Use the ADT Plugin s Outline window in Eclipse to add GUI components to a TableLayout. Directly edit the XML of a GUI layout to customize properties that are not available through the Visual Layout Editor and Properties window in Eclipse. Use TextView, EditText and SeekBar GUI components. Use event handling to respond to user interactions with an EditText and a SeekBar. Refer to TipCalculatorApp android project

  2. TipCalculator App (Continued)

  3. SeekBar A SeekBar presents a draggable thumb The user can touch the thumb and drag left or right to set Current progress level provides a setOnSeekBarChangeListener(OnSeekBarChangeListener) sets listener to receive notification of changes to progress level

  4. Edit Text Component: Text Watcher Edit Text component provides addTextChangeListener(TextWatcher) method That adds a text watcher whose methods are called Whenever the content of the edit text is changed TextWatcher The methods of this interface are called when text is changed onTextChanged(CharSequence s,int start,int before,int count) Notifies that within s, count characters beginning at start Just replaced old text that had length before

  5. Introducing the Application Manifest File

  6. Expanding your Experience: Let us Make a Toast! Toast Is a transient notification visible for a few seconds Is perfect to inform users of events Has a makeText method Enabling you to create a standard Toast display window Displaying a Toast: Refer to makeToastApp android project

  7. Adapting to Screen Orientation 2 possible orientations, namely Portrait and Landscape When orientation is changed Activity redraws its content In landscape mode Empty space on the right Should be used wisely Views at the bottom May be hidden

  8. Techniques for Handling Orientation Changes 2 techniques are available Anchoring Anchor views to edges of the screen Can be done by means of RelativeLayouts Resizing and repositioning Resize every view as a function of the current orientation Refer to AlternativeLayouts Android project

  9. Anchoring views RelativeLayout Does the trick On orientation change Buttons remain Anchored to screen edges

  10. Output

  11. Resizing and Positioning Idea Create a different layout for each mode By creating a new subfolder under res Named layout-land This way, there will be main.xml contained in layout Defining UI for portrait mode main.xml in layout-land Handling UI in landscape mode

  12. Example: layout

  13. Example: layout-land

  14. Output

  15. Detecting Orientation Changes Device s current orientation Can be detected during runtime as follows:

  16. Controlling Orientation of An Activity A change in orientation Can be forced programmatically as follows

  17. Alternative Method for Controlling Orientation Alternatively, orientation can be forced as follows

  18. Implication of Orientation Changes Problem Changing screen orientation destroys activity and recreates it On recreation, current state of activity could be lost => Need to preserve the state of an activity Fixes Implement the onSaveInstance() method Implement onRetainNonConfigurationInstance() Refer to PreservingStateApp Android project

  19. Fix#1: onSaveInstanceState Idea Preserve state and restore it later Use the Bundle object to save current state: Upon recreation, retrieve state saved previously: Limitation Not adequate for recovering complex data structures

  20. Fix#2: onRetainNonConfigurationInstance() This method is fired When an activity is about to be destroyed So, save your data by returning it in the method Then, extract the saved data

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#