The Default App Bar

CriminalIntent already has a simple app bar in place (Figure 15.2).

Figure 15.2  The app bar

The app bar

This is because Android Studio sets up all new projects with activities that extend from AppCompatActivity to include an app bar by default. It does this by:

  • adding the Jetpack AppCompat and Material Components library dependencies

  • applying one of the themes that includes an app bar

Open your app/build.gradle file (the one labeled (Module: CriminalIntent.app)) to see the AppCompat and Material Components dependencies:

    dependencies {
        ...
        implementation 'androidx.appcompat:appcompat:1.4.1'
        implementation 'com.google.android.material:material:1.5.0'
        ...

AppCompat is short for application compatibility. The Jetpack AppCompat foundation library contains classes and resources that are core to providing a consistent-looking UI across different versions of Android.

Android Studio automatically defines your app’s theme when it creates your project. By default, this theme extends from Theme.MaterialComponents.DayNight.DarkActionBar. This theme is a part of the Material Components library, which brings the latest Material Design features (Google’s design language) to your app. The Material Components library builds on AppCompat to make its design touches work across different versions.

Your app’s theme, which specifies default styling for the entire app, is set in res/values/themes.xml:

    <resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
        <style name="Theme.CriminalIntent"
            parent="Theme.MaterialComponents.DayNight.DarkActionBar">
            <!-- Primary brand color. -->
            <item name="colorPrimary">@color/purple_500</item>
            ...
        </style>
    </resources>

The theme for your application is specified at the application level and optionally per activity in your manifest. Open manifests/AndroidManifest.xml and look at the <application> tag. Check out the android:theme attribute. You should see something similar to this:

    <manifest ... >
        <application
            ...
            android:theme="@style/Theme.CriminalIntent" >
            ...
        </application>
    </manifest>

OK, enough background. It is time to add an action to the app bar.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset