Example usage for android.content Context getTheme

List of usage examples for android.content Context getTheme

Introduction

In this page you can find the example usage for android.content Context getTheme.

Prototype

@ViewDebug.ExportedProperty(deepExport = true)
public abstract Resources.Theme getTheme();

Source Link

Document

Return the Theme object associated with this Context.

Usage

From source file:com.shipfindpeople.app.widget.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}./*from  ww w.jav a 2s.c  om*/
 */

protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setLines(1);
    //textView.setAllCaps(true);
    textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    TypedValue outValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        textView.setTextColor(
                getResources().getColorStateList(R.color.sliding_tab_title_selector, context.getTheme()));
    } else {

        textView.setTextColor(getResources().getColorStateList(R.color.sliding_tab_title_selector));
    }
    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}

From source file:org.appcelerator.titanium.util.TiUIHelper.java

public static int getColorAccent(final Context context) {
    TypedArray values = null;/*from  ww w  .  j  a  va  2s  .c  o m*/
    try {
        int resourceId = TiRHelper.getResource("android.support.v7.appcompat.R$", "attr.colorAccent");
        int[] attrs = { resourceId };
        values = context.getTheme().obtainStyledAttributes(attrs);
        return values.getColor(0, 0);
    } catch (ResourceNotFoundException e) {
        return 0;
    } finally {
        if (values != null) {
            values.recycle();
        }
    }
}

From source file:android_network.hetnet.vpn_service.Util.java

public static void setTheme(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    boolean dark = prefs.getBoolean("dark_theme", false);
    String theme = prefs.getString("theme", "teal");
    if (theme.equals("teal"))
        context.setTheme(dark ? R.style.AppThemeTealDark : R.style.AppThemeTeal);
    else if (theme.equals("blue"))
        context.setTheme(dark ? R.style.AppThemeBlueDark : R.style.AppThemeBlue);
    else if (theme.equals("purple"))
        context.setTheme(dark ? R.style.AppThemePurpleDark : R.style.AppThemePurple);
    else if (theme.equals("amber"))
        context.setTheme(dark ? R.style.AppThemeAmberDark : R.style.AppThemeAmber);
    else if (theme.equals("orange"))
        context.setTheme(dark ? R.style.AppThemeOrangeDark : R.style.AppThemeOrange);
    else if (theme.equals("green"))
        context.setTheme(dark ? R.style.AppThemeGreenDark : R.style.AppThemeGreen);

    if (context instanceof Activity && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        TypedValue tv = new TypedValue();
        context.getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
        ((Activity) context).setTaskDescription(new ActivityManager.TaskDescription(null, null, tv.data));
    }//  www .  j  a v  a  2s .c  o m
}

From source file:com.xbm.android.matisse.internal.ui.widget.CheckView.java

private void init(Context context) {
    mDensity = context.getResources().getDisplayMetrics().density;

    mStrokePaint = new Paint();
    mStrokePaint.setAntiAlias(true);/*  w  w w.java 2  s  .c om*/
    mStrokePaint.setStyle(Paint.Style.STROKE);
    mStrokePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
    mStrokePaint.setStrokeWidth(STROKE_WIDTH * mDensity);
    TypedArray ta = getContext().getTheme()
            .obtainStyledAttributes(new int[] { R.attr.item_checkCircle_borderColor });
    int defaultColor = ResourcesCompat.getColor(getResources(), R.color.zhihu_item_checkCircle_borderColor,
            getContext().getTheme());
    int color = ta.getColor(0, defaultColor);
    ta.recycle();
    mStrokePaint.setColor(color);

    mCheckDrawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_check_white_18dp,
            context.getTheme());
}

From source file:com.adherence.adherence.SwipeRevealLayout.java

