How to display Splash Screen in android - android

I want to display splash screen in android app. But I want to execute onCreate() method of MainActivity behind the splash screen. because i am doing huge work in this method. Anyone can tel me how to do that.

Basically you want to do some work in background while user is shown some splash screen, right ? What you need is an Async Task or a Loader kind of thing.
Step 1: Display the splash screen.
Step 2: Start an Async task and do all your heavy processing in the doInBackground method of Async Task
Step 3: Update the UI using the onPostExecute method of Async Task. In this method, first close the timer to splash screen. Then send the intent to start another screen with the data of the heavy processed result of Async task. Display it on UI thread.
Only Showing Splash screen is very simple. This code creates a splash screen of 3 seconds and then sends an Intent to another activity.
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
CountDownTimer cdt1 = new CountDownTimer(3000, 1000) {
Boolean checkInternetConnection = false;
#Override
public void onTick(long l) {
}
#Override
public void onFinish() {
//Send Intent here
Intent i = new Intent(getApplicationContext(),
anotherActivity.class);
startActivity(i);
}
}.start();
}
PS- Dont forget to make the activity with this code as launcher activity from manifest file.

You can try this for Splash Screen...
public class SplashScreen extends Activity {
//Further Needed Declarations
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
/**
* Showing splashscreen while making network calls to download necessary
* data before launching the app Will use AsyncTask to make http call
*/
new PrefetchData().execute();
}
}

This is exactly the same way you wanted it.
create file in drawable
<item
android:drawable="#color/gray"/>
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/ic_launcher"/>
</item>
</layer-list>
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
</resources>
Activity:
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}

You can add this in your onCreate Method
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// going to next activity
Intent i=new Intent(SplashScreenActivity.this,MainActivity.class);
startActivity(i);
finish();
}
},time);
And initialize your time value in milliseconds as yo want...
private static int time=5000;
for more detail download full code from this link...
https://github.com/Mr-Perfectt/Splash-Screen

Related

How to add real Loading Screen

