SlideShare a Scribd company logo
1 of 33
Download to read offline
User Interface Development
CSE 5236: Mobile Application Development
Instructor: Adam C. Champion, Ph.D.
Course Coordinator: Dr. Rajiv Ramnath
Reading: Big Nerd Ranch Guide, Chapter 7 (Fragments)
1
Outline
• UI Support in Android
• Fragments
2
UI Support in the Android SDK
• “Inverted” paradigm
– Each subclass constrains functionality (rather
than extend it)
– Hundreds of methods are exposed: L
• Base classes:
– ViewGroup base class for composite UI
elements
– View base class for terminal UI components
3
View Hierarchy
4
# = SDK Version number
ViewGroup Hierarchy
• Direct Subclasses:
– AbsoluteLayout
– AdapterView<T extends Adapter>
– FragmentBreadCrumbs
– FrameLayout
– GridLayout
– LinearLayout
– PagerTitleStrip
– RelativeLayout
– SlidingDrawer
– ViewPager
• 19 indirect subclasses. See:
– http://developer.android.com/reference/android/view/ViewGroup.html
5
Sample Layout, Login Activity
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android”
android:background="@color/background"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dip">
<LinearLayout android:orientation="vertical
...
<TextView android:text="@string/login_title” ... />
<TextView ... />
<EditText android:id="@+id/username_text” ... />
<TextView ... />
<EditText ... />
<Button ... />
<Button ... />
<Button ... />
</LinearLayout>
</ScrollView>
Layout Configuration
• ID: android:id="@+id/username_text” : used for widget handle
• Parameters:
– layout_width, layout_height
– layout_marginTop / ...marginRight, layout_margin
– orientation
• Can be combined: layout_gravity=“bottom|right”
• Constants: match_parent, wrap_content
• Width and height specs: dp, sp (display pixels, scaled pixels)
• A wide range of LayoutParams
• Resources (e.g. backgrounds):
android:background=“@drawable/backdrop”
• Blank canvases using a (generic) View element in the layout 7
8
Adding Resources
9
Resources can also
be added by right-
clicking on res
directory, selecting
New→Android
Resource File
Other Layout Parameters, Techniques
• Inherit parameters from enclosing elements
• layout_span spans multiple columns
• Empty views to add blank canvases (filled later)
• Shrink or stretch columns as needed
(shrinkColumns, stretchColumns)
• RelativeLayout lets window manager handle size
• ConstraintLayout specifies widget constraints for
flexible widget rendering
10
Linking a UI to a Fragment: Java
// LoginActivity.java
public class LoginActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() { return new LoginFragment(); }
}
// LoginFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_login, container, false);
// Real code handles view rotation
mUsernameEditText = (EditText) v.findViewById(R.id.username_text);
mPasswordEditText = (EditText) v.findViewById(R.id.password_text);
// Setup listener objects for buttons
return v;
}
11
Also can set up listener objects in Activities with onCreate()
Linking a UI to a Fragment: Kotlin
// LoginActivity.kt
class LoginActivity : SingleFragmentActivity() {
override fun createFragment(): Fragment { return LoginFragment() }
}
// LoginFragment.kt
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val v: View = inflater.inflate(R.layout.fragment_login, container, false)
// Real code handles view rotation
mUsernameEditText = v.findViewById<EditText>(R.id.username_text)
mPasswordEditText = v.findViewById<EditText>(R.id.password_text)
// Set up listener objects for Buttons
return v
}
12
Creating a Custom Widget: Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#676767"
android:gravity="center_horizontal"
android:padding="20dip">
<com.wiley.fordummies.androidsdk.tictactoe.Board
android:id="@+id/board"
android:layout_width="match_parent"
android:layout_height="280dip"/>
...
</LinearLayout>
13
Creating a Custom Widget: Java
// Board.java
public class Board extends View {
public Board(Context context, AttributeSet attributes) {
super(context, attributes); // . . .
setFocusable(true); setFocusableInTouchMode(true); // . . .
}
// . . .
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh); // . . .
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); // . . .
}
}
14
Instantiating the view:
// LoginFragment.java: onCreateView() and setupBoard()
// onCreateView()
v = inflater.inflate(R.layout.fragment_game_session, . . .);
// setupBoard()
mBoard = (Board) v.findViewById(R.id.board);
Creating a Custom Widget: Kotlin
// Board.kt
class Board(context: Context, attributes: AttributeSet) : View(context, attributes) {
init {
isFocusable = true
isFocusableInTouchMode = true // . . .
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh) // . . .
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) // . . .
}
}
15
Instantiating the view:
// LoginFragment.kt, onCreateView() and setupBoard()
// onCreateView()
v = inflater.inflate(R.layout.fragment_game_session, container, false)
// setupBoard()
mBoard = (Board) v.findViewById(R.id.board)
Creating a Layout via Code
// DohActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
Button dohButton = new Button(this);
dohButton.setText(“In case of meltdown, Push Me!”);
layout.addView(dohButton);
setContentView(layout);
}
16
Styles and Themes (1)
Use:
<EditText
style=”@style/DarkBold”
android:text=”Hello” />
Definition:
<?xml version=”1.0” encoding=”utf-8”?>
<resources>
<style name=“DarkBold”>
<item name=”android:layout_width”>
match_parent
</item>
<item name=“android:layout_height”>
wrap_content
</item>
... more parameters ...
</style>
</resources> 17
Inheritance:
<style name=“DarkPhone” parent=”@style/DarkBold”>
<item name=”android:phoneNumber”>true</item>
</style>
Styles and Themes (2)
• Stored in res/values/ directory with .xml
extension (name not relevant)
• Can set application-wide and activity-specific styles
(aka themes):
– Set themes in AndroidManifest.xml on <application>
tag or <Activity> tag
<application android:theme="@style/CustomTheme">
<activity android:theme="@android:style/Theme.Translucent">
• Can even create version-specific layout files
• Ref: http://developer.android.com/guide/topics/
ui/themes.html
18
Outline
• UI Support in Android
• Fragments
19
Fragments and Their Rationale
• Fragment: Composite UI component that handles its
own UI
• One, multiple Fragments in an Activity
• Separate class hierarchy: Fragment, DialogFragment,
ListFragment, PreferenceFragment, etc.
• Goals:
– Separate UI design from Activity design
– UI should have its own lifecycle and flow
– Dynamically add/remove UI components in running activity
• Drivers: heterogeneous devices, user experience
20
Example – Login and Account
Portrait Layout: Login
22
<?xml version="1.0" encoding="utf-8"?>
<ScrollView ...>
<LinearLayout...>
<TextView android:text="@string/login_title".../>
<TextView android:text="@string/enter_username”.../>
<EditText android:id="@+id/username_text”.../>
<TextView android:text="Enter Password".../>
<EditText android:id="@+id/password_text".../>
<Button android:id="@+id/login_button".../>
<Button android:id="@+id/cancel_button”.../>
<Button android:id="@+id/new_user_button”.../>
</LinearLayout>
</ScrollView>
Portrait Layout: Account
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout ...>
<FrameLayout .../> ⟸
⟸ Placeholder for fragment
<Button android:id="@+id/exit_button”.../>
</LinearLayout>
What happened to landscape layout of the Fragment?
23
Landscape Layout: Login
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout...
android:orientation="horizontal” ...> ⟸
⟸ Note: horizontal layout
<ScrollView ... >
<LinearLayout .. >
<TextView ... />
<TextView ... />
<!-- . . . -->
</LinearLayout>
</ScrollView>
<fragment class="com.wiley.fordummies.androidsdk.tictactoe.AccountFragment"
android:id="@+id/titles"
android:layout_height="match_parent” . . ./>
</LinearLayout>
24
AccountFragment: Portrait Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout ...>
<LinearLayout...>
<TextView android:text="New Account" .../>
<TextView android:text="Username”.../>
<EditText android:id="@+id/username”.../>
<TextView android:text="Password”.../>
<EditText android:id="@+id/password”.../>
<TextView android:text="Confirm Password" .../>
<EditText android:id="@+id/password_confirm”.../>
<Button android:id="@+id/cancel_button” "/>
<Button android:id="@+id/done_button”.../>
</LinearLayout>
</LinearLayout>
25
AccountFragment: Landscape
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout...>
<LinearLayout...>
<TextView android:text="New Account”.../>
<TextView android:text="Username”.../>
<EditText android:id="@+id/username”...”/>
<TextView android:text="Password" .../>
<EditText android:id="@+id/password”.../>
<TextView android:text="Confirm Password" .../>
<EditText android:id="@+id/password_confirm”.../>
<LinearLayout ... >
<Button android:id="@+id/cancel_button”.../>
<Button android:id="@+id/done_button”.../>
</LinearLayout>
</LinearLayout>
</LinearLayout>
26
LoginFragment: Java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v;
int rotation = getActivity().getWindowManager().getDefaultDisplay()
.getRotation();
if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
v = inflater.inflate(R.layout.fragment_login_land, container, false);
} else {
v = inflater.inflate(R.layout.fragment_login, container, false);
}
mUsernameEditText = (EditText) v.findViewById(R.id.username_text);
mPasswordEditText = (EditText) v.findViewById(R.id.password_text);
// Set up OnClickListeners for buttons
return v;
}
27
LoginFragment: Kotlin
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val v: View
val rotation = activity.windowManager.defaultDisplay.rotation
if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
v = inflater.inflate(R.layout.fragment_login_land, container, false)
} else {
v = inflater.inflate(R.layout.fragment_login, container, false)
}
mUsernameEditText = v.findViewById<EditText>(R.id.username_text)
mPasswordEditText = v.findViewById<EditText>(R.id.password_text)
// Set up onClickListeners for buttons
return v
}
28
AccountFragment:
onCreateView(): Java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.accountfragment, container, false);
Activity activity = getActivity();
if (activity != null) {
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
mEtUsername = v.findViewById(R.id.username);
mEtPassword = v.findViewById(R.id.password);
mEtConfirm = v.findViewById(R.id.password_confirm);
// OnClickListeners for Add, Cancel buttons
Button btnExit = v.findViewById(R.id.exit_button);
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
btnExit.setOnClickListener(this);
} else { btnExit.setVisibility(View.GONE); }
return v;
}
29
AccountFragment:
onCreateView(): Kotlin
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_account, container, false)
mEtUsername = v.findViewById(R.id.username)
mEtPassword = v.findViewById(R.id.password)
mEtConfirm = v.findViewById(R.id.password_confirm)
// Set up OnClickListeners for Add, Cancel buttons
val btnExit = v.findViewById(R.id.exit_button)
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
btnExit.setOnClickListener(this)
} else {
btnExit.setVisibility(View.GONE)
}
return v
}
30
AccountFragment: onClick():
Java
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.done_button:
createAccount();
break;
case R.id.cancel_button:
mEtUsername.setText(""); mEtPassword.setText("");
mEtConfirm.setText("");
break;
case R.id.exit_button:
FragmentActivity activity = getActivity();
if (activity != null) {
activity.getSupportFragmentManager().popBackStack();
}
}
} 31
AccountFragment: onClick():
Kotlin
override fun onClick(view: View) {
when (view.id) {
R.id.done_button -> createAccount()
R.id.cancel_button -> {
mEtUsername.setText("")
mEtPassword.setText("")
mEtConfirm.setText("")
}
R.id.exit_button ->
activity?.supportFragmentManager?.popBackStack()
}
}
32
Thank You
Questions and comments?
33

