Applying a Theme to your Application

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Applying a Theme to your Application

Postby plusminus » Fri Feb 08, 2008 4:27 pm

[align=center]Applying a Theme to your Application[/align]

What you learn: You will learn how to apply a Theme to your Application using not more than I line of Code in XML or JAVA.

Difficulty: 0.5 of 5 :)

:idea: Questions/Problems: Simply post below...

What it will look like: [align=center]Example-Themes on my Android FileBrowser -Tutorial. Default vs. Black Theme.
Image Image[/align]

Description:

If you do not explicitly specify a theme for your UI, Android will use the default theme defined by android.R.style.Theme. Many times you will want to use a different system theme (such as Theme.Dark) or create your own theme (as described in Style and Theme Resources).

[align=center]... the XML-Way.[/align]
To set your theme in XML, simply specify the desired theme in your AndroidManifest.xml file with the theme attribute. This can be used with the <application> tag (shown here) to specify a default theme for all of your activities, and/or with the <activity> to control the theme of a particular activity.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <!-- AndroidManifest.xml-->
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.        package="com.google.android.home">
  6.  
  7.     <application android:theme="@android:style/Theme.Dark" >
  8.  
  9.         <activity class=".Home"
  10.  
  11.        ...
  12.  
  13.        </activity>
  14.  
  15.     </application>
  16.  
  17. </manifest>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


[align=center]... and the JAVA-Way.[/align]
You can also set the theme programmatically, if needed. When doing so, be sure to set the theme before creating any views so that the correct theme is used for all of your user-interface elements. Note that this approach should typically be avoided, especially from the main activities of your application, because the theme you set here may not be used for any animations the system uses to show the activity (which is done before your application starts).
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  protected void onCreate(Bundle icicle) {
  2.  
  3.     super.onCreate(icicle);
  4.  
  5.     // ...
  6.  
  7.     // Call setTheme before creation of any(!) View.
  8.  
  9.     setTheme(android.R.style.Theme_Dark);
  10.  
  11.     // ...
  12.  
  13.     setContentView(R.layout.main);
  14.  
  15. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


[align=center]Thats it :)[/align]

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby Jodol » Fri Feb 08, 2008 9:48 pm

Currently these theme's are supported in Android by defauilt:
- Theme_Dark
- Theme_Dialog
- Theme_Dialog_AppError
- Theme_White (default)

But you can also make your own theme. Read all about it here.

Greetz,

Jodol
Member of a Dutch company developing applications for Android.
ColdTrick Solutions
User avatar
Jodol
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Dec 19, 2007 6:29 pm
Location: Netherlands

Postby Katharnavas » Tue Feb 19, 2008 6:22 am

What about creating and using our custom themes ? Any idea ?
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby venkat » Tue Feb 19, 2008 7:06 am

Hi Plus Minus,
How to Change Whole Andriod Theme( not only app theme and Wall paper.)
Thanks :)
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby plusminus » Tue Feb 19, 2008 4:17 pm

Hey guys,

Katharnavas wrote:What about creating and using our custom themes ? Any idea ?

What do you mean :?:
Jodol provided that like: http://code.google.com/android/referenc ... sandthemes

venkat wrote:Hi Plus Minus,
How to Change Whole Andriod Theme( not only app theme and Wall paper.)
Thanks :)


I'm not aware of such a functionality, yet. :(

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby venkat » Tue Feb 19, 2008 5:57 pm

Thanks for you reply plusminus, let me know if you find any functionality regarding this.

Thanks again,:)
Last edited by venkat on Thu Feb 21, 2008 6:09 am, edited 1 time in total.
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Top

Postby Katharnavas » Wed Feb 20, 2008 8:37 am

plusminus wrote:Hey guys,

Katharnavas wrote:What about creating and using our custom themes ? Any idea ?

What do you mean :?:
Jodol provided that like: http://code.google.com/android/referenc ... sandthemes

venkat wrote:Hi Plus Minus,
How to Change Whole Andriod Theme( not only app theme and Wall paper.)
Thanks :)


I'm not aware of such a functionality, yet. :(

Regards,
plusminus


Thanks, What i need is just similiar to that but i to change the theme of whole application not a single activity. How can it be achieved. Instead of defining it with style attributes will it be possible to apply to all the activities
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby plusminus » Wed Feb 20, 2008 10:20 am

Hello Katharnavas,

