Android Open Source - Material Theme Util






From Project

Back to project page Material.

License

The source code is released under:

Apache License

If you think the Android project Material listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.rey.material.util;
//from ww  w  .j  ava2  s  . co m
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources.Theme;
import android.os.Build;
import android.util.TypedValue;

import com.rey.material.R;

public class ThemeUtil {
    
  private static TypedValue value;
  
  public static int dpToPx(Context context, int dp){
    return (int)(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()) + 0.5f);
  }
  
  public static int spToPx(Context context, int sp){
    return (int)(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics()) + 0.5f);
  }
  
  private static int getColor(Context context, int id, int defaultValue){
    if(value == null)
      value = new TypedValue();
    
    try{
      Theme theme = context.getTheme();    
      if(theme != null && theme.resolveAttribute(id, value, true) && value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT)
        return value.data;
    }
    catch(Exception ex){}
    
    return defaultValue;
  }
  
  public static int windowBackground(Context context, int defaultValue){
    return getColor(context, android.R.attr.windowBackground, defaultValue);
  }
  
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public static int colorPrimary(Context context, int defaultValue){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
      return getColor(context, android.R.attr.colorPrimary, defaultValue);
    
    return getColor(context, R.attr.colorPrimary, defaultValue);
  }
  
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public static int colorPrimaryDark(Context context, int defaultValue){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
      return getColor(context, android.R.attr.colorPrimaryDark, defaultValue);
    
    return getColor(context, R.attr.colorPrimaryDark, defaultValue);
  }
  
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public static int colorAccent(Context context, int defaultValue){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
      return getColor(context, android.R.attr.colorAccent, defaultValue);
    
    return getColor(context, R.attr.colorAccent, defaultValue);
  }
  
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public static int colorControlNormal(Context context, int defaultValue){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
      return getColor(context, android.R.attr.colorControlNormal, defaultValue);
    
    return getColor(context, R.attr.colorControlNormal, defaultValue);
  }
  
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public static int colorControlActivated(Context context, int defaultValue){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
      return getColor(context, android.R.attr.colorControlActivated, defaultValue);
    
    return getColor(context, R.attr.colorControlActivated, defaultValue);
  }
  
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public static int colorControlHighlight(Context context, int defaultValue){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
      return getColor(context, android.R.attr.colorControlHighlight, defaultValue);
    
    return getColor(context, R.attr.colorControlHighlight, defaultValue);
  }
  
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public static int colorButtonNormal(Context context, int defaultValue){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
      return getColor(context, android.R.attr.colorButtonNormal, defaultValue);
    
    return getColor(context, R.attr.colorButtonNormal, defaultValue);
  }
  
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public static int colorSwitchThumbNormal(Context context, int defaultValue){    
    return getColor(context, R.attr.colorSwitchThumbNormal, defaultValue);
  }
  
}




Java Source Code List

com.rey.material.ApplicationTest.java
com.rey.material.demo.ButtonFragment.java
com.rey.material.demo.MainActivity.java
com.rey.material.demo.ProgressFragment.java
com.rey.material.demo.SnackbarFragment.java
com.rey.material.demo.SwitchesFragment.java
com.rey.material.demo.TextfieldFragment.java
com.rey.material.drawable.ArrowDrawable.java
com.rey.material.drawable.BlankDrawable.java
com.rey.material.drawable.CheckBoxDrawable.java
com.rey.material.drawable.CircularProgressDrawable.java
com.rey.material.drawable.DividerDrawable.java
com.rey.material.drawable.LineMorphingDrawable.java
com.rey.material.drawable.LinearProgressDrawable.java
com.rey.material.drawable.NavigationDrawerDrawable.java
com.rey.material.drawable.RadioButtonDrawable.java
com.rey.material.drawable.RevealDrawable.java
com.rey.material.drawable.RippleDrawable.java
com.rey.material.drawable.ToolbarRippleDrawable.java
com.rey.material.util.ColorUtil.java
com.rey.material.util.ThemeUtil.java
com.rey.material.util.ViewUtil.java
com.rey.material.view.Button.java
com.rey.material.view.CheckBox.java
com.rey.material.view.CheckedTextView.java
com.rey.material.view.CompoundButton.java
com.rey.material.view.EditText.java
com.rey.material.view.FloatingActionButton.java
com.rey.material.view.ListPopupWindow.java
com.rey.material.view.ListView.java
com.rey.material.view.PopupWindow.java
com.rey.material.view.ProgressView.java
com.rey.material.view.RadioButton.java
com.rey.material.view.RippleManager.java
com.rey.material.view.SnackBar.java
com.rey.material.view.Spinner.java
com.rey.material.view.Switch.java
com.rey.material.view.TabPageIndicator.java
com.rey.material.view.TextView.java