More Related Content

Similar to 07_UIAndroid.pdf

Android development for iOS developers
Android development for iOS developersAndroid development for iOS developers
Android development for iOS developersDarryl Bayliss
 
Android Tutorial
Android TutorialAndroid Tutorial
Android TutorialFun2Do Labs
 
Native Android Development Practices
Native Android Development PracticesNative Android Development Practices
Native Android Development PracticesRoy Clarkson
 
Android Workshop
Android WorkshopAndroid Workshop
Android WorkshopJunda Ong
 
Android por onde começar? Mini Curso Erbase 2015
Android por onde começar? Mini Curso Erbase 2015 Android por onde começar? Mini Curso Erbase 2015
Android por onde começar? Mini Curso Erbase 2015 Mario Jorge Pereira
 
Lec005 android start_program
Lec005 android start_programLec005 android start_program
Lec005 android start_programEyad Almasri
 
Design Patterns for Tablets and Smartphones
Design Patterns for Tablets and SmartphonesDesign Patterns for Tablets and Smartphones
Design Patterns for Tablets and SmartphonesMichael Galpin
 
行動App開發管理實務 unit2
行動App開發管理實務 unit2行動App開發管理實務 unit2
行動App開發管理實務 unit2Xavier Yin
 
Answer1)Responsive design is the idea where all the developed pag.pdf
Answer1)Responsive design is the idea where all the developed pag.pdfAnswer1)Responsive design is the idea where all the developed pag.pdf
Answer1)Responsive design is the idea where all the developed pag.pdfankitcomputer11
 
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점Jeado Ko
 
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점 Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점 WebFrameworks
 
Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Eliran Eliassy
 