in AndNav I did it using a SharedPreferences, like this.
I have some public Constants:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         /** Name of the Shared Preferences-File. */
  2.  
  3.         public final String SHARED_PREFERENCES_NAME = "andnav_preferences";
  4.  
  5.         public final String PREF_THEME_RESID_ID = "theme_resid";
  6.  
  7.        
  8.  
  9.        
  10.  
  11.         public final int  DEFAULT_THEME_RESID = android.R.style.Theme_Dark;
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

and created a static function taking an Activity as parameter:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         /** Applies the theme saved in Preferences to the Activity parameter.
  3.  
  4.          * @param act Activity to set the shared theme to. */
  5.  
  6.         public static void applySharedTheme(Activity act){
  7.  
  8.                 SharedPreferences sPref = act.getSharedPreferences(Constants.SHARED_PREFERENCES_NAME, ApplicationContext.MODE_PRIVATE);
  9.  
  10.                 int themeID = sPref.getInt(Constants.PREF_THEME_RESID_ID, Constants.DEFAULT_THEME_RESID);
  11.  
  12.                 act.setTheme(themeID);
  13.  
  14.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


In each Activity call that function before setContentTheme, like here:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         /** Called when the activity is first created. */
  2.  
  3.         @Override
  4.  
  5.         public void onCreate(Bundle icicle) {
  6.  
  7.                 super.onCreate(icicle);
  8.  
  9.                 Util.applySharedTheme(this); // <---
  10.  
  11.                 this.setContentView(R.layout.sd_country);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Hope I could help you :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Katharnavas » Wed Feb 20, 2008 11:02 am

Thanks for the information .
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby mmin18 » Wed Feb 20, 2008 2:44 pm

i have a custom widget now and i want to know how to get the right color according to the Theme its Activity used.

for example, the text should be black in Theme_White, and should be white in Theme_Dark.
mmin18
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sun Feb 03, 2008 2:33 pm
Location: China

Postby plusminus » Wed Feb 20, 2008 4:10 pm

Hello mmin18,

If you do not override these settings, the should get inherited from the Theme applied.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Dialog.Dark (Blue) is not active..

Postby sommeralex » Sun Mar 23, 2008 2:07 pm

I tried to reset the default theme to the .dark theme but nothing happend. All elements are still "orange". In the example above, they turn blue. If i set the theme to .dialog, the dialog theme is applied.

so, how can i make my elements blue-looking (on focus, instead of orange -> blue)
borders of the views -> instead of orange -> blue and so on..

happy easter! 8)
sommeralex
Experienced Developer
Experienced Developer
 
Posts: 95
Joined: Sun Jan 20, 2008 3:12 am
Location: Vienna

Postby bjreddi » Thu Apr 17, 2008 11:53 am

Hi Plusminus,

When i put this line "setTheme(android.R.style.Theme_Dark); " in my Java code. Just above the contentView.
It says Theme_Dark is deprecated and marked th elink horizantally on top of it.
When i ran the program and i did not observe any change.
-----------
in Java:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setTheme(android.R.style.Theme_Dark);
setContentView(R.layout.main);
-----------

Second way: Means XML way.

I put it like this (AndroidManifext.xml)
---------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eb.android.statusbar">
<application android:theme="@android:style/Theme.Dark" >

<application android:icon="@drawable/icon">
<activity android:name=".StatusBarNote" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</application>
</manifest>
--------------------------------

I did not observe any change at all.
My emulator background is always black like before.

Can u tell me or guide me how can i apply a theme.

Thanks and regards,
Jyothi
bjreddi
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Apr 03, 2008 1:24 pm

Postby Jesmiatka » Thu Apr 17, 2008 12:43 pm

Hi,

You actually did applied the theme, but since the standard theme of Android is the theme THEME_DARK you do not observe any change ;-)
If you want to see a change try the light theme (Theme_Light)

Your sincerely,
Mike
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby bjreddi » Thu Apr 17, 2008 1:44 pm

Hi Mike,

Thanks a lot for the reply.
I have changed it to Theme_Light . It worked well. And also tried Theme_Dialog.

regards,
jyothi

Jesmiatka wrote:Hi,

You actually did applied the theme, but since the standard theme of Android is the theme THEME_DARK you do not observe any change ;-)
If you want to see a change try the light theme (Theme_Light)

Your sincerely,
Mike
bjreddi
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Apr 03, 2008 1:24 pm

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests