When the Android device set to dark mode.
But the user wants to see Light mode only on this app.
Is there any idea to handle this?
This code does not work for me
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
none of these codes is working
val config: Configuration = resources.getConfiguration()
config.uiMode = Configuration.UI_MODE_NIGHT_NO
resources.configuration.uiMode= Configuration.UI_MODE_NIGHT_NO
applicationContext.createConfigurationContext(config)
resources.updateConfiguration(config, getResources().getDisplayMetrics())
I would like to see the method too, where you set once for all your activities. But as far I know you have to set in each activity before showing any views.
For reference check this:
http://www.anddev.org/applying_a_theme_to_your_application-t817.html
Edit (copied from that forum):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Call setTheme before creation of any(!) View.
setTheme(android.R.style.Theme_Dark);
// ...
setContentView(R.layout.main);
}
Edit
If you call setTheme after super.onCreate(savedInstanceState); your activity recreated but if you call setTheme before super.onCreate(savedInstanceState); your theme will set and activity does not recreate anymore
protected void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme_Dark);
super.onCreate(savedInstanceState);
// ...
setContentView(R.layout.main);
}
Related
In one of my android App, extending Activity, I use onResume() to refresh activity in return from PreferenceActivity, such this way ...
public class ListViewWebShort extends Activity {
ListView listView;
#Override
protected void onResume() {
super.onResume();
this.onCreate(null);
}
....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
....
.. and here, App works as expected.
In another App, where I extend AppCompatActivity, the same onResume() method, makes my app crash or loop on start :
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ListView listView;
ArrayAdapter<String> adapter;
....
#Override
protected void onResume() {
super.onResume();
this.onCreate(null);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
....
If I use requestWindowFeature(Window.FEATURE_NO_TITLE); before setContentView, I see in debug that App loops in getDelegate().setContentView(layoutResID), inside AppCompactActivity.java, but if I remove it, the app FCs in the same function.
It really makes me drive crazy .. what am I missing?
Thanks in advance for any suggestion.
Don't call any activity lifecycle method directly. These methods are intended to be called from the system (inversion of control).
Especially in the super implementation of onCreate() there might happen something, that leads to crashes if they are called in the wrong order.
Try to extract the code, which you want to call in onCreate() and in onResume() in a separate method and call this method from onCreate() and onResume().
Actually in every call of onCreate, the onResume method of the application is also called.
So just move whatever code you wrote in the onCreate to onResume.
Hope it helps.
Switch keeps checked after orientation change, even if I set checked false programmatically.
Here is a example code:
public class MainActivity extends AppCompatActivity {
Switch sw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sw = (Switch)findViewById(R.id.sw);
sw.setChecked(false);
System.out.println(sw.isChecked());
}
}
The sout prints false, but the switch keeps checked on the interface. This is the switch xml element in layout file:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sw"
android:checked="false"/>
What am I missing?
The reason your checkbox is not checked on Orientation Change is that Android fires Activity methods on Orientation Change like so:
onPause -> onSave -> onStop -> onCreate -> onStart -> onResume
If you want sw.setChecked(false); to fire on Orientation Change place it in your Activities onResume() function like so:
//Other Code Above, i.e. onCreate();
#Override
public void onResume(){
super.onResume();
// Put your code here...
sw.setChecked(false);
}
There are also other options to prevent a restart of the Activity per this Android Documentation under Configuration Changes
In some special cases, you may want to bypass restarting of your
activity based on one or more types of configuration changes. This is
done with the android:configChanges attribute in its manifest. For any
types of configuration changes you say that you handle there, you will
receive a call to your current activity's
onConfigurationChanged(Configuration) method instead of being
restarted. If a configuration change involves any that you do not
handle, however, the activity will still be restarted and
onConfigurationChanged(Configuration) will not be called.
restarted.
And Finally another Option via onSaveInstanceState(Bundle savedInstanceState) and onRestoreInstanceState(Bundle savedInstanceState):
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean("YourCheckBox", enable);
super.onSaveInstanceState(savedInstanceState);
}
And retrieve it using this method:
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
boolean mychkbox = savedInstanceState.getBoolean("YourCheckBox");
}
More generally you probably want to override following so that you can preserve state like this when orientation changes (and activity re-created)
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SOME_BOOL_VALUE, boolVar);
}
and read it back then in onCreate if savedInstanceState is set
I need to execute a method before initiating the layout in an activity. If I call the method I need to execute inside onCreate(), would it be executed before the layout is set?
The reason is because I need the method to return a piece of information that is displayed in the layout before initiating it. Would love some feedback on this.
You can do whatever you like before setContentView like so:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int i = 0;
setContentView(R.layout.main);
}
}
As long as you do not interact with views that have not been inflated yet
For example this is an error:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ERROR, CAN'T TOUCH UI ELEMENTS
ImageView img = (ImageView)findViewById(R.id.img);
setContentView(R.layout.main);
}
}
Default activity created with Android Studio contains following code
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Here is code, that executed before layout inflated
setContentView(R.layout.example_activity); //This line inflates layout
}
BTW, you can even remove setContentView and inflate layout programmaticaly.
Do it in onCreate(), preferably before calling setContentView().
However, if the data you want to receive comes from the network, then it will be obtained on a separate Thread (as no network calls can be done on the main Thread). In this situation the layout will almost certainly display before the data is obtained.
A solution would be to obtain the piece of data before you start the Activity, pass it in the Intent as extra and then retrieve in onCreate() using getIntent().getStringExtra()
You are probably inflating your layout in Activity.onCreate() with setContentView(), so you need to put your function call in that method before the call to setContentView().
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
yourFunctionCallHere();
setContentView(R.layout.act_main);
}
I just created an application with single theme. Now i would like to change the theme of android on specific date. That is if user opened the application on that date the application theme should be another one.. I created 2 theme in style.xml and it works if i load
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppTheme1);
setContentView(R.layout.activity_my_home);
}
But i need to define this in every activity also. Is there a way to implement the theme change application wide based on specific date.
Make a parent Activity and call setTheme() with date checks in it's onCreate() function. Extend all other activities with the parent activity and don't forget to call super.onCreate() in the child activities that are extending the parent Activity. Like,
ParentActivity.java
public class ParentActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Date date; //Current date
if(date is 11.11.2016){
setTheme(R.style.AppTheme1);
}else setTheme(R.style.AppTheme2);
}
}
Child1Activity.java
public void Child1Activity extends ParentActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Do anything
}
}
if you have to change the theme dynamically(depends on date) , you have to add to every activity.'https://github.com/akash09766/ApplicationThemeDemo'
I'm having trouble putting my application fullscreen in Android Lollipop, my code :
public class AoA extends ActionBarActivity {
protected void oncreate(Bundle savedInstanceState) {
super.onCreate(SavedInstanceState);
setupParameters();
}
public void setupParameters() {
GameParameterSingleton.ORIENTATION = GameParameterSingleton.PORTRAIT;
GameParameterSingleton.SCREEN_HEIGHT= getWindowManager().getDefaultDisplay().getHeight();
GameParameterSingleton.SCREEN_WIDTH = getWindowManager().getDefaultDisplay().getWidth();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
The application works when I do not use fullscreen,
Can anyone help me as I leave fulllscreen ? as I take the title of the application?
It seems that in API 21
this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
Doesn't work .
Try to Add this to your AndroidManifest.xml file
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
Or add this
this.getActionBar().hide();
in onCreate method after super.onCreate(savedInstanceState);