|
package com.webserveis.app.splitpaywear; |
|
|
|
import android.content.Context; |
|
import android.content.res.Resources; |
|
import android.os.Build; |
|
import android.support.v4.content.ContextCompat; |
|
import android.util.TypedValue; |
|
|
|
/** |
|
* Stles.xml sample |
|
* <p> |
|
* <style name="AppThemeGreen" parent="Theme.AppCompat.Light.DarkActionBar"> |
|
* <p> |
|
* <item name="colorPrimary">@color/md_green_500</item> |
|
* <item name="colorPrimaryDark">@color/md_green_800</item> |
|
* <item name="colorAccent">@color/md_orange_300</item> |
|
* <p> |
|
* <item name="android:textColor">#777733</item> |
|
* <item name="android:textColorPrimary">@color/md_yellow_300</item> |
|
* <item name="android:textColorSecondary">@color/md_red_300</item> |
|
* <item name="android:textColorTertiary">@color/md_grey_500</item> |
|
* <p> |
|
* </style> |
|
* <p> |
|
* <style name="AppThemeGreen.NoActionBar"> |
|
* <item name="windowActionBar">false</item> |
|
* <item name="windowNoTitle">true</item> |
|
* </style> |
|
* <p> |
|
* Set the theme in activity before to setContentView(id_layout) |
|
* setTheme(R.style.AppThemeGreen_NoActionBar); |
|
* <p> |
|
* Get color colorAccent define in colors.xml |
|
* int intColor = ThemeUtils.getColor(MainActivity.this,R.color.colorAccent); |
|
* String hexColor = ThemeUtils.getColorHex(MainActivity.this,R.color.colorAccent); |
|
* <p> |
|
* Get value colorAccent from actual theme define in styles.xml |
|
* intColor = ThemeUtils.getThemeAttribute(MainActivity.this,R.attr.colorAccent); |
|
* <p> |
|
* Get value android:textColorSecondary define styles.xml |
|
* intColor = ThemeUtils.getThemeAttribute(MainActivity.this,android.R.attr.textColorSecondary); |
|
* <p> |
|
* Conver colors int to hex |
|
* String hexColor = ThemeUtils.colorIntToHex(intColor); |
|
* <p> |
|
* Set color text Best way |
|
* .setTextColor(Color.parseColor(hexColor)); |
|
*/ |
|
public class ThemeUtils { |
|
|
|
//setTheme(android.R.style.Theme); |
|
|
|
|
|
public static int getThemeAttribute(Context context, int id) { |
|
TypedValue typedValue = new TypedValue(); |
|
Resources.Theme theme = context.getTheme(); |
|
theme.resolveAttribute(id, typedValue, true); |
|
return typedValue.data; |
|
} |
|
|
|
/** |
|
* Get color define in colors.xml |
|
* |
|
* @param context |
|
* @param id Color id |
|
* @return value color |
|
* @usage getColor(this, R.color.colorAccent) |
|
*/ |
|
public static int getColor(Context context, int id) { |
|
final int version = Build.VERSION.SDK_INT; |
|
if (version >= 23) { |
|
return ContextCompat.getColor(context, id); |
|
} else { |
|
return context.getResources().getColor(id); |
|
} |
|
} |
|
|
|
public static String getColorHex(Context context, int id) { |
|
int intColor = 0; |
|
final int version = Build.VERSION.SDK_INT; |
|
if (version >= 23) { |
|
intColor = ContextCompat.getColor(context, id); |
|
} else { |
|
intColor = context.getResources().getColor(id); |
|
} |
|
return colorIntToHex(intColor); |
|
} |
|
|
|
public static String colorIntToHex(int intColor) { |
|
return String.format("#%06X", (0xFFFFFF & intColor)); |
|
} |
|
} |