Example usage for android.graphics.drawable StateListDrawable StateListDrawable

List of usage examples for android.graphics.drawable StateListDrawable StateListDrawable

Introduction

In this page you can find the example usage for android.graphics.drawable StateListDrawable StateListDrawable.

Prototype

public StateListDrawable() 

Source Link

Usage

From source file:rikka.akashitoolkit.ui.widget.IconSwitchCompat.java

/**
 * Construct a new Switch with a default style determined by the given theme attribute,
 * overriding specific style attributes as requested.
 *
 * @param context      The Context that will determine this widget's theming.
 * @param attrs        Specification of attributes that should deviate from the default styling.
 * @param defStyleAttr An attribute in the current theme that contains a
 *                     reference to a style resource that supplies default values for
 *                     the view. Can be 0 to not look for defaults.
 *//*from   w w  w  .  j ava2 s  .  c om*/
@SuppressLint("PrivateResource")
public IconSwitchCompat(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);

    Resources res = getResources();
    mTextPaint.density = res.getDisplayMetrics().density;

    TintTypedArray a;
    a = TintTypedArray.obtainStyledAttributes(context, attrs,
            android.support.v7.appcompat.R.styleable.SwitchCompat, defStyleAttr, 0);
    mThumbDrawable = a.getDrawable(android.support.v7.appcompat.R.styleable.SwitchCompat_android_thumb);
    if (mThumbDrawable != null) {
        mThumbDrawable.setCallback(this);
    }
    mTrackDrawable = a.getDrawable(android.support.v7.appcompat.R.styleable.SwitchCompat_track);
    if (mTrackDrawable != null) {
        mTrackDrawable.setCallback(this);
    }
    mTextOn = a.getText(android.support.v7.appcompat.R.styleable.SwitchCompat_android_textOn);
    mTextOff = a.getText(android.support.v7.appcompat.R.styleable.SwitchCompat_android_textOff);
    mShowText = a.getBoolean(android.support.v7.appcompat.R.styleable.SwitchCompat_showText, true);
    mThumbTextPadding = a
            .getDimensionPixelSize(android.support.v7.appcompat.R.styleable.SwitchCompat_thumbTextPadding, 0);
    /*mSwitchMinWidth = a.getDimensionPixelSize(
        android.support.v7.appcompat.R.styleable.SwitchCompat_switchMinWidth, 0);*/
    mSwitchPadding = a
            .getDimensionPixelSize(android.support.v7.appcompat.R.styleable.SwitchCompat_switchPadding, 0);
    mSplitTrack = a.getBoolean(android.support.v7.appcompat.R.styleable.SwitchCompat_splitTrack, false);

    final int appearance = a
            .getResourceId(android.support.v7.appcompat.R.styleable.SwitchCompat_switchTextAppearance, 0);
    if (appearance != 0) {
        setSwitchTextAppearance(context, appearance);
    }

    mDrawableManager = AppCompatDrawableManager.get();

    a.recycle();

    a = TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.IconSwitchCompat, defStyleAttr, 0);

    mSwitchMinWidth = a.getDimensionPixelSize(R.styleable.IconSwitchCompat_minWidth, 0);

    Drawable icon = a.getDrawable(R.styleable.IconSwitchCompat_iconDrawable);
    Drawable iconChecked = a.getDrawable(R.styleable.IconSwitchCompat_iconDrawableChecked);

    DrawableCompat.setTintList(icon, a.getColorStateList(R.styleable.IconSwitchCompat_iconColor));

    DrawableCompat.setTintList(iconChecked, a.getColorStateList(R.styleable.IconSwitchCompat_iconColor));

    mIconDrawable = new StateListDrawable();
    ((StateListDrawable) mIconDrawable).addState(CHECKED_STATE_SET, iconChecked);
    ((StateListDrawable) mIconDrawable).addState(new int[] {}, icon);

    if (mIconDrawable != null) {
        mIconDrawable.setCallback(this);
    }

    /*if (mThumbDrawable != null) {
    DrawableCompat.setTintList(mThumbDrawable,
            a.getColorStateList(R.styleable.IconSwitchCompat_thumbColor));
    }
            
    if (mTrackDrawable != null) {
    DrawableCompat.setTintList(mTrackDrawable,
            a.getColorStateList(R.styleable.IconSwitchCompat_trackColor));
    }*/

    a.recycle();

    ViewConfiguration config = ViewConfiguration.get(context);
    mTouchSlop = config.getScaledTouchSlop();
    mMinFlingVelocity = config.getScaledMinimumFlingVelocity();

    // Refresh display with current params
    refreshDrawableState();
    setChecked(isChecked());
}

From source file:com.appeaser.sublimepickerlibrary.utilities.SUtils.java

private static Drawable createOverflowButtonBgBC(int pressedStateColor) {
    StateListDrawable sld = new StateListDrawable();
    sld.addState(new int[] { android.R.attr.state_pressed },
            createBgDrawable(pressedStateColor, 0, CORNER_RADIUS, 0, 0));
    sld.addState(new int[] {}, new ColorDrawable(Color.TRANSPARENT));
    return sld;/*from w  w  w .  jav a 2s. c  om*/
}

