Change Activity's theme programmatically
As docs say you have to call setTheme
before any view output. It seems that super.onCreate()
takes part in view
processing.
So, to switch between themes dynamically you simply need to call setTheme
before super.onCreate
like this:
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
Change launch activity style programmatically
After some research I've discovered that this is not possible. Launch screens are used when we want to show something while our main functionality is loading, so we include the drawable we want to show in the Manifest to have this appearing fast while our main activity loads. This launch screen, as it is in the Manifest, appears before anything else, so if we could change the theme of the launch screen dinamically, we would lose that fast appearing while everything else loads.
How to change current Theme at runtime in Android
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);
}
Android:How to programmatically set an Activity's theme to Theme.Dialog
Would like to give a work around for this problem.
Problem : How to use the same activity as both dialog and full screen based.
Solution :
- Define your activity in your AndroidManifest.xml with the theme
@android:style/Theme.Dialog
- In your respective
.Java
file, check for anintent
extra that definesdialog
mode. - If it does not exist, set the
Theme
toandroid.R.style.Theme
. This is the defaulttheme
which is applied if you do not define any theme.
Code :
boolean fDialogMode = getIntent().hasExtra("dialog_mode");
if( ! fDialogMode ) {
super.setTheme(android.R.style.Theme);
}
Alternate Solution:
A more complex solution is to use AlertDialog
as below:
- Define a
ListAdapter
class extended fromArrayAdapter
. return
1
ingetCount
function@Override
public int getCount() { return 1; }In the
getView
function,inflate
thelayout
of theactivity
you need and do any customization before returning theview
.@Override
public View getView( int position, View view, ViewGroup group ) {
View v = view;
if( v == null ) {
v = getSystemService(Context.LAYOUT_INFLATER_SERVICE).inflate( <layout res id>, null );
}
... Do any customization here ....
return v;
}
This is definitely a second choice option by if you are not doing too much processing in the activity
class
this could be an option.
Only reason to consider this solution could be that the logic to show it in a dialog
is isolated to the places where it is used as a dialog.
Both the options worked for me but for obvious reasons I am taking the first option. :-)
Change Activity theme programmatically dosen't work
I found way to do that. In onCreate i put this
setTheme(Designs.getThemeNoActionBar(this));
setContentView(R.layout.activity_main);
get the theme from shared preferences and return theme without actionbar.
public static int getThemeNoActionBar(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences("Designs", Context.MODE_PRIVATE);
int theme = sharedPreferences.getInt("theme", R.style.AppTheme);
if (theme == nightModeTheme)
return R.style.NightMode_NoActionBar;
else if (theme == theme1)
return R.style.Theme1_NoActionBar;
else
return R.style.AppTheme_NoActionBar;
}
In style res
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="NightMode.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme1.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Switching application-wide theme programmatically?
Create a base activity for your app and override onCreate
to set the theme. Derive all your other activities from this base activity.
Also check this tutorial:
http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html
Related Topics
Android: Speech Recognition Without Using Google Server
Getexternalstoragepublicdirectory Deprecated in Android Q
Apply a Theme to an Activity in Android
Sharing Data Between Fragments Using New Architecture Component Viewmodel
How to Create Text File and Insert Data to That File on Android
Error Message 'Java.Net.Socketexception: Socket Failed: Eacces (Permission Denied)'
Android Set Up Volley to Use from Cache
Handle Screen Rotation Without Losing Data - Android
Why My App Is Not Showing Up on Tablets in Google Play
Show an Alert Dialog in Broadcast Receiver After a System Reboot
What Proguard Configuration Do I Need for Firebase on Android
Android Push Notification (Gcm), Is There Any Daily Limit
How to Maintain Multi Layers of Imageviews and Keep Their Aspect Ratio Based on the Largest One
Manifest Merger Fails for Appcomponentfactory