Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom theme and use it in an Android application?

Tags:

android

themes

How to create a custom themes and use it in the code?

In menu how to implement theme option and apply for the activity?

like image 445
user428231 Avatar asked Sep 21 '10 09:09

user428231


People also ask

How do you apply a theme to an app?

In the Project pane select Android, go to app > res > values > themes > themes.

What is theme in application?

A theme is nothing but an Android style applied to an entire Activity or application, rather than an individual View. Thus, when a style is applied as a theme, every View in the Activity or application will apply each style property that it supports.

How do themes work in Android?

A theme defines a collection of named resources which can be referenced by styles, layouts, widgets, and so on. Themes assign semantic names, like colorPrimary , to Android resources. Styles and themes are meant to work together.


2 Answers

There's a nice Styles and Themes guide on the android developers site. Basically what you need to do is

  1. Define a style (or inherit a built-in one). To define a style

save an XML file in the res/values/ directory of your project. The name of the XML file is arbitrary, but it must use the .xml extension and be saved in the res/values/ folder.

The root node of the XML file must be <resources>.

For each style you want to create, add a element to the file with a name that uniquely identifies the style (this attribute is required).

i.e.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.MyGreenTheme" parent="Theme.Light">
        <item name="android:windowBackground">#11aa22</item>
    </style>
</resources>

It's useful to name the resource file themes.xml so it's easier to recognize what those styles are used for.

  1. Apply the defined style to the activity or view that you want stylized. You can either

    • set the Activity/Application theme in the manifest file:

    <activity android:theme="@style/Theme.MyGreenTheme"/>

    • or set it dynamically - use the corresponding setter of the Activity class - setTheme().
like image 54
stan0 Avatar answered Sep 29 '22 14:09

stan0


This is perfect site which creates all the necessary files you need to make a custom UI. I used it personally a couple of weeks ago and it worked great for me.

I have no affiliation with this site but found it very interesting. Hope this may help you :)

like image 20
Michele La Ferla Avatar answered Sep 29 '22 15:09

Michele La Ferla