-2

I'm completely stuck(

UPD. Can it be possible to set different backgrounds (or other attributes) for separate Views by applying a single theme?

How should be applyed different styles for different views in different themes? Briefly, I'd like to change themes, depend on the time of the day, to change view's attributes (color, background etc) accordingly.

Profi, any hint would be greatly appretiated.

  • Whats the current issue you are facing? – Sanoop Surendran Sep 27 '16 at 10:27
  • It's not an issue until I do not know what to do. It's request for help. Matter is complicated, I neither can formulate a correct question nor find something intelligible on the Internet. – Sergey Sergheyev Sep 27 '16 at 10:50
  • All I want is just to change Theme and get view's attribute(-s) set with another value. – Sergey Sergheyev Sep 27 '16 at 10:58
  • Check [this](https://www.google.co.in/search?client=opera&q=android+change+theme+runtime&sourceid=opera&ie=UTF-8&oe=UTF-8) for changing theme and for checking phone time start a [Service](http://www.tutorialspoint.com/android/android_services.htm) – Sanoop Surendran Sep 27 '16 at 12:02
  • Thank you for your patience in understanding my problem, but to change themes it's not a thing I'm stuck. Look here: TextView_1 has Color.RED and TextView_2 has Color.GREEN set by Theme_1. After implementing method setTheme(Theme_2), I want to be TextViev_1 set with Color.GREEN and TextView_2 set with Color.RED And so on... And more then that - how is it possible at all to define two different colors for two different TextViews in XML style resourse in one Theme? Do I need to create custom "items" with different names for each Views? Comrades, I'm completely lost at the moment...(( – Sergey Sergheyev Sep 27 '16 at 12:38

2 Answers2

0

U should do switch statement like:

        int theme;
        String color;

     switch (theme) {
                case 1:  color = redandblue;
    //code
                         break;
                case 2:  color = greenanddark;
    //code
                         break;
                case 3:  color = redandwhite;
    //code
                         break;

                default: color = standard;
    //default mean that this will be call when any upper not call 
                         break;

code mean ur preferences with colors for view

also u can do this in if else

if(theme == 1){
//code
}else if(theme == 2){
//code
}else if(theme == 3){
//code}
...

and the last is configuring themes with methods

private void configureFirstTheme(){
//code
}

private void configureSecondTheme(){
//code
}

U can add constructor to method like

private void themeSetup(int theme){
//here the switch statement
}

hope u will choose something

Rodriquez
  • 981
  • 1
  • 7
  • 21
  • So it's not possibly to change View's attributes by just aplying an another theme? "Switchcasing" seems an quite ugly mechanism to do this. And if there is more then two theme and a lot of customized views it's demand a lot of handcoding(( Seems sad – Sergey Sergheyev Sep 28 '16 at 06:15
  • possible. also u can make the if else statement and making the methods for every configuration of your layouts. U have so many ways to do this task – Rodriquez Sep 28 '16 at 06:19
  • So pls give me the exact way from "many" to do this correctly. Or provide a link to the resourse to read about. Thx in advance – Sergey Sergheyev Sep 28 '16 at 06:27
  • look at edditing answer. Every way is correct. I prefer the switch statement, but what u prefer is ur choise – Rodriquez Sep 28 '16 at 06:32
  • Thx Rodriquez for your time! – Sergey Sergheyev Sep 28 '16 at 12:16
0

Finally I've found a solution which is after my own heart! I suspected that these hardcoded methods are not very flexible solutions and need multiplexing code depends on the quantity of themes implemented. For thouse who faced the same quest pls have a look at the link

Declare attribute to use as style reference:

<declare-styleable name="CustomThemeViews">
<attr name="myViewStyle" format="reference"/>
</declare-styleable>     Define styles for each theme:

<style name="myViewStyleTheme01">
<item name="android:background">#f00</item>
</style>

<style name="myViewStyleTheme02">
<item name="android:background">#0f0</item>
</style>     Define themes that refer to different styles:

<style name="AppTheme" parent="android:Theme.Light">
<item name="myViewStyle">@style/myViewStyleTheme01</item>
</style>

<style name="AppThemeAlternative" parent="android:Theme.Light">
<item name="myViewStyle">@style/myViewStyleTheme02</item>
</style>     Use themed styles in layout

<ListView style="?attr/myViewStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"    
...     Now the style applied to the ListView changes according to the theme applied.
Community
  • 1
  • 1