Search code examples
javaandroidxmlandroid-theme

unable to change theme in android studio


Tried making a simple overflow application with three buttons in overflow menu each signifying three different background colours for the activity_main.xml but each time i try to change the theme of the app.. the app crashes when started EMULATOR SHOWS UNFORTUNATELY APP STOPPED

I want to make the theme of the app Theme.Holo or anything that shows the overflow menu button

The app only works with the following edits in below codes:

manifest.xml file

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

and

style.xml file

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

in this case the app window THE OVERFLOW BUTTON IS NOT VISIBLE

any changes in the above two CODES (EXAMPLE AS THE CODES GIVEN AT THE END OF QUESTION SHOWS ERROR) makes the app to show the error:UNFORTUNATELY OVERFLOW STOPPED while opening the app

Theres a similar problem discussed in Changing Theme.Holo on Android Manifest.xml file doesn't run on Android

also i read https://developer.android.com/training/basics/actionbar/styling.html

but i couldnt understand the solution...

ALL THE APP FILES ARE COPIED BELOW.. THE BELOW PROGRAM GIVES ERROR BECAUSE manifest and style FILES ARE EDITED

MainActivity.java

package com.example.user.overflow;

import android.content.ClipData;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    RelativeLayout main_view = (RelativeLayout)     findViewById(R.id.main_view);

    switch (item.getItemId()){
        case R.id.menu_red:
            if(item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);
            main_view.setBackgroundColor(Color.RED);
            return true;
        case R.id.menu_green:
            if(item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);
            main_view.setBackgroundColor(Color.GREEN);
            return true;
        case R.id.menu_yellow:
            if(item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);
            main_view.setBackgroundColor(Color.YELLOW);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/main_view">

<TextView android:text="@string/hello_world"     android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

strings.xml

<resources>
<string name="app_name">Overflow</string>

<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="red_string">red</string>
<string name="green_string">green</string>
<string name="yellow_string">yellow</string>
</resources>

styles.xml

<resources>

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

</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.overflow" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/CustomActionBarTheme" >
    <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>

</manifest>

themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
    parent="android:Theme.Holo">

</style>

<!-- ActionBar styles -->

</resources>

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"    tools:context=".MainActivity">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/menu_red"
        android:orderInCategory="1"
        app:showAsAction="never"
        android:title="@string/red_string" />
    <item
        android:id="@+id/menu_green"
        android:orderInCategory="2"
        app:showAsAction="never"
        android:title="@string/green_string" />
    <item
        android:id="@+id/menu_yellow"
        android:orderInCategory="3"
        app:showAsAction="never"
        android:title="@string/yellow_string" />
</group>

</menu>

logcat errors:

03-27 16:54:09.004      817-817/com.example.user.overflow E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.overflow/com.example.user.overflow.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        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)
 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.example.user.overflow.MainActivity.onCreate(MainActivity.java:16)
        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)

Solution

  • You are using this theme for all your activity

    android:theme="@style/CustomActionBarTheme" 
    

    which is a holo theme.

    Try changing into to

    android:theme="@style/AppTheme" 
    

    for a particular activity