0

In my Android app. Some screens are "darkish" themed. So i need to apply a different colorAccent colorPrimary colorDark on them.

Per article - https://plus.google.com/+AndroidDevelopers/posts/JXHKyhsWHAH - we can set android:theme and it and the descendents will get this.

So I have created multiple themes in my styles.xml, and am now trying to apply this to different Views or TextInputs per this solution - https://stackoverflow.com/a/49172034/1828637

My goal (psuedo code)

In my styles.xml I have created two different themes, one for the dark screens, and one for the light screens:

<style name="EditTextTheme1" parent="ThemeOverlay.AppCompat.Light">
  <item name="colorPrimary">@color/cardview_dark_background</item>
  <item name="colorPrimaryDark">@color/colorPrimary</item>
  <item name="colorAccent">@color/colorAccent1</item>
</style>

<style name="SecondTheme" parent="ThemeOverlay.AppCompat.Dark">
  <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorAccent</item>
  <item name="colorAccent">@color/colorAccent2</item>
</style>

My goal is to now do this when on dark screen

<TextInput android:theme="EditTextTheme1" />

And then when on light screens I want to do this:

<TextInput android:theme="SecondTheme" />

Or apply it via a View like this:

<View android:theme="EditTextTheme1">
      <TextInput />
</View>

<View android:theme="SecondTheme">
      <TextInput />
</View>

What I tried

I have not been able to figure out how to apply android:theme. I even tried modifying native code - Modifying ReactEditText.java is not taking affect . It seems View and TextInput etc do not have this android:theme prop. Does anyone know if there is a way to apply android:theme to a View so its children take this custom theme?

The closest I found was I found a ThemeAttrAndroid property in TouchableNativeFeedback - https://github.com/facebook/react-native/blob/b7bb2e5745f2bdbfeeccef8d97d469730942e01c/Libraries/Components/Touchable/TouchableNativeFeedback.android.js#L108 - but I can't figure out how to use this to style descendents. I feel I'm very close.

Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • Did you set your theme on Manifest ?? In ```Manifest```, ``` – TyeolRik Mar 22 '18 at 07:36
  • @TyeolRik yes I set custom themes in `styles.xml` like this - https://stackoverflow.com/a/49172034/1828637 - however I need to apply these custom themes to different text input elements. – Noitidart Mar 22 '18 at 07:37
  • I am not sure what your "Final" Goal (Sorry for lack English). Do you want to set 2 EditText which are different theme? (I mean, you have 2 EditText and both have different Cursor color, line or something which has relationship with theme) – TyeolRik Mar 22 '18 at 07:43
  • No problem @TyeolRik I really appreciate it. I have created two themes. I will add it to the topic and let you know. – Noitidart Mar 22 '18 at 07:44
  • @TyeolRik I added to the topic "my goal" section. And "what i tried" section. – Noitidart Mar 22 '18 at 07:47
  • 1
    [https://stackoverflow.com/a/35733171/7105963](https://stackoverflow.com/a/35733171/7105963) How about watching this article? You can use ```getTheme().applyStyle(R.style.Theme_AppCompat_, true);```. Actually, I am developing **Theme.AppCompat.DayNight** which has 2 modes and I am set modes programtically within 1 line. I am not sure whether this is "Right solution", But I think this could be an solution. – TyeolRik Mar 22 '18 at 07:55
  • Thanks @TyeolRik - but doesn't this change the theme for the whole app? I need the whole app to be a one theme permanently. I just need some "TextInput"s to use a different theme. – Noitidart Mar 22 '18 at 09:06
  • 1
    Sorry for late comments (because of work :/) I don't know how to make but, there is **Theme.AppCompat.DayNight** which has 2 themes Light theme and Night theme. And you can switch it by using [this link](https://stackoverflow.com/a/35669650/7105963). – TyeolRik Mar 23 '18 at 00:04
  • 1
    This link means you can change whole app theme (Actually not changing theme but switching 2 modes. so User thinks "Oh, theme could be changed with just 1 button".) ```getDelegate().applyDayNight();``` – TyeolRik Mar 23 '18 at 00:07
  • Thanks @TyeolRik - i actually can't use that method, because on one screen I need to have some methods that are using "dark" theme and some elements that are using "light" theme. – Noitidart Mar 23 '18 at 00:46

0 Answers0