From source file:com.xandy.calendar.month.MonthByWeekFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mListView.setSelector(new StateListDrawable());
    mListView.setOnTouchListener(this);

    if (!mIsMiniMonth) {
        mListView.setBackgroundColor(getResources().getColor(R.color.month_bgcolor));
    }/*from  ww w . ja  v  a 2s .  co m*/

    // To get a smoother transition when showing this fragment, delay loading of events until
    // the fragment is expended fully and the calendar controls are gone.
    if (mShowCalendarControls) {
        mListView.postDelayed(mLoadingRunnable, mEventsLoadingDelay);
    } else {
        mLoader = (CursorLoader) getLoaderManager().initLoader(0, null, this);
    }
    mAdapter.setListView(mListView);
}

From source file:com.android.calendar.month.MonthByWeekFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mListView.setSelector(new StateListDrawable());
    mListView.setOnTouchListener(this);

    if (!mIsMiniMonth) {
        mListView.setBackgroundColor(getResources().getColor(R.color.month_bgcolor));
    }/*from   ww  w.  ja  va2  s.  c om*/

    // To get a smoother transition when showing this fragment, delay
    // loading of events until
    // the fragment is expended fully and the calendar controls are gone.
    if (mShowCalendarControls) {
        mListView.postDelayed(mLoadingRunnable, mEventsLoadingDelay);
    } else {
        mLoader = (CursorLoader) getLoaderManager().initLoader(0, null, this);
    }
    mAdapter.setListView(mListView);
}

From source file:com.oginotihiro.datepicker.DatePickerDialog.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    int currentView = MONTH_AND_DAY_VIEW;
    int listPosition = -1;
    int listPositionOffset = 0;

    if (savedInstanceState != null) {
        currentView = savedInstanceState.getInt(KEY_CURRENT_VIEW);
        listPosition = savedInstanceState.getInt(KEY_LIST_POSITION);
        listPositionOffset = savedInstanceState.getInt(KEY_LIST_POSITION_OFFSET);
    }/*from  www.  j  a  va2s.  co m*/

    View view = inflater.inflate(R.layout.oginotihiro_date_picker_dialog, container, false);

    mDayOfWeekView = (TextView) view.findViewById(R.id.datePickerHeader);

    mMonthAndDayView = (LinearLayout) view.findViewById(R.id.date_picker_month_and_day);
    mMonthAndDayView.setOnClickListener(this);

    mSelectedMonthTv = (TextView) view.findViewById(R.id.date_picker_month);
    mSelectedDayTv = (TextView) view.findViewById(R.id.date_picker_day);
    mSelectedYearTv = (TextView) view.findViewById(R.id.date_picker_year);
    mSelectedYearTv.setOnClickListener(this);

    int[][] states = new int[][] { new int[] { -android.R.attr.state_pressed, -android.R.attr.state_selected },
            new int[] { -android.R.attr.state_pressed, android.R.attr.state_selected },
            new int[] { android.R.attr.state_pressed } };
    int[] colors = new int[] { getResources().getColor(R.color.date_picker_text_normal), mColor, mDarkColor };
    ColorStateList csl = new ColorStateList(states, colors);

    mSelectedMonthTv.setTextColor(csl);
    mSelectedDayTv.setTextColor(csl);
    mSelectedYearTv.setTextColor(csl);

    Activity activity = getActivity();
    mYearPickerView = new YearPickerView(activity, this);
    mDayPickerView = new DayPickerView(activity, this);

    mAnimator = (ViewAnimator) view.findViewById(R.id.animator);
    mAnimator.addView(mDayPickerView);
    mAnimator.addView(mYearPickerView);

    AlphaAnimation inAlphaAnimation = new AlphaAnimation(0.0F, 1.0F);
    inAlphaAnimation.setDuration(300);
    mAnimator.setInAnimation(inAlphaAnimation);

    AlphaAnimation outAlphaAnimation = new AlphaAnimation(1.0F, 0.0F);
    outAlphaAnimation.setDuration(300);
    mAnimator.setOutAnimation(outAlphaAnimation);

    mDoneButton = (Button) view.findViewById(R.id.done);

    StateListDrawable sld = new StateListDrawable();
    sld.addState(new int[] { android.R.attr.state_pressed },
            new ColorDrawable(Color.argb(60, Color.red(mColor), Color.green(mColor), Color.blue(mColor))));

    mDoneButton.setOnClickListener(this);

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        mDoneButton.setBackgroundDrawable(sld);
    } else {
        mDoneButton.setBackground(sld);
    }

    updateDisplay();
    setCurrentView(currentView, true);

    if (listPosition != -1) {
        if (currentView == MONTH_AND_DAY_VIEW) {
            mDayPickerView.postSetSelection(listPosition);
        }
        if (currentView == YEAR_VIEW) {
            mYearPickerView.postSetSelectionFromTop(listPosition, listPositionOffset);
        }
    }
    return view;
}

From source file:com.example.util.ImageUtils.java

/**
 *  //from   w ww.  j  av a  2s  .c  o m
 */