private void init(Context context, AttributeSet attrs) {
    if (attrs != null && context != null) {
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SwipeRevealLayout, 0, 0);

        mDragEdge = a.getInteger(R.styleable.SwipeRevealLayout_dragEdge, DRAG_EDGE_LEFT);
        mMinFlingVelocity = a.getInteger(R.styleable.SwipeRevealLayout_flingVelocity,
                DEFAULT_MIN_FLING_VELOCITY);
        mMode = a.getInteger(R.styleable.SwipeRevealLayout_mode, MODE_NORMAL);

        mMinDistRequestDisallowParent = a.getDimensionPixelSize(
                R.styleable.SwipeRevealLayout_minDistRequestDisallowParent,
                dpToPx(DEFAULT_MIN_DIST_REQUEST_DISALLOW_PARENT));
    }//from w ww .  j av  a2s.  c  o m

    mDragHelper = ViewDragHelper.create(this, 1.0f, mDragHelperCallback);
    mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_ALL);

    mGestureDetector = new GestureDetectorCompat(context, mGestureListener);
}

From source file:com.ternup.caddisfly.fragment.ResultFragment.java

private void displayResult() {

    String[] projection = { TestTable.TABLE_TEST + "." + TestTable.COLUMN_ID,
            TestTable.TABLE_TEST + "." + TestTable.COLUMN_DATE, TestTable.COLUMN_RESULT, TestTable.COLUMN_TYPE,
            TestTable.COLUMN_FOLDER, LocationTable.COLUMN_NAME, LocationTable.COLUMN_STREET,
            LocationTable.COLUMN_TOWN, LocationTable.COLUMN_CITY, LocationTable.COLUMN_STATE,
            LocationTable.COLUMN_COUNTRY, LocationTable.COLUMN_STREET, LocationTable.COLUMN_SOURCE };

    Log.d("Result", mId + " test");

    Uri uri = ContentUris.withAppendedId(TestContentProvider.CONTENT_URI, mId);
    Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null, null);
    cursor.moveToFirst();/*from   w w  w  .  j av a 2 s . co m*/

    mAddressText.setText(cursor.getString(cursor.getColumnIndex(LocationTable.COLUMN_NAME)) + ", "
            + cursor.getString(cursor.getColumnIndex(LocationTable.COLUMN_STREET)));

    mAddress2Text.setText(cursor.getString(cursor.getColumnIndex(LocationTable.COLUMN_TOWN)) + ", "
            + cursor.getString(cursor.getColumnIndex(LocationTable.COLUMN_CITY)));

    mAddress3Text.setText(cursor.getString(cursor.getColumnIndex(LocationTable.COLUMN_STATE)) + ", "
            + cursor.getString(cursor.getColumnIndex(LocationTable.COLUMN_COUNTRY)));

    if (mAddress2Text.getText().equals(", ")) {
        mAddress2Text.setVisibility(View.GONE);
    } else {
        mAddress2Text.setVisibility(View.VISIBLE);
    }
    if (mAddress3Text.getText().equals(", ")) {
        mAddress3Text.setVisibility(View.GONE);
    } else {
        mAddress3Text.setVisibility(View.VISIBLE);
    }
    String[] sourceArray = getResources().getStringArray(R.array.source_types);
    int sourceType = cursor.getInt(cursor.getColumnIndex(LocationTable.COLUMN_SOURCE));
    if (sourceType > -1) {
        mSourceText.setText(sourceArray[sourceType]);
        mSourceText.setVisibility(View.VISIBLE);
    } else {
        mSourceText.setVisibility(View.GONE);
    }
    Date date = new Date(cursor.getLong(cursor.getColumnIndex(TestTable.COLUMN_DATE)));
    SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy");
    DateFormat tf = android.text.format.DateFormat.getTimeFormat(getActivity()); // Gets system TF

    String dateString = df.format(date.getTime()) + ", " + tf.format(date.getTime());

    mTestType = DataHelper.getTestTitle(getActivity(),
            cursor.getInt(cursor.getColumnIndex(TestTable.COLUMN_TYPE)));
    mTestTypeId = cursor.getInt(cursor.getColumnIndex(TestTable.COLUMN_TYPE));

    mTitleView.setText(mTestType);
    mDateView.setText(dateString);

    Double resultPpm = cursor.getDouble(cursor.getColumnIndex(TestTable.COLUMN_RESULT));

    if (mTestTypeId == Globals.PH_INDEX) {
        mPpmText.setText("");
    } else {
        mPpmText.setText(R.string.ppm);
    }

    if (resultPpm < 0) {
        mResultTextView.setText("0.0");
        //mResultIcon.setVisibility(View.GONE);
        mPpmText.setVisibility(View.GONE);
    } else {
        mResultTextView.setText(String.format("%.2f", resultPpm));

        Context context = getActivity().getApplicationContext();

        int resourceAttribute;

        if (resultPpm <= Globals.FLUORIDE_MAX_DRINK) {
            resourceAttribute = R.attr.drink;
        } else if (resultPpm <= Globals.FLUORIDE_MAX_COOK) {
            resourceAttribute = R.attr.cook;
        } else if (resultPpm <= Globals.FLUORIDE_MAX_BATHE) {
            resourceAttribute = R.attr.bath;
        } else {
            resourceAttribute = R.attr.wash;
        }

        TypedArray a = context.getTheme().obtainStyledAttributes(
                ((MainApp) context.getApplicationContext()).CurrentTheme, new int[] { resourceAttribute });
        int attributeResourceId = a.getResourceId(0, 0);
        //mResultIcon.setImageResource(attributeResourceId);

        //mResultIcon.setVisibility(View.VISIBLE);
        mPpmText.setVisibility(View.VISIBLE);
    }

    cursor.close();
}