Writing HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAEWriting HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAERon Reiter
 
Android Development Made Easy - With Sample Project
Android Development Made Easy - With Sample ProjectAndroid Development Made Easy - With Sample Project
Android Development Made Easy - With Sample ProjectJoemarie Amparo
 
Zyncro zyncro apps & ui customization feb 2013
Zyncro zyncro apps & ui customization feb 2013Zyncro zyncro apps & ui customization feb 2013
Zyncro zyncro apps & ui customization feb 2013Zyncro
 
What's New in Android
What's New in AndroidWhat's New in Android
What's New in AndroidRobert Cooper
 

Similar to 07_UIAndroid.pdf (20)

08ui.pptx
08ui.pptx08ui.pptx
08ui.pptx
 
Android development for iOS developers
Android development for iOS developersAndroid development for iOS developers
Android development for iOS developers
 
Android Tutorial
Android TutorialAndroid Tutorial
Android Tutorial
 
Native Android Development Practices
Native Android Development PracticesNative Android Development Practices
Native Android Development Practices
 
Android Workshop
Android WorkshopAndroid Workshop
Android Workshop
 
Android por onde começar? Mini Curso Erbase 2015
Android por onde começar? Mini Curso Erbase 2015 Android por onde começar? Mini Curso Erbase 2015
Android por onde começar? Mini Curso Erbase 2015
 