I have an android application of a pdf file inside. When I open my application on lower configuration devices (galaxy 2 etc.), it takes a while to load the PDF file. I tried to adding a splash screen in my application but when opening MainActivity.class after SplashScreen still taking same time. How can I develop a real loading screen? It should work on top of Main Activity layer.
Here my codes:
AndroidManifest.xml
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
</application>
</manifest>
SplashScreen.class
public class SplashScreen extends AppCompatActivity {
private GifImageView gifImageView;
private ProgressBar progressBar;
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
gifImageView=(GifImageView)findViewById(R.id.gifview);
progressBar=(ProgressBar)findViewById(R.id.progressbar);
progressBar.setVisibility(progressBar.VISIBLE);
try {
InputStream inputStream = getAssets().open("loading.gif");
byte[] bytes = IOUtils.toByteArray(inputStream);
gifImageView.setBytes(bytes);
gifImageView.startAnimation();
}
catch (IOException ex)
{
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = newIntent(SplashScreen.this,MainActivity.class);
SplashScreen.this.startActivity(intent);
SplashScreen.this.finish();
}
},12000);
}
}
Here my codes how I can solve this problem?
Have you considered using Async Task for this?
You can use the async task in your MainActivity.
For Example you can do something like the following:
class PDFLoader extends AsyncTask<Void, Void, Void>
{
protected void onPreExecute (){
super.onPreExecute();
// Here you can instantiate the Progressdialog to show the progress.
}
protected String doInBackground(Void...arg0) {
// here you can go ahead write the reading logic for the PDF
// if it is taking time.
}
protected void onProgressUpdate(Integer...a){
super.onProgressUpdate(a);
// you may or may not use this. This can act as a progress updated based on
// the integer.
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Here you can dismiss the progress bar since the read as been done.
}
}
Call it like this in your MainActivity:
new PDFLoader().execute();
I hope this helps you.
This is just the basic structure to get you started. More information here.
Using a Splash screen is not a solution to this.You are adding a screen with 12second gap and after 12 second the MainActivity class is getting execute.
So, you can directly add a progress bar in MainActivity class and you can use AsyncTask to solve this problem.
AsyncTask has 3 methods-
OnPreExecute, doInBackground, OnPostExecute. Here, you can add a progress bar in OnPreExecute method and the code for opening PDF file in doInBackground method, and finally stop the progress bar in OnPostExecute method.
For more help you should provide your MainActivity class code here.
You should give a shot to this article: https://antonioleiva.com/branded-launch-screen/
It's far better than Splash Screen, 'cause It loads async to your MainActivity
Add this code to styles:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar" />
<style name="AppTheme.BrandedLaunch" parent="AppTheme">
<item name="android:windowBackground">#drawable/branded_logo</item>
</style>
Now create branded_logo.xml into your drawable directory
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#color/background_material_light"/>
</item>
<item>
<bitmap
android:src="#drawable/launch_logo_image"
android:tileMode="disabled"
android:gravity="center"/>
</item>
</layer-list>
And assign that style into your MainActivity
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.BrandedLaunch">
And finally add setStyle to your default theme in your onCreate in MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
...
You can create a LoadingScreen instead of splash screen(looking at your 12 secs. delay). Here is something that might help:
public class LoadingScreen {private ImageView loading;
LoadingScreen(ImageView loading) {
this.loading = loading;
}
public void setLoadScreen(){
final Integer[] loadingImages = {R.mipmap.loading_1, R.mipmap.loading_2,
R.mipmap.loading_3, R.mipmap.loading_4};
final Handler loadingHandler = new Handler();
Runnable runnable = new Runnable() {
int loadingImgIndex = 0;
public void run() {
loading.setImageResource(loadingImages[loadingImgIndex]);
loadingImgIndex++;
if (loadingImgIndex >= loadingImages.length)
loadingImgIndex = 0;
loadingHandler.postDelayed(this, 500);//change to accordingly(i.e. 12000)
}
};
loadingHandler.postDelayed(runnable, 500); // change it accordingly
}}
In your MainActivity, you can pass a to the LoadingScreen class like this :-
private ImageView loadingImage;
Don't forget to add an ImageView in activity_main. After that call the LoadingScreen class like this;
LoadingScreen loadingscreen = new LoadingScreen(loadingImage);
loadingscreen.setLoadScreen();
It is always helpful in creating a custom loading screen instead of Splash screen.Also don't forget to add some different images to make that feel like a gif and after that add the images in the res folder . I have checked it for lower versions too Therefore, believe that it might serve your purpose...
Nowadays lots of applications are avoiding loading screen due to bad UI/UX. You can use alternative for content loading. This article may help more about. You can look this kind of implementation on lot of popular apps like Facebook, LinkedIn, Upwork etc.

How to trigger onPause programmatically in android activity