From source file:com.ruesga.rview.widget.TagEditTextView.java

private void init(Context ctx, AttributeSet attrs, int defStyleAttr) {
    mHandler = new Handler(mTagMessenger);
    mTriggerTagCreationThreshold = CREATE_CHIP_DEFAULT_DELAYED_TIMEOUT;

    Resources.Theme theme = ctx.getTheme();
    TypedArray a = theme.obtainStyledAttributes(attrs, R.styleable.TagEditTextView, defStyleAttr, 0);

    mReadOnly = a.getBoolean(R.styleable.TagEditTextView_readonly, false);
    mDefaultTagMode = TAG_MODE.HASH;/*from  w w w  .jav  a  2s  .c  o  m*/

    // Create the internal EditText that holds the tag logic
    mTagEdit = mReadOnly ? new TagEditText(ctx, attrs, defStyleAttr) : new TagEditText(ctx, attrs);
    mTagEdit.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0));
    mTagEdit.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    mTagEdit.addTextChangedListener(mEditListener);
    mTagEdit.setTextIsSelectable(false);
    mTagEdit.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    mTagEdit.setOnFocusChangeListener((v, hasFocus) -> {
        // Remove any pending message
        mHandler.removeMessages(MESSAGE_CREATE_CHIP);
    });
    mTagEdit.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    });
    addView(mTagEdit);

    // Configure the window mode for landscape orientation, to disallow hide the
    // EditText control, and show characters instead of chips
    int orientation = ctx.getResources().getConfiguration().orientation;
    if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
        if (ctx instanceof Activity) {
            Window window = ((Activity) ctx).getWindow();
            if (window != null) {
                window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
                mTagEdit.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
            }
        }
    }

    // Save the keyListener for later restore
    mEditModeKeyListener = mTagEdit.getKeyListener();

    // Initialize resources for chips
    mChipBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    mChipFgPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mChipFgPaint.setTextSize(mTagEdit.getTextSize() * (mReadOnly ? 1 : 0.8f));
    if (CHIP_TYPEFACE != null) {
        mChipFgPaint.setTypeface(CHIP_TYPEFACE);
    }
    mChipFgPaint.setTextAlign(Paint.Align.LEFT);

    // Calculate the width area used to remove the tag in the chip
    mChipRemoveAreaWidth = (int) (mChipFgPaint.measureText(CHIP_REMOVE_TEXT) + 0.5f);

    if (ONE_PIXEL <= 0) {
        Resources res = getResources();
        ONE_PIXEL = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, res.getDisplayMetrics());
    }

    int n = a.getIndexCount();
    for (int i = 0; i < n; i++) {
        int attr = a.getIndex(i);
        switch (attr) {
        case R.styleable.TagEditTextView_supportUserTags:
            setSupportsUserTags(a.getBoolean(attr, false));
            break;

        case R.styleable.TagEditTextView_chipBackgroundColor:
            mChipBackgroundColor = a.getColor(attr, mChipBackgroundColor);
            break;

        case R.styleable.TagEditTextView_chipTextColor:
            mChipFgPaint.setColor(a.getColor(attr, Color.WHITE));
            break;
        }
    }
    a.recycle();
}

