Set the theme from the application

You can also load a theme for an Activity programmatically, if needed. To do so, use the setTheme() method. Note that, when doing so, you must be sure to set the theme before instantiating any Views in the context, for example, before calling setContentView(View) or inflate(int, ViewGroup) . This ensures that the system applies the same theme for all of your UI screens. Here's an example:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setTheme(android.R.style.Theme Light); setContentView(R.layout.linear layout 3);

If you are considering loading a theme programmatically for the main screen of your application, note that the theme would not be applied in any animations the system would use to start the activity, which would take place before your application opens. In most cases, if you want to apply a theme to your main screen, doing so in XML is a better approach.

For detailed information about custom styles and themes and referencing them from your application, see Available Resource Types: Style and Themes.

For information about default themes and styles available, see R.style

0 0

Post a comment