Setting DayNight theme using AppCompatDelegate.setDefaultNightMode() not working for FragmentActivity - android

I'm a bit puzzled. I'm using a DayNight theme in my app (using AppCompatDelegate.setDefaultNightMode()), but can't get it to work in my MainActivity.
The MainActivity (which extends FragmentActivity) looks like it's never set to dark theme - it always remains in light theme.
I tried setting the theme directly in my MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
super.onCreate(savedInstanceState);
// create main activity.
}
But this is not working.
I have set all of the colors in my layout files properly using ?attr/colorReference. Does anyone know what is going wrong here?
EDIT:
My styles.xml is as follows:
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!--Default typeface and colors:-->
<item name="android:typeface">monospace</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorAccentDarker">#color/colorAccentDarker</item>
<item name="colorAccentDarker_80percent">#color/colorAccentDarker_80percent</item>
<!--Show people's own wallpaper background-->
<item name="android:windowShowWallpaper">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>

MainActivity (which extends FragmentActivity)
FragmentActivity has no idea of AppCompat. AppCompatDelegate is only used by AppCompatActivity or you have to wire it manually to your other activities.
You can extend AppCompatActivity instead of FragmentActivity.

Related

How to have a new activity start with the action bar not visible using a theme?

Why doesn't this style I have result in the action bar not being displayed?
My activity is descended from appcompat. I assume these are all the correct properties.
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="Theme.AppCompat.Light">
<item name="android:windowFullscreen">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
it is taking effect, cause the status bar gets removed. But the action bar doesn't seem to get affected.
I can still successfully remove the action bar like this:
public class MainMenuScreen extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main_menu_screen);
but i have to admit I'm a little bit disappointed that I can't do it from xaml.
Can you show your manifest? Where do you associate the theme with the activity?
Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="BaseTheme" />
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/cobalt_light</item>
<item name="colorPrimaryDark">#color/cobalt</item>
<item name="colorControlHighlight">#color/cobalt_lighter</item>
<item name="colorAccent">#color/cobalt_light</item>
</style>
</resources>
You dont need to extends from parent Theme.AppCompat.Light

How to Switch theme for an activity

