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:com.appeaser.sublimenavigationviewlibrary.SublimeThemer.java

private void setDefaultItemBackground() {
    TypedValue value = new TypedValue();
    int colorControlHighlight = 0;
    if (mContext.getTheme().resolveAttribute(R.attr.colorControlHighlight, value, true)) {
        colorControlHighlight = value.data;
    } else {/*w ww  . j  av a 2 s.  c  o  m*/
        colorControlHighlight = mDefaultTheme == DefaultTheme.LIGHT
                ? mContext.getResources().getColor(R.color.snv_ripple_material_light)
                : mContext.getResources().getColor(R.color.snv_ripple_material_dark);
    }

    StateListDrawable drawable = new StateListDrawable();
    LayerDrawable checked = new LayerDrawable(
            new Drawable[] { new ColorDrawable(colorControlHighlight), obtainSelectableItemBackground() });
    drawable.addState(CHECKED_STATE_SET, checked);
    drawable.addState(EMPTY_STATE_SET, obtainSelectableItemBackground());
    mItemBackground = drawable;
}

From source file:de.mrapp.android.view.FloatingActionButton.java

/**
 * Creates and returns a state list drawable, which can be used as the floating action button
 * background and adapts the background color depending on the button's current state.
 *
 * @return The drawable, which has been created, as an instance of the class {@link Drawable}
 */// w w w  .  ja v  a2  s .co  m
private Drawable createStateListBackgroundDrawable() {
    StateListDrawable drawable = new StateListDrawable();

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        drawable.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_pressed },
                createPressedBackgroundDrawable());
    }

    drawable.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_activated },
            createActivatedBackgroundDrawable());
    drawable.addState(new int[] { android.R.attr.state_enabled }, createBackgroundDrawable(getColor()));
    drawable.addState(new int[] {}, createDisabledBackgroundDrawable());
    return drawable;
}

From source file:com.moki.touch.fragments.views.WebContent.java

private StateListDrawable createButtonStateList(int pressedState, int nonPressedState) {
    StateListDrawable stateList = new StateListDrawable();
    stateList.addState(new int[] { android.R.attr.state_pressed },
            context.getResources().getDrawable(pressedState));
    stateList.addState(new int[] { -android.R.attr.state_pressed },
            context.getResources().getDrawable(nonPressedState));

    return stateList;
}

From source file:com.afollestad.polar.ui.MainActivity.java

private void setupNavDrawer() {
    assert mNavView != null;
    assert mDrawer != null;
    mNavView.getMenu().clear();/*from  w w  w . j  a v  a  2s .co m*/
    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 = VC.get(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:org.creativecommons.thelist.activities.RandomActivity.java

public void setButtonTheme(ImageButton button, int normalState, int pressedState, int focusedState) {
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(pressedState));
    states.addState(new int[] { android.R.attr.state_focused }, getResources().getDrawable(focusedState));
    states.addState(new int[] {}, getResources().getDrawable(normalState));
    button.setImageDrawable(states);/*from  w  ww.  j  a  v  a 2 s .  c o m*/
}

From source file:com.mappn.gfan.ui.HomeTabActivity.java

private void drawUpdateCount(Activity context, Resources res, ImageView view, boolean flag) {
    DisplayMetrics dm = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(dm);
    Bitmap cornerRes = BitmapFactory.decodeResource(res, R.drawable.notify_update);
    Bitmap appBitmapNormal = BitmapFactory.decodeResource(res, R.drawable.main_tab_app_unselect);
    Bitmap appBitmapPressed = BitmapFactory.decodeResource(res, R.drawable.main_tab_app_select);

    StateListDrawable stateDrawable = new StateListDrawable();
    int stateSelected = android.R.attr.state_selected;
    if (flag) {/*from  www.j  a v a2s . c  o m*/
        Bitmap cornerBitmap = drawText(dm, res, cornerRes, mSession.getUpgradeNumber());
        Bitmap newBitmapNormal = drawBitmap(dm, appBitmapNormal, cornerBitmap);
        Bitmap newBitmapPressed = drawBitmap(dm, appBitmapPressed, cornerBitmap);

        stateDrawable.addState(new int[] { -stateSelected }, new BitmapDrawable(res, newBitmapNormal));
        stateDrawable.addState(new int[] { stateSelected }, new BitmapDrawable(res, newBitmapPressed));

        view.setImageDrawable(stateDrawable);
    } else {

        view.setImageResource(R.drawable.main_tab_app_manager_selector);
    }
}

From source file:com.winneredge.stockly.wcommons.floatingactionwidget.FloatingActionButton.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[] { -android.R.attr.state_enabled }, createCircleDrawable(mColorDisabled));
    drawable.addState(new int[] { android.R.attr.state_pressed }, createCircleDrawable(mColorPressed));
    drawable.addState(new int[] {}, createCircleDrawable(mColorNormal));

    if (Util.hasLollipop()) {
        RippleDrawable ripple = new RippleDrawable(
                new ColorStateList(new int[][] { {} }, new int[] { mColorRipple }), drawable, null);
        setOutlineProvider(new ViewOutlineProvider() {
            @Override/* w w  w. j  ava  2 s .co m*/
            public void getOutline(View view, Outline outline) {
                outline.setOval(0, 0, view.getWidth(), view.getHeight());
            }
        });
        setClipToOutline(true);
        mBackgroundDrawable = ripple;
        return ripple;
    }

    mBackgroundDrawable = drawable;
    return drawable;
}

