Week 4 Toolbar and Menu
Week 4 Toolbar and Menu
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings
"/>
</menu>
Showing the Menu
To display menu, need to override, or else the option menu will not display
Menu Resource Files
The root element has the tag <menu>.
– As always it must declare the xmlns:android atribute.
Items have the tag <item>.
Items can have the following atributes:
– android:id
● As before, it lets us refer to the menu resource in
–
code.
android:title
The text that is displayed for the menu
●
item.
–
android:icon
Reference to a Drawable resource to provide an icon for the
●
item.
–
android:showAsActio
Determines whether the menu item can appear in the Action Bar rather than the overflow
●
n item.
Menus: Event Handling
There is one event handler for all menu “click” events. Your
After the user click on the option, what action
Activity needs to override should be perform
onOptionsItemSelected(item:MenuItem)
You can find which item it was by using the itemId
attribute of a MenuItem.
This is a Boolean method.
If you handle the menu event, you should return true.
Otherwise, return the superclass implementation of the method.
Menu Example: Resource File
|
|
|
|
|
radioButtons | Need to implement the onRadioButtonClicked,
inside | then check whether it is clicked or not
radioGroup |
|
|
|
|
|
RadioButton Event Handling 1
Like this:
RadioButton Event Handling 2
But this is more preferred: implement into radioGroup
Spinners
Quick way to select one value from a set.
In the default state shows currently selected
value.
Touching displays a dropdown menu available
values
Spinners Resources
The choices you provide for the spinner can come
from any source
string arrays in strings.xml resource file.
Normally works with SpinnerAdapter or
ArrayAdapter.
Optional way by using the entries attributes.
Spinners Resources
<?xml version="1.0"
encoding="utf-8"?>
<resources>
<string-array
name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
Spinner - Layout
<Spinner
android:id="@+id/contact_plannet"
android:layout_width="match_parent"
android:layout_height="wrap_content“
android:entries =
“@array/planets_array” />
Spinner – Event Handling
val spinner = findViewById<Spinner>(R.id.spinner)
val planets = resources.getStringArray(R.array.planets_array)
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, planets)
Send the adapter to here
<NumberPicker android:id="@+id/numberpicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Number Picker Event Handling
numberPicker.minValue = 0
numberPicker.maxValue = 10
numberPicker.wrapSelectorWheel = true
datePicker.init( today.get(Calendar.YEAR),
today.get(Calendar.MONTH),
today.get(Calendar.DAY_OF_MONTH))
{ view, year, month, day ->
override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
// Do something with the date chosen by the user
}
}
DatePicker – Using
DialogFragment Part 2
Showing DatePicker with the DialogFragment
AlertDialog.Builder
Negative button
Neutral button no action will be performed
Eg. Maybe ( 不確定的時候選的 ) Positive button
Alert Dialogs Example
val builder = AlertDialog.Builder(this)
builder.setTitle("Androidly Alert")
builder.setMessage("We have a message")
//builder.setPositiveButton("OK", DialogInterface.OnClickListener(function = x))