From source file:gdswww.com.momo.view.SwipeRevealLayout.java

private void init(Context context, AttributeSet attrs) {
    if (attrs != null && context != null) {
        TypedArray a;//  w  w w  . j  ava2  s  .  c o m
        a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SwipeRevealLayout, 0, 0);

        mDragEdge = a.getInteger(R.styleable.SwipeRevealLayout_dragEdge, DRAG_EDGE_LEFT);
        mMinFlingVelocity = a.getInteger(R.styleable.SwipeRevealLayout_flingVelocity,
                DEFAULT_MIN_FLING_VELOCITY);
        //            mMode = a.getInteger(R.styleable.SwipeRevealLayout_mode, MODE_NORMAL);

        mMinDistRequestDisallowParent = a.getDimensionPixelSize(
                R.styleable.SwipeRevealLayout_minDistRequestDisallowParent,
                dpToPx(DEFAULT_MIN_DIST_REQUEST_DISALLOW_PARENT));
    }

    mDragHelper = ViewDragHelper.create(this, 1.0f, mDragHelperCallback);
    mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_ALL);

    mGestureDetector = new GestureDetectorCompat(context, mGestureListener);
}

From source file:com.adkdevelopment.e_contact.utils.UnderlinePageIndicator.java

@TargetApi(Build.VERSION_CODES.M)
public UnderlinePageIndicator(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if (isInEditMode())
        return;//from  ww w .j a  va 2s  .c  om

    final Resources res = getResources();

    //Load defaults from resources
    final boolean defaultFades = res.getBoolean(R.bool.default_underline_indicator_fades);
    final int defaultFadeDelay = res.getInteger(R.integer.default_underline_indicator_fade_delay);
    final int defaultFadeLength = res.getInteger(R.integer.default_underline_indicator_fade_length);

    int defaultSelectedColor;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        defaultSelectedColor = res.getColor(R.color.white, context.getTheme());
    } else {
        //noinspection deprecation
        defaultSelectedColor = res.getColor(R.color.white);
    }

    //Retrieve styles attributes
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.UnderlinePageIndicator, defStyle, 0);

    setFades(a.getBoolean(R.styleable.UnderlinePageIndicator_fades, defaultFades));
    setSelectedColor(a.getColor(R.styleable.UnderlinePageIndicator_selectedColor, defaultSelectedColor));
    setFadeDelay(a.getInteger(R.styleable.UnderlinePageIndicator_fadeDelay, defaultFadeDelay));
    setFadeLength(a.getInteger(R.styleable.UnderlinePageIndicator_fadeLength, defaultFadeLength));

    mCornerRadius = a.getDimension(R.styleable.UnderlinePageIndicator_vpi_corner_radius, 0);

    Drawable background = a.getDrawable(R.styleable.UnderlinePageIndicator_android_background);
    if (background != null) {
        setBackground(background);
    }

    a.recycle();

    final ViewConfiguration configuration = ViewConfiguration.get(context);
    mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
}

From source file:com.ternup.caddisfly.fragment.DetailsFragment.java