From source file:com.chenl.widgets.flippablestackview.indicator.OrientedPagerSlidingTabLayout.java

private static final StateListDrawable createStateDrawable() {
    StateListDrawable stateListDrawable = new StateListDrawable();
    ColorDrawable normal = new ColorDrawable(Color.TRANSPARENT);
    ColorDrawable pressed = new ColorDrawable(Color.parseColor("#66000000"));
    stateListDrawable.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled },
            pressed);/*from ww  w . j  av  a2s  .co  m*/
    stateListDrawable.addState(new int[] { android.R.attr.state_pressed }, pressed);
    stateListDrawable.addState(new int[] { android.R.attr.state_enabled }, normal);
    stateListDrawable.addState(new int[] {}, normal);
    return stateListDrawable;
}

From source file:git.egatuts.nxtremotecontroller.activity.ControllerActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setActiveTheme(super.getPreferenceTheme());
    super.setContentView(R.layout.controller_layout);
    toolbar = (Toolbar) this.findViewById(R.id.toolbar);
    super.setSupportToolbar();
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override//ww  w .  j a v a2 s  .  c  om
        public void onClick(View v) {
            ControllerActivity.this.onBackPressed();
        }
    });

    /*
     *  We get the device from the extra data of the intent.
     */
    this.device = this.getIntent().getParcelableExtra("device");

    /*
     *  We create the final variables we will use in the listeners, etc.
     */
    final ControllerActivity self = this;
    final GlobalUtils utils = this.getGlobalUtils();

    /*
     *  We create the AlertDialog.Builder we will show to ask the user to reconnect with the device.
     */
    this.builder = utils
            .createAlertDialog(utils.getStringResource(R.string.connecting_reconnect_title),
                    utils.format(R.string.connecting_reconnect_message, this.device.getName()))
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    self.finish();
                }
            }).setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    self.resume();
                }
            });

    /*
     *  Now we define the progress dialog we will show while we are connecting with the device.
     *  When the progress dialog has been cancelled it means the connection process has been cancelled.
     *  But on dismiss we have to check if the connection failed or it succeed.
     */
    this.progressDialog = this.getLongProgressDialog();
    this.progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            self.aborted = true;
            utils.showToast(R.string.connecting_aborted);
            self.connector.closeConnectThread();
            self.finish();
        }
    });

    /*
     *  Now we declare the handler that will handle (so obviously) the messages
     *  sent by the threads that are in the background connecting with the device.
     */
    this.handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if ((self.connector.getConnectThread() == null || self.aborted)
                    && self.connector.getConnectedThread() == null) {
                return;
            }
            int category = msg.what;
            int state;
            int error;
            if (category == NXTConnector.WHAT_CHANGE_STATE) {
                progressDialog.show();
                state = msg.arg1;
                error = msg.arg2;
                if (NXTConnector.isPreparingConnection(state)) {
                    progressDialog.setText(R.string.connecting_preparing_connection);
                } else if (NXTConnector.isCreatingSocket(state)) {
                    progressDialog.setText(R.string.connecting_creating_socket);
                } else if (NXTConnector.isConnecting(state)) {
                    progressDialog.setText(R.string.connecting_connecting);
                } else if (NXTConnector.isConnected(state)) {
                    progressDialog.dismiss();
                    utils.showToast(R.string.connecting_connected, device.getName());
                    self.connected();
                }
            } else if (category == NXTConnector.WHAT_ERROR_ENCOUNTERED) {
                progressDialog.dismiss();
                self.connector.closeAllThreads();
                error = msg.arg1;
                state = msg.arg2;
                boolean notReconnect = false;
                if (NXTConnector.connectionClosed(state, error)) {
                    utils.showToast(R.string.connecting_connection_closed);
                    notReconnect = true;
                    ControllerActivity.this.finish();
                } else if (NXTConnector.connectionLost(state, error)) {
                    utils.showToast(R.string.connecting_connection_lost);
                    if (!self.connector.getBluetoothUtils().isEnabled()) {
                        notReconnect = true;
                        ControllerActivity.this.finish();
                    }
                } else if (NXTConnector.connectionSocketFailed(state, error)) {
                    utils.showToast(R.string.connecting_socket_error);
                } else if (NXTConnector.connectionRequestFailed(state, error)) {
                    utils.showToast(R.string.connecting_request_failed);
                } else if (NXTConnector.connectionUnexpectedFailed(state, error)) {
                    utils.showToast(R.string.connecting_unexpected_error);
                }
                if (!notReconnect) {
                    self.builder.show();
                }
            }
        }
    };

    /*
     *  Now we create the connector with the device and the handler
     *  which we will use to connect and send messages to the robot.
     */
    this.connector = new NXTConnector(this, this.device, this.handler);

    /*
     *  We set the title with the device name.
     */
    String title = utils.getStringResource(R.string.controller_window_title);
    this.setTitle(title + this.device.getName());

    /*
     *  We set the colors we will use with the drawables used in the tabs.
     */
    final int underlineColor = utils.getAttribute(R.attr.toolbar_color);
    final int backgroundColor = utils.getAttribute(R.attr.toolbar_background);
    final int lightBackgroundColor = GlobalUtils.mixColors(backgroundColor, 0x55FFFFFF);

    /*
     *  Now we create the drawables used in all the states of the tabs and then we assign
     *  them to a StateListDrawable to add it to the tabs.
     */
    ShapeDrawable.ShaderFactory tabSelectedFactory = new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(0, 0, /* Origin of the background (top left corner) */
                    0, height, /* End of the background (bottom left corner) */
                    new int[] { backgroundColor,
                            backgroundColor, /* The first gradient doesn't change color so it's like a rectangle shape */
                            underlineColor, underlineColor /* The same for the second one */
            }, new float[] { 0, 51f / 55f, /* The first background covers 51dp out of 55dp */
                    51f / 55f, 1 /* And the second one takes the rest of the space */
            }, Shader.TileMode.REPEAT /* The repeat mode doesn't mind but this would look prettier in case of error */
            );
        }
    };
    PaintDrawable tabSel = new PaintDrawable();
    tabSel.setShape(new RectShape());
    tabSel.setShaderFactory(tabSelectedFactory);

    ShapeDrawable.ShaderFactory tabSelectedAndPressedFactory = new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(0, 0, /* Origin of the background (top left corner) */
                    0, height, /* End of the background (bottom left corner) */
                    new int[] { lightBackgroundColor,
                            lightBackgroundColor, /* The first gradient doesn't change color so it's like a rectangle shape */
                            underlineColor, underlineColor /* The same for the second one */
            }, new float[] { 0, 51f / 55f, /* The first background covers 51dp out of 55dp */
                    51f / 55f, 1 /* And the second one takes the rest of the space */
            }, Shader.TileMode.REPEAT /* The repeat mode doesn't mind but this would look prettier in case of error */
            );
        }
    };
    PaintDrawable tabSelAndPress = new PaintDrawable();
    tabSelAndPress.setShape(new RectShape());
    tabSelAndPress.setShaderFactory(tabSelectedAndPressedFactory);

    /*
     *  Now we create the states lists for the drawables and the colors.
     */
    StateListDrawable drawableList = new StateListDrawable();
    drawableList.addState(new int[] { -android.R.attr.state_selected, android.R.attr.state_pressed },
            new ColorDrawable(lightBackgroundColor));
    drawableList.addState(new int[] { -android.R.attr.state_selected, android.R.attr.state_pressed,
            android.R.attr.state_focused }, new ColorDrawable(lightBackgroundColor));
    drawableList.addState(new int[] { android.R.attr.state_selected, -android.R.attr.state_pressed }, tabSel);
    drawableList.addState(new int[] { android.R.attr.state_selected, -android.R.attr.state_pressed,
            -android.R.attr.state_focused }, tabSel);
    drawableList.addState(new int[] { android.R.attr.state_selected, android.R.attr.state_pressed },
            tabSelAndPress);
    drawableList.addState(new int[] { android.R.attr.state_selected, android.R.attr.state_pressed,
            android.R.attr.state_focused }, tabSelAndPress);
    drawableList.addState(new int[] {}, new ColorDrawable(backgroundColor));

    int darkColor = utils.getAttribute(R.attr.toolbar_color);
    int lightColor = Color.argb(0xAA, Color.red(darkColor), Color.green(darkColor), Color.blue(darkColor));
    int[][] states = new int[][] { new int[] { -android.R.attr.state_selected, android.R.attr.state_pressed },
            new int[] { -android.R.attr.state_selected, android.R.attr.state_pressed,
                    android.R.attr.state_focused },
            new int[] { android.R.attr.state_selected, -android.R.attr.state_pressed },
            new int[] { android.R.attr.state_selected, -android.R.attr.state_pressed,
                    -android.R.attr.state_focused },
            new int[] { android.R.attr.state_selected, android.R.attr.state_pressed },
            new int[] { android.R.attr.state_selected, android.R.attr.state_pressed,
                    android.R.attr.state_focused },
            new int[] { -android.R.attr.state_selected, -android.R.attr.state_pressed,
                    -android.R.attr.state_focused },
            new int[] {} };
    int[] colors = new int[] { lightColor, /* Text color when pressed and not selected */
            lightColor, /* Text color when pressed (with focused fallback) */
            darkColor, /* Text color when selected and not pressed */
            darkColor, /* Text color when selected and not pressed (with focused fallback) */
            darkColor, /* Text color when selected and pressed */
            darkColor, /* Text color when selected and pressed (with focused fallback) */
            lightColor, /* Normal color when not pressed, selected or focused */
            lightColor /* Default text color */
    };
    ColorStateList colorList = new ColorStateList(states, colors);

    /*
     *  We assign the drawable and the list to the activity instance to be accessible everywhere.
     */
    this.tabSelected = tabSel;
    this.tabSelectedAndPressed = tabSelAndPress;
    this.tabDrawableList = drawableList;
    this.tabColorList = colorList;

    /*
     *  Now we setup the tab host and add the tabs to the view.
     */
    this.tabHost = (FragmentTabHost) this.findViewById(R.id.tabhost);
    this.tabHost.setup(this, super.fragmentManager, R.id.tabcontent);
    this.tabHost.getTabWidget().setDividerDrawable(null);
    this.addTab(this.createTabView(R.layout.controller_tab, R.string.controller_tab_local_title),
            R.string.controller_tab_local_spec, LocalControllerFragment.class);
    this.addTab(this.createTabView(R.layout.controller_tab, R.string.controller_tab_online_title),
            R.string.controller_tab_online_spec, OnlineControllerFragment.class);
}

From source file:aierjun.com.aierjunlibrary.widget.tablayout.PagerSlidingTabStrip.java

public StateListDrawable createColorStateListDrawable(@ColorInt int normal, @ColorInt int checked) {
    StateListDrawable bg = new StateListDrawable();
    Drawable normalDrawable = new ColorDrawable(normal);
    Drawable pressedDrawable = new ColorDrawable(checked);
    Drawable focusedDrawable = new ColorDrawable(checked);
    // View.PRESSED_ENABLED_STATE_SET
    bg.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressedDrawable);
    // View.ENABLED_FOCUSED_STATE_SET
    bg.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focusedDrawable);
    // View.ENABLED_STATE_SET
    bg.addState(new int[] { android.R.attr.state_enabled }, normalDrawable);
    // View.FOCUSED_STATE_SET
    bg.addState(new int[] { android.R.attr.state_focused }, focusedDrawable);
    // View.EMPTY_STATE_SET
    bg.addState(new int[] {}, normalDrawable);
    return bg;/*from   ww w .j a v a  2 s .c o  m*/
}