Home » Android » android – missing ActionBar in material design

android – missing ActionBar in material design

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to develop an Android app with AndroidStudio and the new material design and it looks great on Android 5.0 but when I test the app on my 4.4.2 phone, the ActionBar of the app is missing.

Here is my Manifest:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

the styles.xml

<resources>

<!-- Base application theme. -->
<style name="MyTheme" parent="Theme.AppCompat.Light">
</style>

and the build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.0.0"

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:+'
}

Do you know where the error is?

Thank you!
Simon

How to&Answers:

To use the appcompat-v7 action bar backport, you need to have your activities inherit from ActionBarActivity.

Quoting Chris Banes’ blog post:

If you are not currently using AppCompat, or you are starting from scratch, then here’s how to set it up:

  • All of your Activities must extend from ActionBarActivity. It extends from FragmentActivity from the v4 support library, so you can continue to use fragments.
  • All of your themes (that want an action bar/Toolbar) must inherit from Theme.AppCompat. There are variants available including Light and NoActionBar.
  • When inflating anything to be displayed on the action bar (such as a SpinnerAdapter for list navigation in the action bar), make sure you use the action bar’s themed context retrieved via getSupportActionBar().getThemedContext().
  • You must use the static methods in MenuItemCompat for any action-related calls on a MenuItem.

UPDATE: Note that Google is migrating to AppCompatActivity. ActionBarActivity works — it is just a do-nothing subclass of AppCompatActivity — but you are better served by directly extending AppCompatActivity if you are going to use the appcompat-v7 action bar backport.