I am trying to work out how I can simulate pausing an activity for debugging my app. I want onPause to be called but NOT onStop. I just want to try a pause resume cycle and am looking for some code i can call (e.g. after a button press) to trigger this.
Any ideas how?
I have seen people suggest pressing the home button in other threads but when I do this is stops the app and calls onStop as well as onPause so it isn't quite what I was looking for.
Taken from this link: The easiest is to add a semitransparent activity on top of your activity. I did the test myself and onStop is not called indeed:
The transparent activity:
public class TransparentActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.transparent_layout);
}
}
any simple layout can be used for transparent_layout, but the tricky part is in the Manifest:
<activity
android:name=".TransparentActivity"
android:theme="#style/Theme.Transparent" >
</activity>
where in styles.xml:
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Then in starter activity:
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnNext).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, TransparentActivity.class));
}
});
}
#Override
protected void onPause() {
super.onPause();
Log.d("TSTAct", "#onPause");
}
#Override
protected void onStop() {
super.onStop();
Log.d("TSTAct", "#onStop");
}
#Override
protected void onResume() {
super.onResume();
Log.d("TSTAct", "#onResume");
}
#Override
protected void onStart() {
super.onStart();
Log.d("TSTAct", "#onStart");
}
}
When opening the TransparentActivity I can see in the Logcat only:
07-10 23:35:28.323: D/TSTAct(27180): #onPause
no onStop call.
Simple way, From Mainactivity i will call another activity using Intent. In Manifest i will define as
<activity android:name=".AnotherActivity"
android:theme="#style/Theme.AppCompat.Dialog" >
</activity>
Means for another activity i will add style as "Theme.AppCompat.Dialog" means this will looks as Dialog.
From Main activity if you call this using Intent , then "AnotherActivity" will show as Dialog , It will come in on top of Main activity, that time main activity will be in onPause state (It will not call onStop state of MainActivity)
1) define a method.
public static Instrumentation callLifeCycleMethod()
{
return new Instrumentation();
}
2) call method.
callLifeCycleMethod().callActivityOnDestroy(MainActivity.this);
or
callLifeCycleMethod().callActivityOnPause(MainActivity.this);
Another way to debug what happens with onPause and onResume is to make use of the Android test framework. In an activity test you can get an Instrumentation object and then trigger pause and resume:
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
public class LaunchActivityTest extends ActivityInstrumentationTestCase2<LaunchActivity> {
private LaunchActivity launchActivity;
public LaunchActivityTest() {
super(LaunchActivity.class);
// TODO Auto-generated constructor stub
}
#Override
protected void setUp() throws Exception {
super.setUp();
launchActivity = getActivity();
}
#Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testPause() {
Instrumentation ins = getInstrumentation();
ins.callActivityOnPause(launchActivity);
assertEquals(true, true);
ins.callActivityOnResume(launchActivity);
}
}
Note that I am just using a dummy assert here. As an aside I am not yet sure how i can assert that the methods were called in Java
Another option might be to call the private method performPause in Activity using reflection.
Something like this should work in principle:
Method method = myactivity.getClass().getSuperclass().getDeclaredMethod("performPause");
method.setAccessible(true);
method.invoke(myactivity);
Note I have yet to test it

How do I avoid a black screen before startup?

I created a loadpage with a background and a progress bar and when it finishes loading, it starts the main class but the loading screen is not showing up. It is just a black screen while it loads and then the main screen. I put all the work in the onResume and I also tried onStart with no luck
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loadpage);
//get rid of title bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(1);
}
#Override
public void onResume() {
super.onResume();
words = WordList.sharedWordList(this);
if(generatedLevels==null)
{
generatedLevels = new ArrayList<PuzzleMZLen>();
}
if(!p.isAlive())
{
p.start();
}
Intent i = new Intent(getApplicationContext(), Main.class);
startActivity(i);
}
thanks in advance
You have to use AsyncTask for this kind of work.Your layout is populated after the completion of the loading.So you are watching black screen.
Use onPreExecute of AsyncTask class to show the progress bar. And write loading code in the doInBackground method.
Definitely use AsyncTask for this. I had the same thing going for my app, and here's my code:
private class getAllData extends AsyncTask<Context, Void, Cursor> {
protected void onPreExecute () {
dialog = ProgressDialog.show(Directory.this, "",
"Loading. Please wait...", true);
}
#Override
protected Cursor doInBackground(Context... params) {
DirectoryTransaction.doDepts();
return null;
}
protected void onPostExecute(Cursor c) {
for(int i = 0 ; i < DeptResults.length ; i++){
deptsAdapter.add(DeptResults[i][0]);
}
dialog.dismiss();
}
}
onPreExecute method loads a ProgressDialog that just shows "Loading. Please wait...". doInBackground does what was making my app load (in my case grabbing and parsing text from a server) and then onPostExecute is filling a spinner then dismissing the ProgressDialog. You'll want some thing different for a progress BAR, but the AsyncTask will be very similar. Call it in onCreate with new getAllData.execute(this);
There is a good way how to create a good splash activity.
To prevent ANR you should move all data loading outside of UI thread as it was mentioned at other answers. Please use AsyncTask or any other kind of multi-threading for that.
However to remove annoying black screen which shows for few moments on some slow devices you need to do next steps:
Create bg_splash drawable. It will be shown instead of black background just in time activity shows to user. For example it can be brand logo on brand color background.
Create a splash screen theme and put it into /res/values/themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyApp.Splash" parent="#style/Theme.Sherlock.Light.NoActionBar">
<item name="android:windowBackground">#drawable/bg_splash</item>
</style>
</resources>
Don't forget assign created theme to splash activity by updating AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1" >
...
<application>
<activity
android:name=".SplashActivity"
android:theme="#style/MyApp.Splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity>
...
</application>
</manifest>

