9

I'm new to Android development and I'm trying to follow Android development guide in Android Studio, specifically trying to set up an action bar.

My minSdkVersion is 15, stated in build:gradle(Module:app) so I would think I wouldn't need to use any App Compatibility Support but my theme, as it says in styles.xml is <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> and I can't change it to any Holo whitout crashing my app anytime I run it.

Besides, using this one I can't use android:showAsAction(it just doesnt work) and instead need to use app:showAsAction and all the Android Support library.

Thanks in advance.

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Joaquim Ferrer
  • 603
  • 6
  • 23
  • Show us the specific problems you're stuck with, so we can help you with that. As it currently stands, your question is off-topic as per the help center. – nhaarman Jul 10 '15 at 10:50
  • if you don't want to support earlier versions of android then you won't need it. you can reconfigure the settings and should set min sdk support to the latest api you are using. – Shekhar Jul 10 '15 at 10:51
  • When I add a item in a menu xml and state android:showAsAction="ifRoom" it throws an error and says I should use app:showAsAction as I'm using appcompat library. But I don't want to use appcompat libraries. My min SDK version is API 15. I reseted all my project to make it 15 in the beginning. – Joaquim Ferrer Jul 10 '15 at 11:02
  • You could remove the appcompat dependency from your build.gradle.. However it is recommended that you use the support library even though your minSdk is higher since they are updated and also contain bugfixes – ashkhn Jul 10 '15 at 11:36
  • Are you sure? In the android development lessons they say that as My min SDK version is high enough I shouldn't need to use them (although I do). If you are sure I will use them though. – Joaquim Ferrer Jul 10 '15 at 11:49

2 Answers2

11

CommonsWare provided the correct steps but I still battled as there wasn't enough details for me to know exactly what to do (being new to Android Studio and Android development).

I found a blog post that explains the details here and it worked for me: https://mobiarch.wordpress.com/2015/04/17/removing-support-library-in-android-studio

Here is what it says (I have added some additional help):

Open build.gradle from your project. Locate the dependencies section.

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

Remove the line for the compatibility library. After that the section should look like this.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

Save and close.

By default the app uses a theme that is available from the support library. This is not available from the core API. So we need to fix that. Open res/values/styles.xml. The style tag will look something like this:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

Change the parent to a theme that is available from the core SDK. For example:

<style name="AppTheme" parent="android:style/Theme.Holo.Light">
    <!-- Customize your theme here. -->
</style>

Rename properties in the activity xml files such as app:showAsAction to android:showAsAction.

Extent your activity classes from Activity instead of ActionBarActivity and AppCompatActivity. You'll have to press Alt+Enter on Activity once you have made the changes to add import android.app.Activity at the top of the file. See the example below:

Change:

import android.support.v7.app.ActionBarActivity;

public class DisplayMessageActivity extends ActionBarActivity {
    .
    .
    .
}

to:

import android.app.Activity;

public class DisplayMessageActivity extends Activity {
    .
    .
    .
}

And the same for any other activities that extends ActionBarActivity and AppCompatActivity

Finally, perform a Build | Clean Project and a Build | Rebuild Project to sort out the current build errors.

Donatello
  • 988
  • 7
  • 7
7

Step #1: Change your theme away from Theme.AppCompat

Step #2: Remove appcompat-v7 from your list of dependencies in your app module's build.gradle file

Step #3: Change all activities to not inherit from AppCompatActivity or ActionBarActivity, but instead inherit from something else, like Activity

Step #4: Change all menu resources, replacing app: with android:

Step #5: Do a clean rebuild (Build > Clean Project) and fix any lingering compilation errors triggered by the above four steps

Here is a sample project that uses the native action bar.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Why doesn't the template Android Studio creates come with all that? As I move on doing the android development lessons I notice a lot of things they say I should be able to call like **getActionBar().setDisplayHomeAsUpEnabled(true);**" crashes my app. Instead I need to use **getSupportActionBar().setDisplayHomeAsUpEnabled(true);** although (don't know why) I have the up option defined without having to set it up – Joaquim Ferrer Jul 10 '15 at 11:45
  • @JoaquimFerrer: "Why doesn't the template Android Studio creates come with all that?" -- actually, it does, if you create the project with a `minSdkVersion` of 22, at least in Android Studio 1.2.2. You then have to manually reset the `minSdkVersion` to your desired value. However, the behavior of the new-project/new-activity templates varies by Android Studio version. "I should be able to call like getSupportActionBar().setDisplayHomeAsUpEnabled(true); crashes my app" -- that should not compile if you are not using `appcompat-v7`. Use `getActionBar()` instead of `getSupportActionBar()`. – CommonsWare Jul 10 '15 at 11:48
  • It was the opposite functions sorry(I already edited). But yes I'm using the lattest Android Studio but in the creation of the project I made my `minSdk` 15 and not 22. Do you think I should use the appcompat libraries? – Joaquim Ferrer Jul 10 '15 at 11:53
  • @JoaquimFerrer: "Do you think I should use the appcompat libraries?" -- with a `minSdkVersion` of 15, their use is not required. Some other things in Android, like the Android Design Support Library, require you to use `appcompat-v7`. On the other hand, it has many more bugs than does the native implementation. It also forces a bit of the Material Design aesthetic on you -- your action bar on Android 4.x will not look like the native action bar. Whether this is a good thing (consistency in your app UI across versions) or not (inconsistency with other apps on the device) is up to you. – CommonsWare Jul 10 '15 at 11:56
  • Ok I will update it then to native version. As to change the theme: I tried changing the theme to ` – Joaquim Ferrer Jul 10 '15 at 12:03
  • @JoaquimFerrer: "it didn't recognize it" -- Sorry, but I do not know what either of the "it" instances in that phrase refer to. [Here is another sample project](https://github.com/commonsguy/cw-omnibus/tree/master/ActionBar/VersionedColor) where I use `@android:style/Theme.Holo` as the base for Android 4.x and I use `@android:style/Theme.Material` as the base for Android 5.0+. – CommonsWare Jul 10 '15 at 12:10
  • Android studio recognized the theme. But yes I will use that project. Thanks :) – Joaquim Ferrer Jul 10 '15 at 12:12