Home > Software design >  Focus color is not apply for button in Android
Focus color is not apply for button in Android

Time:03-02

I am trying to set the button background programmatically. But focused color is not applying.

private void drawKeyBackground() {
        String normalColor = "e11515ff";
        String focusedColor = "dd3f3fff";
        Drawable npd = getSelectorDrawable(normalColor,focusedColor);
        Button button = findViewById(R.id.btn1);
        button.setBackground(npd);
    }



public StateListDrawable getSelectorDrawable(String normalColor, String focusedColor) {
        StateListDrawable out = new StateListDrawable();
        out.addState(new int[]{}, getRoundRect(normalColor));
        out.addState(new int[]{ android.R.attr.state_focused}, getRoundRect(focusedColor));
        return out;
    }

public Drawable getRoundRect(String color) {
        RoundRectShape rectShape = new RoundRectShape(new float[]{
                0, 0, 0, 0,
                0, 0, 0, 0
        }, null, null);


        int[] backRGB = new int[3];
        backRGB[0] = Integer.parseInt(color.substring(0,
                2), 16);
        backRGB[1] = Integer.parseInt(color.substring(2,
                4), 16);
        backRGB[2] = Integer.parseInt(color.substring(4),
                16);
        ShapeDrawable shapeDrawable = new ShapeDrawable(rectShape);
        shapeDrawable.getPaint().setColor(Color.rgb(backRGB[0], backRGB[1], backRGB[2]));
        //        shapeDrawable.getPaint().setColor((Styles.getARGBValue(color, Styles.getAlphaValue("sStyleID"))));
        shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        shapeDrawable.getPaint().setAntiAlias(true);
        shapeDrawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
        return shapeDrawable;
    }

Please let me some ideas to resolve this issue. How to set the background for view in android.

CodePudding user response:

Change your parent theme in themes.xml to Theme.AppCompat.Light.NoActionBar or another theme parent.

CodePudding user response:

Here is an example for your problem. You may want to play with it a little bit.

Button button = findViewById(R.id.button);
        
GradientDrawable pressed = new GradientDrawable();
pressed.setShape(GradientDrawable.RECTANGLE);
pressed.setGradientType(GradientDrawable.LINEAR_GRADIENT);
pressed.setColor(Color.WHITE);
pressed.setStroke(10, Color.GREEN);
pressed.setCornerRadius(30F);
        
GradientDrawable focused = new GradientDrawable();
focused.setShape(GradientDrawable.RECTANGLE);
focused.setGradientType(GradientDrawable.LINEAR_GRADIENT);
focused.setColor(Color.WHITE);
focused.setStroke(10, Color.RED);
focused.setCornerRadius(30F);

GradientDrawable enabled = new GradientDrawable();
enabled.setShape(GradientDrawable.RECTANGLE);
enabled.setGradientType(GradientDrawable.LINEAR_GRADIENT);
enabled.setColor(Color.WHITE);
enabled.setStroke(10, Color.BLUE);
enabled.setCornerRadius(30F);
        
StateListDrawable drawables = new StateListDrawable();
drawables.addState(new int[] { android.R.attr.state_pressed }, pressed);
drawables.addState(new int[] { android.R.attr.state_focused }, focused);
drawables.addState(new int[] { android.R.attr.state_enabled }, enabled);
        
button.setBackground(drawables);
//You might not even need this one.
//button.setFocusableInTouchMode(true);
  • Related