Skip to content Skip to sidebar Skip to footer

How To Create A Custom Theme And Use It In An Android Application?

How to create a custom themes and use it in the code? In menu how to implement theme option and apply for the activity?

Solution 1:

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><stylename="Theme.MyGreenTheme"parent="Theme.Light"><itemname="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().

Solution 2:

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 :)

Solution 3:

Create Custome Views:

public class CustomTextView extends AppCompatTextView {

publicCustomTextView(Context context) {
    super(context);
    setCommonChanges(DefaultTheme.getInstance().textColor, true, context);
}

publicCustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setDefaultValues(context, attrs);
}

publicCustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setDefaultValues(context, attrs);
}

privatevoidsetDefaultValues(Context context, AttributeSet attrs) {

    TypedArraya= context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
    finalintN= a.getIndexCount();
    intcolor= DefaultTheme.getInstance().textColor;
    booleanisCustomFont= a.getBoolean(R.styleable.CustomTextView_isCustomFont, true);
    for (inti=0; i < N; ++i) {

        intcolorIndex= a.getInteger(R.styleable.CustomTextView_tvBackground, 2);
        switch (colorIndex) {
            case1:
                color = DefaultTheme.getInstance().headingTextColor;
                break;

            case2:
                color = DefaultTheme.getInstance().textColor;
                break;

            case3:
                color = DefaultTheme.getInstance().textHintColor;
                break;

            case4:
                color = DesignUtils.getColorIdFromHexCode("#FFFFFF");
                break;

            case5:
                color = DefaultTheme.getInstance().iconColor;
                break;
            case6:
                color = DefaultTheme.getInstance().menuHeaderTextColor;
                break;
            case7:
                color = DefaultTheme.getInstance().menuTextColor;
                break;
            case8:
                color = DefaultTheme.getInstance().keyboardtextcolor;
                break;
            case9:
                color = DesignUtils.getColorIdFromHexCode("#BEBEBE");
                break;
        }


    }
    a.recycle();

    setCommonChanges(color, isCustomFont, context);
}

privatevoidsetCommonChanges(int color, boolean isCustomFont, Context context) {
    if (isCustomFont) {
        Typefacetypeface= DefaultTheme.getInstance().getTVFont(context);
        setTypeface(typeface, getTypeface().getStyle());
    }
    setTextColor(color);
}

publicvoidupdateTypeFace(int style){
    Typefacetypeface= DefaultTheme.getInstance().getTVFont(getContext());
    setTypeface(typeface, style);
}

Post a Comment for "How To Create A Custom Theme And Use It In An Android Application?"