GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Defining The ActionBar Defining The ActionBar
codepath-wiki-review[bot] edited this page
The ActionBar, now known as the App Bar, is a consistent navigation element that is standard throughout modern Android applications. The ActionBar can consist of:
Important to note is that prior to 3.0, there was no ActionBar. In 2013, Google announced a support library that provides much better compatibility for older versions and support for tabbed interfaces. Since most of the examples below depend on this support library, make sure to include the AppCompat library. Every application unless otherwise specified has an ActionBar by default. The ActionBar by default now has just the title for the current activity. The ActionBar title displayed at the top of the screen is governed by the <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name="com.codepath.example.simpleapp.FirstActivity" android:label="@string/activity_name" > </activity> </application> Change the supportActionBar?.title = "My new title" // set the top title val title: String? = supportActionBar?.title?.toString() // get the title (null if no ActionBar) supportActionBar?.hide() // or even hide the actionbar ActionBar actionBar = getSupportActionBar(); // or getActionBar(); getSupportActionBar().setTitle("My new title"); // set the top title String title = actionBar.getTitle().toString(); // get the title actionBar.hide(); // or even hide the actionbar You can also change many other properties of the ActionBar not covered here. See the Extended ActionBar Guide for more details. In the new Android 5.0 material design guidelines, the style guidelines have changed to discourage the use of the icon in the ActionBar. Although the icon can be added back with: supportActionBar?.setDisplayShowHomeEnabled(true) supportActionBar?.setLogo(R.mipmap.ic_launcher) supportActionBar?.setDisplayUseLogoEnabled(true) getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setLogo(R.mipmap.ic_launcher); getSupportActionBar().setDisplayUseLogoEnabled(true); The above code results in: You can read more about this on the material design guidelines which state: "The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer." When you want to add primary actions to the ActionBar, you add the items to the activity context menu and if properly specified, they will automatically appear at the top right as icons in the ActionBar. An activity populates the ActionBar from within the class MainActivity : AppCompatActivity() { override fun onCreateOptionsMenu(menu: Menu) : Boolean { // Inflate the menu; this adds items to the action bar if it is present. menuInflater.inflate(R.menu.menu_main, menu) return true } } public class MainActivity extends AppCompatActivity { @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } } Entries in the action bar are typically called actions. Use this method to inflate a menu resource that defines all the action items within a <!-- Menu file for `activity_movies.xml` is located in a file such as `res/menu/menu_movies.xml` --> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/miCompose" android:icon="@drawable/ic_compose" app:showAsAction="ifRoom" android:title="Compose"> </item> <item android:id="@+id/miProfile" android:icon="@drawable/ic_profile" app:showAsAction="ifRoom|withText" android:title="Profile"> </item> </menu> You also should note that the The above code results in two action icons being displayed: Note: The above code refers to the There are two ways to handle the click for an ActionBar item. The first approach is you can use the <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/item1" android:icon="@drawable/ic_compose" android:onClick="onComposeAction" app:showAsAction="ifRoom" android:title="Compose"> </item> </menu> and then define the method class MainActivity : AppCompatActivity() { fun onComposeAction(mi: MenuItem) { // handle click here } } public class MainActivity extends AppCompatActivity { public void onComposeAction(MenuItem mi) { // handle click here } } The second approach is to use the override fun onOptionsItemSelected(item: MenuItem): Boolean { // Handle presses on the action bar items when (item.getItemId()) { R.id.miCompose -> { composeMessage() return true } R.id.miProfile -> { showProfileView() return true } else -> return super.onOptionsItemSelected(item) } } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items int itemId = item.getItemId(); if (itemId == R.id.miCompose) { composeMessage(); return true; } else if (itemId == R.id.miProfile) { showProfileView(); return true; } return super.onOptionsItemSelected(item); } and then you can handle all the action buttons in this single method. Note: Android Gradle Plugin 8.0 and higher generates ToolBar was introduced in Android Lollipop, API 21 release and is the spiritual successor of the ActionBar. It's a [[ ToolBar works well with apps targeted to API 21 and above. However, Android has updated the AppCompat support libraries so the ToolBar can be used on lower Android OS devices as well. In AppCompat, ToolBar is implemented in the
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license. Finding these guides helpful? We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out. Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources. Clone this wiki locally |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |