Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Implementing Designs for Android Customizing the Login Screen Generating 9-Patch Images

Charles Li
Charles Li
15,557 Points

Need to use a Theme.AppCompat theme (or descendant) with this activity???

Hello guys, so I'm working on the "Generating 9-Patch Images" video lecture for the Ribbit app. Once I followed the instruction to delete some existing codes in the styles.xml folder, my app can't be opened.

Here is my code for the styles.xml file:

<resources>

<style name="AppTheme" parent="@style/_AppTheme"/>

<style name="_AppTheme" parent="android:Theme.Holo.Light">

    <item name="android:editTextBackground">@drawable/apptheme_edit_text_holo_light</item>

    <item name="android:buttonStyle">@style/ButtonAppTheme</item>

    <item name="android:imageButtonStyle">@style/ImageButtonAppTheme</item>

</style>


<style name="ButtonAppTheme" parent="android:Widget.Holo.Light.Button">
    <item name="android:background">@drawable/apptheme_btn_default_holo_light</item>
</style>

<style name="ImageButtonAppTheme" parent="android:Widget.Holo.Light.ImageButton">
    <item name="android:background">@drawable/apptheme_btn_default_holo_light</item>
</style>

</resources>

When I ran the app, this error appears in the logcat:

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151) at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138) at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123) at com.charlesli.ribbit.MainActivity.onCreate(MainActivity.java:171) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)             at android.app.ActivityThread.access$600(ActivityThread.java:141)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)             at android.os.Handler.dispatchMessage(Handler.java:99)             at android.os.Looper.loop(Looper.java:137)             at android.app.ActivityThread.main(ActivityThread.java:5041)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:511)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)             at dalvik.system.NativeStart.main(Native Method)

Any help is much appreciated! Thank you in advance!

Charles Li
Charles Li
15,557 Points

This is the code that I deleted:

style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"

If I don't delete this as instructed "AppTheme" is duplicated.

8 Answers

Sam McDavid
Sam McDavid
20,242 Points

Charles,

I ran into the same problem, here is how I got around the issue:

<resources>

    <style name="ButtonAppTheme" parent="android:Widget.Holo.Light.Button">
        <item name="android:background">@drawable/apptheme_btn_default_holo_light</item>
    </style>

    <style name="ImageButtonAppTheme" parent="android:Widget.Holo.Light.ImageButton">
        <item name="android:background">@drawable/apptheme_btn_default_holo_light</item>
    </style>

    <style name="AppTheme" parent="@style/_AppTheme"/>

 <!--   <style name="_AppTheme" parent="android:Theme.Holo.Light"> -->
    <style name="_AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

        <item name="android:editTextBackground">@drawable/apptheme_edit_text_holo_light</item>

        <item name="android:buttonStyle">@style/ButtonAppTheme</item>

        <item name="android:imageButtonStyle">@style/ImageButtonAppTheme</item>

    </style>

</resources>
Sam McDavid
Sam McDavid
20,242 Points

However, you may also want to look through your code and see which classes are importing an AppCompat class. I know that from completing the previous project that a few of my classes were and was able to clean them up but I guess that I still have some floating around.

Charles Li
Charles Li
15,557 Points

Thank you so much Sam! The problem goes away after I used your solution! There is however one more problem, my button is now in purple but the "underline" of my editText is still the default blue instead of purple. Is this the case for you as well? I checked the files and the image that the underline referred to is indeed purple so I'm not sure what's going on here. Thanks again!!!

Sam McDavid
Sam McDavid
20,242 Points

Yes, the button should be purple now. The underline should be purple as well though. Don't know what is happening there.

In my 4.4.4 emulator (Genymotion Galaxy S5) the under bars look like a bold black line.

In my 5.0.0 emulator (Genymotion Nexus 5) the under bars look correct.

Sam McDavid
Sam McDavid
20,242 Points

As a side note, the under bar may be blue in the preview since the previews in Android Studio still use the Holo themes, but when you run it in an emulator it should be purple.

Charles Li
Charles Li
15,557 Points