private void displayResult() {

    String[] projection = { TestTable.TABLE_TEST + "." + TestTable.COLUMN_ID,
            TestTable.TABLE_TEST + "." + TestTable.COLUMN_DATE, TestTable.COLUMN_RESULT, TestTable.COLUMN_TYPE,
            TestTable.COLUMN_FOLDER, LocationTable.COLUMN_NAME, LocationTable.COLUMN_STREET,
            LocationTable.COLUMN_TOWN, LocationTable.COLUMN_CITY, LocationTable.COLUMN_STATE,
            LocationTable.COLUMN_COUNTRY, LocationTable.COLUMN_STREET, LocationTable.COLUMN_SOURCE };

    Log.d("Result", mId + " test");

    Uri uri = ContentUris.withAppendedId(TestContentProvider.CONTENT_URI, mId);
    Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null, null);
    cursor.moveToFirst();/*from   ww w  . j  a  va  2 s. c  o m*/

    if (cursor.getCount() > 0) {
        mAddressText.setText(cursor.getString(cursor.getColumnIndex(LocationTable.COLUMN_NAME)) + ", "
                + cursor.getString(cursor.getColumnIndex(LocationTable.COLUMN_STREET)));

        mAddress2Text.setText(cursor.getString(cursor.getColumnIndex(LocationTable.COLUMN_TOWN)) + ", "
                + cursor.getString(cursor.getColumnIndex(LocationTable.COLUMN_CITY)));

        mAddress3Text.setText(cursor.getString(cursor.getColumnIndex(LocationTable.COLUMN_STATE)) + ", "
                + cursor.getString(cursor.getColumnIndex(LocationTable.COLUMN_COUNTRY)));

        if (mAddress2Text.getText().equals(", ")) {
            mAddress2Text.setVisibility(View.GONE);
        } else {
            mAddress2Text.setVisibility(View.VISIBLE);
        }
        if (mAddress3Text.getText().equals(", ")) {
            mAddress3Text.setVisibility(View.GONE);
        } else {
            mAddress3Text.setVisibility(View.VISIBLE);
        }
        String[] sourceArray = getResources().getStringArray(R.array.source_types);
        int sourceType = cursor.getInt(cursor.getColumnIndex(LocationTable.COLUMN_SOURCE));
        if (sourceType > -1) {
            mSourceText.setText(sourceArray[sourceType]);
            mSourceText.setVisibility(View.VISIBLE);
        } else {
            mSourceText.setVisibility(View.GONE);
        }
        Date date = new Date(cursor.getLong(cursor.getColumnIndex(TestTable.COLUMN_DATE)));
        SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy");
        DateFormat tf = android.text.format.DateFormat.getTimeFormat(getActivity()); // Gets system TF

        String dateString = df.format(date.getTime()) + ", " + tf.format(date.getTime());

        mTestType = DataHelper.getTestTitle(getActivity(),
                cursor.getInt(cursor.getColumnIndex(TestTable.COLUMN_TYPE)));
        mTestTypeId = cursor.getInt(cursor.getColumnIndex(TestTable.COLUMN_TYPE));

        mTitleView.setText(mTestType);
        mDateView.setText(dateString);

        Double resultPpm = cursor.getDouble(cursor.getColumnIndex(TestTable.COLUMN_RESULT));

        if (mTestTypeId == Globals.PH_INDEX) {
            mPpmText.setText("");
        } else {
            mPpmText.setText(R.string.ppm);
        }

        if (resultPpm < 0) {
            mResultTextView.setText("0.0");
            //mResultIcon.setVisibility(View.GONE);
            mPpmText.setVisibility(View.GONE);
        } else {
            mResultTextView.setText(String.format("%.2f", resultPpm));

            Context context = getActivity().getApplicationContext();

            int resourceAttribute;

            if (resultPpm <= Globals.FLUORIDE_MAX_DRINK) {
                resourceAttribute = R.attr.drink;
            } else if (resultPpm <= Globals.FLUORIDE_MAX_COOK) {
                resourceAttribute = R.attr.cook;
            } else if (resultPpm <= Globals.FLUORIDE_MAX_BATHE) {
                resourceAttribute = R.attr.bath;
            } else {
                resourceAttribute = R.attr.wash;
            }

            TypedArray a = context.getTheme().obtainStyledAttributes(
                    ((MainApp) context.getApplicationContext()).CurrentTheme, new int[] { resourceAttribute });
            int attributeResourceId = a.getResourceId(0, 0);
            //mResultIcon.setImageResource(attributeResourceId);

            //mResultIcon.setVisibility(View.VISIBLE);
            mPpmText.setVisibility(View.VISIBLE);
        }
    }
    cursor.close();
}