3

I would like to change the whole application's text and background color in Java, is this possible? With this I mean to change the color of every item in the application (TextViews, ListView items, everything).

Is this possible?

I have tried using a custom-made style but I can't make it work. Here is the xml file (put in the res/layout/values folder):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Light">
        <item name="android:textColor">#00FF00</item>
    </style>
</resources>

Let's say I just want to change the text color for now.

Now I call this style in my application like this:

public void onCreate(Bundle icicle) {

    Activity.setTheme(android.R.style.light);
    super.onCreate(icicle);
    setContentView(R.layout.main);

But I get the error light cannot be resolved or is not a field.

Update:

One way I found to do this programmatically is to restart the activity, calling

this.setTheme(R.style.Light);
onCreate(null);

However, this works only for the current activity and not for the whole application. It would be great if it were possible to do this launching another activity, not only the current one.

user1301428
  • 1,743
  • 3
  • 25
  • 57

3 Answers3

1

You're trying it in a bit to simple way. Like this you're just adjusting your general Activity's background instead of all the different Views that are out there.

In order to try and adjust every type of View (Button, TextView etc) you'll need to address all their own styles to overwrite them.

Per example if you want to adjust Button you'll need in your own general style:

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

This will point at your own custom style, which takes its parent from the Android's standard Button.

<style name="ButtonHoloDark" parent="android:style/Widget.Button">
    <item name="android:background">@drawable/btn_default_holo_dark</item>
    <item name="android:textColor">#ffffff</item>
</style>

Be warned, doing this for every View will take you quite some themes and styles.

You can find a great example how to do this exactly in theHoloEverywhere lib, which basically does the same for creating a holo theme backported to Android 2.2 or so

Finally, drop the Activity.setTheme(android.R.style.light); stuff, and just set your own theme via the manifest.

Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
  • This is right but as the question asked, he wanted to do this using java code. have you any idea about that – Narendra Pal Jan 11 '13 at 11:16
  • Unfortunately I need to change the theme via Java code, after an activity has started. Any idea how to do this? – user1301428 Jan 11 '13 at 11:17
  • Hmm I'm sorry I didn't know you actually needed to change it in Java code. I thought you simply thought this would be your only option left since your xml implementation didn't work. – Stefan de Bruijn Jan 11 '13 at 11:19
  • @user1301428 Can you tell me why you want to go with the java code,if you have the better solution using xml. and even if you will go through the java code,so you cannot change the whole application theme. you have to chage it in every activity. as per my knowledge – Narendra Pal Jan 11 '13 at 11:21
  • Basically you can still just make the styles in XML like this, and then call them in your code with this.setTheme(R.style.myTheme); But i'm not sure why you're so set on doing it in your java code. – Stefan de Bruijn Jan 11 '13 at 11:23
  • @NarendraPal Because I have a list with some styles that I want the user to choose from, so depending on it's choice I need to change the app's theme dynamically – user1301428 Jan 11 '13 at 11:23
  • You can find a good example in the ActionBarSherlock demo's app on how to do this - in that app you've got a nearly constant option to switch theme. – Stefan de Bruijn Jan 11 '13 at 11:24
  • @StefandeBruijn the fact is that using setTheme after an activity has started doesn't work... – user1301428 Jan 11 '13 at 11:24
  • Okay. so you can check for this link. http://stackoverflow.com/a/2483001/1395259 – Narendra Pal Jan 11 '13 at 11:27
  • You're right that changing Theme after starting an activity started doesn't work, but it won't ever :) The closest you can get is to quickly reboot the activity with a flag to clear out backstack (so you won't go back through your themes with the back button) and set the correct theme on the new activity. The app 'Tasks' handles this by adding a nice Activity animation so it doesn't comes over to the user like its a defect. – Stefan de Bruijn Jan 11 '13 at 11:32
1

Ok so I found one possible solution, which is to pass the theme information between the activities using intents and the putExtra method.

Code for the first activity (the caller):

Intent i = new Intent(this, ActivityToCall.class);
i.putExtra("key", R.style.Light);
startActivity(i);

Code for the second activity (the called one):

public void onCreate(Bundle icicle) {
    int theme = getIntent().getIntExtra("key",-1);
    this.setTheme(theme);
    super.onCreate(icicle);
    // other code...

I don't know if it's the best possible approach but at least it works.

user1301428
  • 1,743
  • 3
  • 25
  • 57
0

The attributes you want change are:

<style name="AppTheme" parent="android:style/Theme.Holo">
   <item name="android:textColorPrimary">...</item>
   <item name="android:textColorSecondary">...</item>
</style>

There are a few other ways to change them: Please Refer to this information

You can then set this Theme via the Manifest or setTheme(R.style.AppTheme) in your Activity's onCreate(...)

Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36
Chris Banes
  • 31,763
  • 16
  • 59
  • 50