Ya I really don't know what's going on here. Somehow it works for the buttons but not for the under bars... I tried it on different emulators and on my own device too but the problem remains.

All the values seem to be referring to the right file when I checked. Hopefully someone else can figure out this problem if they do run into it as well.

Just want to say thank you so much again for your time! A blue under bar is something I can live with for now lol I'm just going to progress through the rest of this course and hopefully I'll get an answer before the completion of this course. Thanks again Sam!

Charles Li
Charles Li
15,557 Points

Sam! I found a solution! Not sure if this is the best solutions but here's the link/solution that works for me. (http://stackoverflow.com/questions/21480398/edittext-underline-below-text-property)

Sam McDavid
Sam McDavid
20,242 Points

Awesome! I was just having some issues with a later badge with changing the color of the ActionBar and such. It looks like I was extending the wrong class in my MainActivity.

I just downloaded the version of the file as of that lesson and just worked through the issues that I was having and was able to resolve the AppCompat issue altogether so that I could use the Holo themes.

Charles Li
Charles Li
15,557 Points

Hi Sam, I think I ran into the same problem as you now LOL I'm on the changing the color of Action Bar lecture too and I'm wondering how did you fix your problem.

I have the same error as last time Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

How did you resolve the AppCompat issue? and thanks!

Sam McDavid
Sam McDavid
20,242 Points

I just downloaded the project files from that video and compared my java classes to them to find the differences and make mine match. I think that is really the only way that it is going to work.

George Pazdral
George Pazdral
2,972 Points

I changed it to ''' parent="Theme.AppCompat.Light.DarkActionBar" ''', but nothing changes...no purple box, no underline, nothing. Not sure what I'm doing wrong. Any guidance would be much appreciated. Not sure if I need to change something about extends AppCompatActivity..

Jeremy Jiang
Jeremy Jiang
3,075 Points

I encountered this issue today , but resolved now! If you want to use ActionBarActivity, you must use AppCompat theme, so the solution is change MainActivity extends FragmentActivity, Change SignInActivity, SignUpActivity extends Activity instead of extends ActionBarActivity.

FYI: https://developer.android.com/guide/topics/ui/actionbar.html On API level 11 or higher: The action bar is included in all activities that use the Theme.Holo theme (or one of its descendants), which is the default theme when either the targetSdkVersion or minSdkVersion attribute is set to "11" or higher. If you don't want the action bar for an activity, set the activity theme to Theme.Holo.NoActionBar.

Josh Gold
Josh Gold
12,207 Points

This is what worked for me:

"All you need to do is add android:theme="@style/Theme.AppCompat.Light" to your application tag in the AndroidManifest.xml file."

http://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity

Great discussion. It was extremely helpful! Thank you guys, now I´m wondering why we needed to add:


<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/EditTextCustomHolo"/>


To make the edit Text show the right color, but it wasnt needed for the Button.

Benoit Plante
Benoit Plante
6,474 Points

Thanks to you 2, I got the same issue, and your discussion was very helpful!

Charles Li
Charles Li
15,557 Points

Glad our discussion could help you out!

<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:id="@+id/passwordField" android:hint="@string/password_hint" android:textColorHint="@color/light_gray" android:textSize="17sp" style="@style/EditTextCustomHolo" />

For the edit Text: android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:id="@+id/passwordField" android:hint="@string/password_hint" android:textColorHint="@color/light_gray" android:textSize="17sp" style="@style/EditTextCustomHolo"

Marcelino Yax
Marcelino Yax
10,966 Points

Solving "underline" color of the editText in "Generating 9-Patch Images" video lecture. If the "underline" of the editText does not turn purple when running in the genymotion emulator for Android 5.0 (API level 21), you have to copy all values from the styles.xml file under "values" folder, and paste them in the styles.xml file under "values-v21" folder. Android 5.0 (API level 21) uses that "values-v21/style.xml" file as alternative layout files. Use the same code given in the video lecture, don't change anything. Do not change <style name="_AppTheme" parent="android:Theme.Holo.Light"> to <style name="_AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">.