-
Notifications
You must be signed in to change notification settings - Fork 0
Lab 7
-
Update styling of
Toolbar
- In
activity_main.xml
changeandroidx.appcompat.widget.Toolbar
tocom.google.android.material.appbar.MaterialToolbar
- Set
style="@style/Widget.MaterialComponents.Toolbar.Primary"
- In
-
Add an overflow menu to the
Toolbar
that includes an option for showingTwitterActivity
- Create a new
Menu
resource namedmain_toolbar.xml
- Add a menu item with
id="@+id/shareOnTwitter
andtitle="Share on Twitter
- Within
activity_main.xml
addapp:menu="@menu/main_toolbar"
to yourMaterialToolbar
- Remove the call to
setSupportActionBar()
fromMainActivity
. We can remove this now that we are taking full control over ourToolbar
.- If we don't remove this line, the
Activity
will clear the menu we have inflated.
- If we don't remove this line, the
- In
MainActivity
, implementToolbar.setOnMenuItemClickListener()
to respond to clicking the menu item by showingTwitterActivity
- Create a new
-
Prepare
MyNotesFragment
for future interactions- Change the
createNoteButton
to be aFloatingActionButton
- Reconstrain the
FloatingActionButton
to the bottom, and end, of the screen
- Change the
-
Add dimension resources for spacing values
- Create a resources file named
dimens.xml
- Create a dimen named
spacing_1x
with a value of8dp
- Create a dimen named
spacing_2x
with a value of16dp
- Use either (or both) these dimension resources to add margin to a view inside
fragment_create_note.xml
- ex
android:layout_marginEnd="@dimen/spacing_2x"
- ex
- Create a resources file named
-
Setup basic note creation UI for
CreateNoteFragment
- Add a
TextInputLayout
for collecting a note title from the user - Add a
Spinner
for selecting note category - Add a
TextInputLayout
for collecting the note content from the user - Add a
FloatingActionButton
for saving a note - Convert
CreateNoteFragment
to use ViewBinding and create a class property storing a reference to the binding object - Create a class
CategorySpinnerAdapter
that extendsArrayAdapter
and populates the adapter with aList<String>
- Create a list of Strings you want to represent your note categories,
- Populate the spinner by creating an instance of
CategorySpinnerAdapter
and assigning it tobinding.categorySpinner.adpater
- Instantiating your spinner should look something like this
CategorySpinnerAdapter(requireContext(), CATEGORIES)
- The call to
requireContext()
will get a non-null reference to the parentContext
for theFragment
. AnyContext
will work here. We userequireContext()
specifically because it returns a non-null value, or it will throw an exception. You could replacerequireContext()
withrequireActivity()
becauseActivity
is a validContext
.
- Instantiating your spinner should look something like this
- Add a
Β
1. Add a `TextView` for displaying the note title
2. Add a `TextView` for displaying the note category
3. Add a `TextView` for displaying the note content
Update your TextInputLayout
s to use the Outlined
theme so they look a nicer.
Β
Β
- BottomNavigationView Documentation
- Set Up the App Bar Documentation
- Toolbar.inflateMenu()
- Populating a Dropdown Spinner
- TextInputLayout Documentation
- MaterialDesign Text Fields Guidance
Β
There are a few things you can check:
- Did you add
app:menu="@menu/main_toolbar"
to yourToolbar
declaration inactivity_main.xml
? - Did you remove the call to
setSupportActionBar()
fromMainActivity
?
Β
ArrayAdapter
will enabled us to take a collection of items, String
in this case, and turn that collection into View
s that can then be used to populate our Spinner
. Because our Spinner
is very simple in this case, we don't have to create a custom layout and can instead use a built-in layout of android.R.layout.simple_spinner_dropdown_item
.
class CategorySpinnerAdapter(
context: Context, items: List<String>) :
ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item
) {
init {
addAll(items)
}
}
Β
TextInputLayout
is meant to wrap an EditText
. So, when using them, we want to declare first the TextInputLayout
in our layout file, and then add an EditText
as a child view.
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Enter your note"
/>
</com.google.android.material.textfield.TextInputLayout>
Β
First, you'll need to create an Android resource file within the res/values
directory. By convention, this file is typically named dimens.xml
. Within this file, we can define individual dimension resources and their values.
<resources>
<dimen name="spacing_1x">8dp</dimen>
<dimen name="spacing_2x">16dp</dimen>
</resources>
Β
The title of a Toolbar
can be set programmatically or in xml.
app:title="Android Study Guide"
binding.toolbar.title = "Your Title"
Β
Do you have a view that doesn't seem to respect the constraints you've applied to it?
Is your view filling the screen in some axis instead of restricting itself to some barrie/alignment?
This can happen if you've applied constraints, but told the view to match_parent
along some axis. Double check that your height/width is using 0dp
size along whichever axis it should be constrained.
Β
TextInputLayout
comes in 2 styles:
Filled
Outlined
To change the style to Outline
apply the following to your XML style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"