- The document discusses user interface development in Android, focusing on fragments.
- Fragments allow dividing the user interface into modular sections that can be reused across activities and handle their own lifecycles. This improves separation of concerns and allows dynamic configuration of UI components.
- The example shows a login fragment and account fragment used in both portrait and landscape orientations by inflating different layout files depending on device rotation.
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
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
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
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
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
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
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