So here is the situation:
I have DogActivity and FavoritesActivity. DogActivity is just a ListView. When you click on a Dog in the list, it takes you to FavoritesActivity.
I want to have a number of themes ready to go. They don’t need to be dynamically generated. They can already exist in XML form.
Depending on which dog the user selects from the list, I want to have the FavoritesActivity shown in one of my pre-existing themes.
I hear talks about ContextWrapper, but I am not sure how to apply it. Any thoughts on how I may accomplish this?
Details:
Here is the usual single theme:
for v21/styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorControlHighlight">#color/colorAccentLight</item>
<item name="android:colorControlNormal">#color/colorAccent</item>
<item name="android:itemTextAppearance">#style/AppTheme.itemTextStyle</item>
<item name="popupMenuStyle">#style/PopupMenu.MyAppTheme</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:colorControlHighlight">#color/colorAccentLight</item>
</style>
<style name="AppTheme.itemTextStyle" parent="#android:style/TextAppearance.Widget.IconMenu.Item">
<item name="android:textColor">#color/colorPrimary</item>
</style>
</resources>
for styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Want I want to do:
Essentially I just want to change the colorPrimary, colorPrimaryDark and colorAccent on the fly and have all the styles and themes and XML layouts that use them to change. So if I can change those colors before I launch FavoritesActivity then that would solve my problems.
You can just send the dog type as an Intent extra, and then use the setTheme() method to set the appropriate Theme.
For this example, suppose you just have two Themes:
<style name="AppThemeOne" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppThemeTwo" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimaryCustom</item>
<item name="colorPrimaryDark">#color/colorPrimaryDarkCustom</item>
<item name="colorAccent">#color/colorAccentCustom</item>
</style>
Then, in DogActivity, set an Intent Extra to the Dog type the user selected from the ListView:
Intent intent = new Intent(DogActivity.this, FavoritesActivity.class);
intent.putExtra("dog_type", "terrier");
startActivity(intent);
Then, in FavoritesActivity, load the correct Theme:
#Override
protected void onCreate(Bundle savedInstanceState) {
String dogType = getIntent().getStringExtra("dog_type");
if (dogType.equals("terrier")) {
setTheme(R.style.AppThemeOne);
} else {
setTheme(R.style.AppThemeTwo);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites_layout);
//.....
}
I've accomplish it quite simply on my latest project, you just have to set the values on the theme via Java. Like the following code:
public class FavoritesActivity extends AppCompatActivity { // it can be Activity too
#Override
public void onCreate(Bundle savedInstanceState) {
if( ... check condition to change theme ) {
// this will replace every value from FavoritesActivity theme by the
// the values on `other_style` theme.
getTheme().applyStyle(R.style.other_style, true);
}
// call super AFTER applying the theme
super.onCreate(savedInstanceState);
.. carry on your normal stuff
}
that's very useful because you can very easily replace just a few values and keep the rest as the original, or change everything from the original. It all depends what argument you pass to the applyTheme method.
Also is great you don't have to mock with ContextThemeWrapper. The values are there on the theme and that's it.
https://developer.android.com/reference/android/content/res/Resources.Theme.html#applyStyle(int, boolean)
You can use this.recreate() method for this.
Based on https://stackoverflow.com/a/29722976/7547681 answer.

Android: Applying theme directly to CheckBox breaks 'android:onClick'

I've been working on this app for a while, and had zero issue with android:onClick's linking up with the relevant method in the activity.
...Then I started messing with themes and my CheckBox onClick's began breaking if I apply the theme directly to them with a 'android:theme="#style/CheckBoxTheme"', to the tune of this:
java.lang.IllegalStateException: Could not find a method onLinkCheckboxClicked(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.support.v7.widget.AppCompatCheckBox with id 'chx_sub_all'
If I DON'T use android:theme on the checkBoxes and just let the RF_AppTheme do it's thing, I can alter the checkbox with android:buttonTint just fine. onClicks work no problem. (The reason I'm splitting this into it's own specific theme is because I want to support a lower version, so I have a 'value/styles.xml' that just changes the background color, and a 'values-r21/styles.xml' that uses buttonTint which is version 21+ )
From what I can tell, the theme being used is changing the version of CheckBox, so that it tries to look at an activity that isn't my MainActivity, which doesn't exist.
My theme is literally a copy/paste of the theme generated with the project, with more things added to it:
<resources>
<style name="RF_AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:background">#color/colorBackground</item>
<item name="android:textSize">#dimen/text_size</item>
<item name="android:textColor">#color/colorTextNormal</item>
<item name="android:buttonTint">#color/colorPrimaryLight</item>
</style>
<style name="CheckBoxTheme" parent="RF_AppTheme">
<item name="android:buttonTint">#color/colorPrimaryLight</item>
<item name="android:gravity">center_vertical</item>
</style>
</resources>
This is the general idea of what my MainActivity looks like, without all the other code copy/pasted over.
public class MainActivity extends AppCompatActivity {
//onStart and the like above
public void onLinkCheckboxClicked(View view)
{
//doing checkbox things here, which never gets called
}
}
and this is one of my checkboxes
<CheckBox android:id="#+id/chx_sub_all"
android:theme="#style/CheckBoxTheme"
android:text="#string/check_all"
android:textColor="#color/colorTextNormal"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onLinkCheckboxClicked"/>

Action bar looks cut while using Theme.AppCompat.Dialog

I wished to make one of my activities to look like a dialog and used a Theme.AppCompat.Dialog theme for it, but it made it's action bar to look bad (see below).
Now background is cut to the length of the title string and I cant't find any theme property to fix it.(
What can be done to avoid it?
Related part of styles.xml:
<style name="DeviceListTheme" parent="Theme.AppCompat.Dialog">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
I start the activity using the following code:
Intent intent = new Intent(this, DeviceListActivity.class);
startActivityForResult(intent, REQUEST_CONNECT_DEVICE);
First, when I encountered this problem, I tried using supportRequestWindowFeature(Window.FEATURE_NO_TITLE); but this didn't work for me and had no effect.
The alternative method to remove the bar at the top of your dialog activity would be to create a custom style and apply it to that activity.
In styles.xml, create a new style like so:
<style name="MyCustomDialog" parent="Base.Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
Now, in AndroidManifest.xml, add in android:theme="#style/MyCustomDialog" to your activity.
Of course, MyCustomDialog can be renamed to anything you want.
Use DialogWhenLarge instead of standard Dialog style:
<style name="MyDialogTheme" parent="#style/Theme.AppCompat.Light.DialogWhenLarge">
...
</style>
I solved the same problem this way:
Activity:
public class MyActivity extends android.support.v4.app.FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
setTitle("View task");
}
}
Theme:
<style name="Theme.MyDialog" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
Manifest:
<activity android:name=".MyActivity" android:theme="#style/Theme.MyDialog"/>
Result:
Its Working
<style name="AppThemeDialog" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:spinnerStyle">#style/holoSpinner</item>
</style>
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
setTitle("Batches");
}

android mobile theme developing

I want to develop a theme for android mobile here the code which i found some where in site but it doesn't work.
My code for XML in /res/values/styles.xml.
<resources>
<!-- Base application theme is the default theme. -->
<style name="Theme" parent="android:Theme">
</style>
<!-- Variation on our application theme that has a translucent
background. -->
<style name="Theme.Translucent">
<item name="android:windowBackground">#drawable/translucent_background</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#fff</item>
</style>
<!-- Variation on our application theme that has a transparent
background; this example completely removes the background,
allowing the activity to decide how to composite. -->
<style name="Theme.Transparent">
<item name="android:windowBackground">#drawable/transparent_background</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#fff</item>
</style>
<style name="TextAppearance.Theme.PlainText" parent="android:TextAppearance.Theme">
<item name="android:textStyle">normal</item>
</style>
</resources>
WidgetActivity:
public class WidgetActivity extends Activity {
#Override protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
Log.i(Global.TAG2, "sub-Activity WidgetActivity being creation: start");
setTheme(R.style.Theme_Translucent);
setContentView(Panel2Builder.createWidgetPanel(this));
Log.i(Global.TAG2, "sub-Activity WidgetActivity being creation: end");
}
}//end class WidgetActivity
this is not work as i expected.
can any one have idea to work on theme for android and make it by developing.

Categories

Resources