Clean dialog collector class for use in all other activities [Android]

Im trying and searching for a good method to handle dialogs in a single class so i can use them in any activity i want. The most clean and good performance method would be best.
Currently im handly dialogs in each activity where the main work is done. If i need to change a dialog or dialogevent its a hassle to search through all my classes.
[SOLVED] ~ Update with code below.
Looks great. Hope im doing good with it. Any optimization?
--- Code from Dialog Class
public class Dialogs extends Activity {
public static final int DIALOG_START = 0;
public static final int DIALOG_END = 1;
private Context mContext;
private int mDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getApplicationContext();
mDialog = getIntent().getExtras().getInt("dialog");
showDialog(mDialog);
}
#Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case DIALOG_START:
Toast.makeText(mContext, "Test...", Toast.LENGTH_SHORT).show();
finish(); //works because toasts are somehow delayed
break;
case DIALOG_END:
// do something else but always finish(), e.g. after dialogbutton- click.
break;
}
return dialog;
}
}
--- Code in target Activity (for example button click):
Intent dialogIntent = new Intent();
dialogIntent.setClass(Main.this, Dialogs.class);
dialogIntent.putExtra("dialog" , Dialogs.DIALOG_START);
startActivityForResult(dialogIntent, 0x0);
--- Code in Manifest:
<activity android:name=".Dialogs" android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden"
android:theme="#style/dialog" />
--- Code in Stylefile (values/style.xml):
<style name="dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
You have to create one Activity whose theme set as #android:style/Theme.Dialog in Android-manifest.xml. Then you can use this one as dialog(or popup) anywhere simply as we use activities that is by startActivityForResult(Intent intent, int requestCode).

How to change current Theme at runtime in Android [duplicate]

