Skip to content

Instantly share code, notes, and snippets.

@webserveis
Last active November 28, 2016 12:53
Show Gist options
  • Select an option

  • Save webserveis/41955a11f5b61f3b0559a49a56084f59 to your computer and use it in GitHub Desktop.

Select an option

Save webserveis/41955a11f5b61f3b0559a49a56084f59 to your computer and use it in GitHub Desktop.
Aplicar tema en Android

Como aplicar tema en Android, modificando tambien el popup de la ActionBar

Asignar un tema en Java

setTheme(R.style.AppThemeGreen_NoActionBar);
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myToolbarStyle" format="reference"/>
</resources>
...
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="?attr/myToolbarStyle" />
...
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="myToolbarStyle">@style/AppTheme.PopupOverlay</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="myToolbarStyle">@style/AppTheme.PopupOverlay</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<!--
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
-->
<!-- Tema verde. -->
<style name="AppThemeGreen" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/md_green_500</item>
<item name="colorPrimaryDark">@color/md_green_800</item>
<item name="colorAccent">@color/md_orange_300</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>
<item name="myToolbarStyle">@style/AppThemeGreen.PopupOverlay</item>
</style>
<style name="AppThemeGreen.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="myToolbarStyle">@style/AppThemeGreen.PopupOverlay</item>
</style>
<style name="AppThemeGreen.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppThemeGreen.PopupOverlay" parent="ThemeOverlay.AppCompat.Dark" />
</resources>
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));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment