0

I found topic with instruction - how to set theme for whole application:

Setting global styles for Views in Android

It does not work for me at all. Here's my code:

style.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <!--1-->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:radioButtonStyle">@style/TBRadioButtonStyle</item>
    </style>

    <style name="TBRadioButtonStyle" parent="android:Widget.CompoundButton.RadioButton">
        <item name="android:textColor">#F00</item>
        <item name="android:textStyle">bold</item>
        <item name="android:background">@drawable/state_color_selector</item>
    </style>

</resources>

state_color_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true">
            <color android:color="#F00" />
        </item>
        <item android:state_checked="false">
            <color android:color="#0F0" />
        </item>
    </selector>

AndroidManifest.xml:

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

        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

        <application
            android:name=".controller.ApplicationController"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".view.DrawersWidget"
                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>

.java file with RadioButton creation:

RadioButton newButton = new RadioButton(ApplicationController.INSTANCE.getApplicationContext());
//...
newButton.setLayoutParams(new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
newButton.setButtonDrawable(new BitmapDrawable(ApplicationController.INSTANCE.getResources(), filteredBitmap));
newButton.setText(someText);
Community
  • 1
  • 1
Mariusz Jaskółka
  • 4,137
  • 2
  • 21
  • 47

1 Answers1

1

When you create the RadioButton, use the Context of the Activity, instead of the ApplicationContext.

Mimmo Grottoli
  • 5,758
  • 2
  • 17
  • 27
  • `java.lang.RuntimeException: Unable to start activity ComponentInfo{my.app/my.Activity}: android.content.res.Resources$NotFoundException: File from drawable resource ID #0x0` occured on line `RadioButton newButton = new RadioButton(getActivity());` – Mariusz Jaskółka Jul 24 '15 at 13:56
  • It seems like there is some error connected with `` tag, as error disappears as son as I remove it. – Mariusz Jaskółka Jul 24 '15 at 14:23
  • I've never used a color as a drawable ``. The #F00 should be replaced with a real drawable? And I've never used a selector inside an item of a theme. Better to add the selector as drawable too? – Mimmo Grottoli Jul 24 '15 at 14:27
  • The same error after changing `drawable` to `color`. What does it mean to "add the selector as drawable"? – Mariusz Jaskółka Jul 24 '15 at 14:39
  • Inside the res/drawable folder create a my_selector_drawable.xml. Then copy in that file the content of your selector. In your theme reference the drawable as @drawable/my_selector_drawable – Mimmo Grottoli Jul 24 '15 at 14:43
  • Thanks, I've updated my question. Your solution works like charm now. – Mariusz Jaskółka Jul 24 '15 at 15:19