How to create a custom themes and use it in the code?
In menu how to implement theme option and apply for the activity?
There's a nice Styles and Themes guide on the android developers site. Basically what you need to do is
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 theres/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.
Apply the defined style to the activity or view that you want stylized. You can either
<activity android:theme="@style/Theme.MyGreenTheme"/>
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 :)
Create Custome Views:
public class CustomTextView extends AppCompatTextView {
public CustomTextView(Context context) {
super(context);
setCommonChanges(DefaultTheme.getInstance().textColor, true, context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setDefaultValues(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setDefaultValues(context, attrs);
}
private void setDefaultValues(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
final int N = a.getIndexCount();
int color = DefaultTheme.getInstance().textColor;
boolean isCustomFont = a.getBoolean(R.styleable.CustomTextView_isCustomFont, true);
for (int i = 0; i < N; ++i) {
int colorIndex = a.getInteger(R.styleable.CustomTextView_tvBackground, 2);
switch (colorIndex) {
case 1:
color = DefaultTheme.getInstance().headingTextColor;
break;
case 2:
color = DefaultTheme.getInstance().textColor;
break;
case 3:
color = DefaultTheme.getInstance().textHintColor;
break;
case 4:
color = DesignUtils.getColorIdFromHexCode("#FFFFFF");
break;
case 5:
color = DefaultTheme.getInstance().iconColor;
break;
case 6:
color = DefaultTheme.getInstance().menuHeaderTextColor;
break;
case 7:
color = DefaultTheme.getInstance().menuTextColor;
break;
case 8:
color = DefaultTheme.getInstance().keyboardtextcolor;
break;
case 9:
color = DesignUtils.getColorIdFromHexCode("#BEBEBE");
break;
}
}
a.recycle();
setCommonChanges(color, isCustomFont, context);
}
private void setCommonChanges(int color, boolean isCustomFont, Context context) {
if (isCustomFont) {
Typeface typeface = DefaultTheme.getInstance().getTVFont(context);
setTypeface(typeface, getTypeface().getStyle());
}
setTextColor(color);
}
public void updateTypeFace(int style){
Typeface typeface = DefaultTheme.getInstance().getTVFont(getContext());
setTypeface(typeface, style);
}