This question already has answers here:
Implementing user choice of theme
(4 answers)
Closed 5 years ago.
I've created a PreferenceActivity that allows the user to choose the theme he wants to apply to the entire application.
When the user selects a theme, this code is executed:
if (...) {
getApplication().setTheme(R.style.BlackTheme);
} else {
getApplication().setTheme(R.style.LightTheme);
}
But, even though I've checked with the debugger that the code is being executed, I can't see any change in the user interface.
Themes are defined in res/values/styles.xml, and Eclipse does not show any error.
<resources>
<style name="LightTheme" parent="#android:style/Theme.Light">
</style>
<style name="BlackTheme" parent="#android:style/Theme.Black">
</style>
</resources>
Any idea about what could be happening and how to fix it?
Should I call setTheme at any special point in the code? My application consists of several Activities if that helps.
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);
}
If you want to change theme of an already existing activity, call recreate() after setTheme().
Note: don't call recreate if you change theme in onCreate(), to avoid infinite loop.
recreate() (as mentioned by TPReal) will only restart current activity, but the previous activities will still be in back stack and theme will not be applied to them.
So, another solution for this problem is to recreate the task stack completely, like this:
TaskStackBuilder.create(getActivity())
.addNextIntent(new Intent(getActivity(), MainActivity.class))
.addNextIntent(getActivity().getIntent())
.startActivities();
EDIT:
Just put the code above after you perform changing of theme on the UI or somewhere else. All your activities should have method setTheme() called before onCreate(), probably in some parent activity. It is also a normal approach to store the theme chosen in SharedPreferences, read it and then set using setTheme() method.
i got the same problem but i found the solution.
public class EditTextSmartPhoneActivity extends Activity implements DialogInterface.OnClickListener
{
public final static int CREATE_DIALOG = -1;
public final static int THEME_HOLO_LIGHT = 0;
public final static int THEME_BLACK = 1;
int position;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
position = getIntent().getIntExtra("position", -1);
switch(position)
{
case CREATE_DIALOG:
createDialog();
break;
case THEME_HOLO_LIGHT:
setTheme(android.R.style.Theme_Holo_Light);
break;
case THEME_BLACK:
setTheme(android.R.style.Theme_Black);
break;
default:
}
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private void createDialog()
{
/** Options for user to select*/
String choose[] = {"Theme_Holo_Light","Theme_Black"};
AlertDialog.Builder b = new AlertDialog.Builder(this);
/** Setting a title for the window */
b.setTitle("Choose your Application Theme");
/** Setting items to the alert dialog */
b.setSingleChoiceItems(choose, 0, null);
/** Setting a positive button and its listener */
b.setPositiveButton("OK",this);
/** Setting a positive button and its listener */
b.setNegativeButton("Cancel", null);
/** Creating the alert dialog window using the builder class */
AlertDialog d = b.create();
/** show dialog*/
d.show();
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
AlertDialog alert = (AlertDialog)dialog;
int position = alert.getListView().getCheckedItemPosition();
finish();
Intent intent = new Intent(this, EditTextSmartPhoneActivity.class);
intent.putExtra("position", position);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
We have to set theme before calling 'super.onCreate()' and 'setContentView()' method.
Check out this link for applying new theme to whole application at runtime.
I had a similar problem and I solved in this way..
#Override
public void onCreate(Bundle savedInstanceState) {
if (getIntent().hasExtra("bundle") && savedInstanceState==null){
savedInstanceState = getIntent().getExtras().getBundle("bundle");
}
//add code for theme
switch(theme)
{
case LIGHT:
setTheme(R.style.LightTheme);
break;
case BLACK:
setTheme(R.style.BlackTheme);
break;
default:
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//code
}
this code is for recreate the Activity saving Bundle and changing the theme. You have to write your own onSaveInstanceState(Bundle outState); From API-11 you can use the method recreate() instead
Bundle temp_bundle = new Bundle();
onSaveInstanceState(temp_bundle);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("bundle", temp_bundle);
startActivity(intent);
finish();
Instead of
getApplication().setTheme(R.style.BlackTheme);
use
setTheme(R.style.BlackTheme);
My code: in onCreate() method:
super.onCreate(savedInstanceState);
if(someExpression) {
setTheme(R.style.OneTheme);
} else {
setTheme(R.style.AnotherTheme);
}
setContentView(R.layout.activity_some_layout);
Somewhere (for example, on a button click):
YourActivity.this.recreate();
You have to recreate activity, otherwise - change won't happen
This is what i have created for Material Design. May it will helpful you.
Have a look for MultipleThemeMaterialDesign
I know that i am late but i would like to post a solution here:
Check the full source code here.
This is the code i used when changing theme using preferences..
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(this);
String themeName = pref.getString("prefSyncFrequency3", "Theme1");
if (themeName.equals("Africa")) {
setTheme(R.style.AppTheme);
} else if (themeName.equals("Colorful Beach")) {
//Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
setTheme(R.style.beach);
} else if (themeName.equals("Abstract")) {
//Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
setTheme(R.style.abstract2);
} else if (themeName.equals("Default")) {
setTheme(R.style.defaulttheme);
}
This way work for me:
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(GApplication.getInstance().getTheme());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Then you want to change a new theme:
GApplication.getInstance().setTheme(R.style.LightTheme);
recreate();
You can finish the Acivity and recreate it afterwards in this way your activity will be created again and all the views will be created with the new theme.
Call SetContentView(Resource.Layout.Main) after setTheme().
This had no effect for me:
public void changeTheme(int newTheme) {
setTheme(newTheme);
recreate();
}
But this worked:
int theme = R.style.default;
protected void onCreate(Bundle savedInstanceState) {
setTheme(this.theme);
super.onCreate(savedInstanceState);
}
public void changeTheme(int newTheme) {
this.theme = newTheme;
recreate();
}

Categories

Resources