private static StateListDrawable getMaskDrawable(Context context) {
    StateListDrawable stateDrawable = new StateListDrawable();
    int statePressed = android.R.attr.state_pressed;
    final Resources res = context.getResources();
    stateDrawable.addState(new int[] { statePressed },
            new BitmapDrawable(res, BitmapFactory.decodeResource(res, R.drawable.banner_pressed)));
    return stateDrawable;
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static StateListDrawable createSelector(Drawable normal, Drawable pressed) {
    StateListDrawable selector = new StateListDrawable();

    selector.addState(new int[] { android.R.attr.state_pressed }, pressed.mutate());

    selector.addState(new int[0], normal.mutate());

    return selector;
}

From source file:org.androidwhite.icons.ui.MainActivity.java

private void setupNavDrawer() {
    assert mNavView != null;
    assert mDrawer != null;
    mNavView.getMenu().clear();/*from w ww  . j  a v a 2s . c om*/
    for (PagesBuilder.Page page : mPages)
        page.addToMenu(mNavView.getMenu());

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        getWindow().setStatusBarColor(Color.TRANSPARENT);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mDrawer.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {

            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
                //TODO: Check if NavigationView needs bottom padding
                WindowInsets drawerLayoutInsets = insets.replaceSystemWindowInsets(
                        insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(),
                        insets.getSystemWindowInsetRight(), 0);
                mDrawerModeTopInset = drawerLayoutInsets.getSystemWindowInsetTop();
                ((DrawerLayout) v).setChildInsets(drawerLayoutInsets,
                        drawerLayoutInsets.getSystemWindowInsetTop() > 0);
                return insets;
            }
        });
    }

    assert getSupportActionBar() != null;
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    Drawable menuIcon = ContextCompat.getDrawable(this, R.drawable.ic_action_menu);
    menuIcon = TintUtils.createTintedDrawable(menuIcon, DialogUtils.resolveColor(this, R.attr.tab_icon_color));
    getSupportActionBar().setHomeAsUpIndicator(menuIcon);

    mDrawer.addDrawerListener(
            new ActionBarDrawerToggle(this, mDrawer, mToolbar, R.string.drawer_open, R.string.drawer_close));
    mDrawer.setStatusBarBackgroundColor(DialogUtils.resolveColor(this, R.attr.colorPrimaryDark));
    mNavView.setNavigationItemSelectedListener(this);

    final ColorDrawable navBg = (ColorDrawable) mNavView.getBackground();
    final int selectedIconText = DialogUtils.resolveColor(this, R.attr.colorAccent);
    int iconColor;
    int titleColor;
    int selectedBg;
    if (TintUtils.isColorLight(navBg.getColor())) {
        iconColor = ContextCompat.getColor(this, R.color.navigationview_normalicon_light);
        titleColor = ContextCompat.getColor(this, R.color.navigationview_normaltext_light);
        selectedBg = ContextCompat.getColor(this, R.color.navigationview_selectedbg_light);
    } else {
        iconColor = ContextCompat.getColor(this, R.color.navigationview_normalicon_dark);
        titleColor = ContextCompat.getColor(this, R.color.navigationview_normaltext_dark);
        selectedBg = ContextCompat.getColor(this, R.color.navigationview_selectedbg_dark);
    }

    final ColorStateList iconSl = new ColorStateList(new int[][] { new int[] { -android.R.attr.state_checked },
            new int[] { android.R.attr.state_checked } }, new int[] { iconColor, selectedIconText });
    final ColorStateList textSl = new ColorStateList(new int[][] { new int[] { -android.R.attr.state_checked },
            new int[] { android.R.attr.state_checked } }, new int[] { titleColor, selectedIconText });
    mNavView.setItemTextColor(textSl);
    mNavView.setItemIconTintList(iconSl);

    StateListDrawable bgDrawable = new StateListDrawable();
    bgDrawable.addState(new int[] { android.R.attr.state_checked }, new ColorDrawable(selectedBg));
    mNavView.setItemBackground(bgDrawable);

    mPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            dispatchFragmentUpdateTitle(false);
            invalidateNavViewSelection(position);
        }
    });

    mToolbar.setContentInsetsRelative(getResources().getDimensionPixelSize(R.dimen.second_keyline), 0);
}

From source file:com.androguide.honamicontrol.MainActivity.java

private StateListDrawable getColouredTouchFeedback() {
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.parseColor(mAppColor)));
    states.addState(new int[] { android.R.attr.state_focused }, new ColorDrawable(Color.parseColor(mAppColor)));
    states.addState(new int[] {}, getResources().getDrawable(android.R.color.transparent));
    return states;
}

From source file:com.apptentive.android.sdk.util.Util.java

/**
 * helper method to generate the ImageButton background with specified highlight color.
 *
 * @param selected_color the color shown as highlight
 * @return/* w  w w . j  av  a  2  s.co m*/
 */
public static StateListDrawable getSelectableImageButtonBackground(int selected_color) {
    ColorDrawable selectedColor = new ColorDrawable(selected_color);
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_pressed }, selectedColor);
    states.addState(new int[] { android.R.attr.state_activated }, selectedColor);
    return states;
}