Lec005 android start_program
Lec005 android start_programLec005 android start_program
Lec005 android start_program
 
Android programming basics
Android programming basicsAndroid programming basics
Android programming basics
 
Design Patterns for Tablets and Smartphones
Design Patterns for Tablets and SmartphonesDesign Patterns for Tablets and Smartphones
Design Patterns for Tablets and Smartphones
 
行動App開發管理實務 unit2
行動App開發管理實務 unit2行動App開發管理實務 unit2
行動App開發管理實務 unit2
 
Hello Android
Hello AndroidHello Android
Hello Android
 
Android 3
Android 3Android 3
Android 3
 
Answer1)Responsive design is the idea where all the developed pag.pdf
Answer1)Responsive design is the idea where all the developed pag.pdfAnswer1)Responsive design is the idea where all the developed pag.pdf
Answer1)Responsive design is the idea where all the developed pag.pdf
 
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
 
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점 Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
 
Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics
 
Writing HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAEWriting HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAE
 
Android Development Made Easy - With Sample Project
Android Development Made Easy - With Sample ProjectAndroid Development Made Easy - With Sample Project
Android Development Made Easy - With Sample Project
 
Zyncro zyncro apps & ui customization feb 2013
Zyncro zyncro apps & ui customization feb 2013Zyncro zyncro apps & ui customization feb 2013
Zyncro zyncro apps & ui customization feb 2013
 
