AppCompat Default App Bar

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

Figure 14.2  The app bar

The app bar

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

  • adding the Jetpack AppCompat foundation library dependency

  • applying one of the AppCompat themes that includes an app bar

Open your app/build.gradle file to see the AppCompat dependency:

    dependencies {
        ...
        implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
        ...

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. You can explore what is contained in each of the AppCompat subpackages on the official API listings at developer.android.com/​reference/​kotlin/​androidx/​packages.

Android Studio automatically sets your app’s theme to Theme.AppCompat.Light.DarkActionBar when it creates your project. The theme, which specifies default styling for your entire app, is set in res/values/styles.xml:

    <resources>

        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
          <!-- Customize your theme here. -->
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</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. Notice the android:theme attribute. You should see something similar to this:

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

You will learn much more about styles and themes in Chapter 21. Now it is time to add actions 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
3.227.252.87