Android: Toolbar, Activeness Bar In Addition To Carte Du Jour Items (Including Spinner)
It's rattling slow to drag out a Toolbar when designing your interface but you lot can't only drib buttons into it. There is a to a greater extent than mystical agency of structure to teach inwards into. Toolbars concur a menu. The top dog toolbar that you lot run across when you lot exercise an empty app is assigned Action Bar condition inwards the onCreate method.
Finally nosotros tin add together a listener to the spinner.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);You volition also notice its bill of fare beingness "inflated" later on inwards the onCreateMenuOptionsMenu method.
getMenuInflater().inflate(R.menu.menu_main, menu);The setting of the Toolbar every bit an Action Bar enables the following:
Once you lot laid the toolbar every bit an activity's app bar, you lot accept access to the diverse utility methods provided past times the v7 appcompat back upwards library's ActionBar class. This approach lets you lot exercise a publish of useful things, similar enshroud together with present the app bar.While the inflation adds the bill of fare items from res/menu/menu_main.xml to the toolbar.
To purpose the ActionBar utility methods, telephone band the activity's getSupportActionBar() method. This method returns a reference to an appcompat ActionBar object. Once you lot accept that reference, you lot tin telephone band whatever of the ActionBar methods to adjust the app bar. For example, to enshroud the app bar, telephone band ActionBar.hide(). (Use App Bar Utility Methods)
Adding a minute toolbar
While you lot tin freely add together items to the primary toolbar, the enquiry is how exercise nosotros add together toolbars together with exercise all this for a secondary toolbar.Step One: Create Your Menu
First exercise your bill of fare together with its items:<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.gylphi.anthonylevings.contextualmenuapp.MainActivity"> <item android:id="@+id/bold" android:orderInCategory="200" android:title="Android: Toolbar, Action Bar together with Menu Items (including Spinner)" app:showAsAction="always"/> <item android:id="@+id/italics" android:orderInCategory="300" android:title="Android: Toolbar, Action Bar together with Menu Items (including Spinner)" app:showAsAction="always" /> <item android:id="@+id/underline" android:orderInCategory="400" android:title="Android: Toolbar, Action Bar together with Menu Items (including Spinner)" app:showAsAction="always" /> <item android:id="@+id/spinner" android:orderInCategory="500" android:title="Android: Toolbar, Action Bar together with Menu Items (including Spinner)" app:actionViewClass="android.widget.Spinner" android:background="#ff00" app:showAsAction="always" /> </menu>There are 2 types of item here. The starting fourth dimension is a regular type of item amongst a uncomplicated title. The minute has an Action View Class, which enables to a greater extent than sophisticated actions from the toolbar.
Handling the Simple Menu Items
After inflating our menu:Toolbar secondToolbar = (Toolbar) findViewById(R.id.toolbar2); secondToolbar.inflateMenu(R.menu.formatting_toolbar);We tin grip the uncomplicated items inwards the next way:
secondToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override world boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { instance R.id.action_settings: render true; instance R.id.bold: Log.d("type", "bold"); render true; instance R.id.italics: Log.d("type", "italics"); render true; instance R.id.underline: Log.d("type", "underline"); render true; } render true; } });
Handling the Action View Class Item
The Spinner is accessed a piddling differently:MenuItem itemM = secondToolbar.getMenu().findItem(R.id.spinner); Spinner spinner = (Spinner) itemM.getActionView();It tin together with thence move fix amongst an Array Adapter:
ArrayAdapterSee previous post on XML together with Android for to a greater extent than exceptional together with the accompanying XML that is beingness accessed here.adapter = ArrayAdapter.createFromResource(this, R.array.font_sizes, android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter);
Finally nosotros tin add together a listener to the spinner.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override world void onItemSelected(AdapterView adapterView, View view, int i, long l) { Log.d("Select", "You chose " + adapterView.getSelectedItem().toString()); } @Override world void onNothingSelected(AdapterView adapterView) { } });And all should move working every bit expected.
Comments
Post a Comment