What's New in Android
What's New in AndroidWhat's New in Android
What's New in Android
 

Recently uploaded

FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 

Recently uploaded (20)

FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 

07_UIAndroid.pdf

  • 1. User Interface Development CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr. Rajiv Ramnath Reading: Big Nerd Ranch Guide, Chapter 7 (Fragments) 1
  • 2. Outline • UI Support in Android • Fragments 2
  • 3. UI Support in the Android SDK • “Inverted” paradigm – Each subclass constrains functionality (rather than extend it) – Hundreds of methods are exposed: L • Base classes: – ViewGroup base class for composite UI elements – View base class for terminal UI components 3
  • 4. View Hierarchy 4 # = SDK Version number
  • 5. ViewGroup Hierarchy • Direct Subclasses: – AbsoluteLayout – AdapterView<T extends Adapter> – FragmentBreadCrumbs – FrameLayout – GridLayout – LinearLayout – PagerTitleStrip – RelativeLayout – SlidingDrawer – ViewPager • 19 indirect subclasses. See: – http://developer.android.com/reference/android/view/ViewGroup.html 5
  • 6. Sample Layout, Login Activity <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android” android:background="@color/background" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="20dip"> <LinearLayout android:orientation="vertical ... <TextView android:text="@string/login_title” ... /> <TextView ... /> <EditText android:id="@+id/username_text” ... /> <TextView ... /> <EditText ... /> <Button ... /> <Button ... /> <Button ... /> </LinearLayout> </ScrollView>
  • 7. Layout Configuration • ID: android:id="@+id/username_text” : used for widget handle • Parameters: – layout_width, layout_height – layout_marginTop / ...marginRight, layout_margin – orientation • Can be combined: layout_gravity=“bottom|right” • Constants: match_parent, wrap_content • Width and height specs: dp, sp (display pixels, scaled pixels) • A wide range of LayoutParams • Resources (e.g. backgrounds): android:background=“@drawable/backdrop” • Blank canvases using a (generic) View element in the layout 7
  • 8. 8
  • 9. Adding Resources 9 Resources can also be added by right- clicking on res directory, selecting New→Android Resource File
  • 10. Other Layout Parameters, Techniques • Inherit parameters from enclosing elements • layout_span spans multiple columns • Empty views to add blank canvases (filled later) • Shrink or stretch columns as needed (shrinkColumns, stretchColumns) • RelativeLayout lets window manager handle size • ConstraintLayout specifies widget constraints for flexible widget rendering 10
  • 11. Linking a UI to a Fragment: Java // LoginActivity.java public class LoginActivity extends SingleFragmentActivity { @Override protected Fragment createFragment() { return new LoginFragment(); } } // LoginFragment.java @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_login, container, false); // Real code handles view rotation mUsernameEditText = (EditText) v.findViewById(R.id.username_text); mPasswordEditText = (EditText) v.findViewById(R.id.password_text); // Setup listener objects for buttons return v; } 11 Also can set up listener objects in Activities with onCreate()
  • 12. Linking a UI to a Fragment: Kotlin // LoginActivity.kt class LoginActivity : SingleFragmentActivity() { override fun createFragment(): Fragment { return LoginFragment() } } // LoginFragment.kt override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val v: View = inflater.inflate(R.layout.fragment_login, container, false) // Real code handles view rotation mUsernameEditText = v.findViewById<EditText>(R.id.username_text) mPasswordEditText = v.findViewById<EditText>(R.id.password_text) // Set up listener objects for Buttons return v } 12
  • 13. Creating a Custom Widget: Layout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#676767" android:gravity="center_horizontal" android:padding="20dip"> <com.wiley.fordummies.androidsdk.tictactoe.Board android:id="@+id/board" android:layout_width="match_parent" android:layout_height="280dip"/> ... </LinearLayout> 13
  • 14. Creating a Custom Widget: Java // Board.java public class Board extends View { public Board(Context context, AttributeSet attributes) { super(context, attributes); // . . . setFocusable(true); setFocusableInTouchMode(true); // . . . } // . . . protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // . . . } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // . . . } } 14 Instantiating the view: // LoginFragment.java: onCreateView() and setupBoard() // onCreateView() v = inflater.inflate(R.layout.fragment_game_session, . . .); // setupBoard() mBoard = (Board) v.findViewById(R.id.board);
  • 15. Creating a Custom Widget: Kotlin // Board.kt class Board(context: Context, attributes: AttributeSet) : View(context, attributes) { init { isFocusable = true isFocusableInTouchMode = true // . . . } override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { super.onSizeChanged(w, h, oldw, oldh) // . . . } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) // . . . } } 15 Instantiating the view: // LoginFragment.kt, onCreateView() and setupBoard() // onCreateView() v = inflater.inflate(R.layout.fragment_game_session, container, false) // setupBoard() mBoard = (Board) v.findViewById(R.id.board)
  • 16. Creating a Layout via Code // DohActivity.java @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = new LinearLayout(this); Button dohButton = new Button(this); dohButton.setText(“In case of meltdown, Push Me!”); layout.addView(dohButton); setContentView(layout); } 16
  • 17. Styles and Themes (1) Use: <EditText style=”@style/DarkBold” android:text=”Hello” /> Definition: <?xml version=”1.0” encoding=”utf-8”?> <resources> <style name=“DarkBold”> <item name=”android:layout_width”> match_parent </item> <item name=“android:layout_height”> wrap_content </item> ... more parameters ... </style> </resources> 17 Inheritance: <style name=“DarkPhone” parent=”@style/DarkBold”> <item name=”android:phoneNumber”>true</item> </style>
  • 18. Styles and Themes (2) • Stored in res/values/ directory with .xml extension (name not relevant) • Can set application-wide and activity-specific styles (aka themes): – Set themes in AndroidManifest.xml on <application> tag or <Activity> tag <application android:theme="@style/CustomTheme"> <activity android:theme="@android:style/Theme.Translucent"> • Can even create version-specific layout files • Ref: http://developer.android.com/guide/topics/ ui/themes.html 18
  • 19. Outline • UI Support in Android • Fragments 19
  • 20. Fragments and Their Rationale • Fragment: Composite UI component that handles its own UI • One, multiple Fragments in an Activity • Separate class hierarchy: Fragment, DialogFragment, ListFragment, PreferenceFragment, etc. • Goals: – Separate UI design from Activity design – UI should have its own lifecycle and flow – Dynamically add/remove UI components in running activity • Drivers: heterogeneous devices, user experience 20
  • 21. Example – Login and Account
  • 22. Portrait Layout: Login 22 <?xml version="1.0" encoding="utf-8"?> <ScrollView ...> <LinearLayout...> <TextView android:text="@string/login_title".../> <TextView android:text="@string/enter_username”.../> <EditText android:id="@+id/username_text”.../> <TextView android:text="Enter Password".../> <EditText android:id="@+id/password_text".../> <Button android:id="@+id/login_button".../> <Button android:id="@+id/cancel_button”.../> <Button android:id="@+id/new_user_button”.../> </LinearLayout> </ScrollView>
  • 23. Portrait Layout: Account <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...> <FrameLayout .../> ⟸ ⟸ Placeholder for fragment <Button android:id="@+id/exit_button”.../> </LinearLayout> What happened to landscape layout of the Fragment? 23
  • 24. Landscape Layout: Login <?xml version="1.0" encoding="utf-8"?> <LinearLayout... android:orientation="horizontal” ...> ⟸ ⟸ Note: horizontal layout <ScrollView ... > <LinearLayout .. > <TextView ... /> <TextView ... /> <!-- . . . --> </LinearLayout> </ScrollView> <fragment class="com.wiley.fordummies.androidsdk.tictactoe.AccountFragment" android:id="@+id/titles" android:layout_height="match_parent” . . ./> </LinearLayout> 24
  • 25. AccountFragment: Portrait Layout <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...> <LinearLayout...> <TextView android:text="New Account" .../> <TextView android:text="Username”.../> <EditText android:id="@+id/username”.../> <TextView android:text="Password”.../> <EditText android:id="@+id/password”.../> <TextView android:text="Confirm Password" .../> <EditText android:id="@+id/password_confirm”.../> <Button android:id="@+id/cancel_button” "/> <Button android:id="@+id/done_button”.../> </LinearLayout> </LinearLayout> 25
  • 26. AccountFragment: Landscape Layout <?xml version="1.0" encoding="utf-8"?> <LinearLayout...> <LinearLayout...> <TextView android:text="New Account”.../> <TextView android:text="Username”.../> <EditText android:id="@+id/username”...”/> <TextView android:text="Password" .../> <EditText android:id="@+id/password”.../> <TextView android:text="Confirm Password" .../> <EditText android:id="@+id/password_confirm”.../> <LinearLayout ... > <Button android:id="@+id/cancel_button”.../> <Button android:id="@+id/done_button”.../> </LinearLayout> </LinearLayout> </LinearLayout> 26
  • 27. LoginFragment: Java @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v; int rotation = getActivity().getWindowManager().getDefaultDisplay() .getRotation(); if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) { v = inflater.inflate(R.layout.fragment_login_land, container, false); } else { v = inflater.inflate(R.layout.fragment_login, container, false); } mUsernameEditText = (EditText) v.findViewById(R.id.username_text); mPasswordEditText = (EditText) v.findViewById(R.id.password_text); // Set up OnClickListeners for buttons return v; } 27
  • 28. LoginFragment: Kotlin override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val v: View val rotation = activity.windowManager.defaultDisplay.rotation if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) { v = inflater.inflate(R.layout.fragment_login_land, container, false) } else { v = inflater.inflate(R.layout.fragment_login, container, false) } mUsernameEditText = v.findViewById<EditText>(R.id.username_text) mPasswordEditText = v.findViewById<EditText>(R.id.password_text) // Set up onClickListeners for buttons return v } 28
  • 29. AccountFragment: onCreateView(): Java public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.accountfragment, container, false); Activity activity = getActivity(); if (activity != null) { int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); mEtUsername = v.findViewById(R.id.username); mEtPassword = v.findViewById(R.id.password); mEtConfirm = v.findViewById(R.id.password_confirm); // OnClickListeners for Add, Cancel buttons Button btnExit = v.findViewById(R.id.exit_button); if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) { btnExit.setOnClickListener(this); } else { btnExit.setVisibility(View.GONE); } return v; } 29
  • 30. AccountFragment: onCreateView(): Kotlin override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val v = inflater.inflate(R.layout.fragment_account, container, false) mEtUsername = v.findViewById(R.id.username) mEtPassword = v.findViewById(R.id.password) mEtConfirm = v.findViewById(R.id.password_confirm) // Set up OnClickListeners for Add, Cancel buttons val btnExit = v.findViewById(R.id.exit_button) if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) { btnExit.setOnClickListener(this) } else { btnExit.setVisibility(View.GONE) } return v } 30
  • 31. AccountFragment: onClick(): Java @Override public void onClick(View view) { switch (view.getId()) { case R.id.done_button: createAccount(); break; case R.id.cancel_button: mEtUsername.setText(""); mEtPassword.setText(""); mEtConfirm.setText(""); break; case R.id.exit_button: FragmentActivity activity = getActivity(); if (activity != null) { activity.getSupportFragmentManager().popBackStack(); } } } 31
  • 32. AccountFragment: onClick(): Kotlin override fun onClick(view: View) { when (view.id) { R.id.done_button -> createAccount() R.id.cancel_button -> { mEtUsername.setText("") mEtPassword.setText("") mEtConfirm.setText("") } R.id.exit_button -> activity?.supportFragmentManager?.popBackStack() } } 32
  • 33. Thank You Questions and comments? 33