Question

I am creating a color chooser from a SeekBar. So far, I have successfully replaced the progress drawable in onCreate() as follows:

    SeekBar colorChooser = (SeekBar) findViewById(R.id.seekBarColorChooser);

    LinearGradient rainbow = new LinearGradient(0f, 0f, 100f, 0f,
            new int[] { 0xFFFF0000, 0xFFFFFF00, 0xFF00FF00, 
                                    0xFF00FFFF, 0xFF0000FF,
                                    0xFFFF00FF, 0xFFFF0000}, null, TileMode.CLAMP);

    RoundRectShape roundedRect = new RoundRectShape(new float [] {10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f} , null, null);

    ShapeDrawable shape = new ShapeDrawable(roundedRect);
    shape.getPaint().setShader(rainbow);

    colorChooser.setProgressDrawable((Drawable) shape);

This works perfectly! The result is exactly what I am looking for, however, the one thing that concerns me is the hard coded width (100f) in the LinearGradient.

Since this dialog is not yet realized, neither the SeekBar or the Dialog seem to have any width. In other words, colorChooser.getWidth() returns 0.

If width is too small, the gradient is compacted on the left side. If it is too large, only a part of the gradient appears. How do I get a good width value to setup the linear gradient to display properly?

Thank you!

Was it helpful?

Solution

You could add a dimension to your resources, which, of course, can have different values for different screens - it will also scale appropriately if you don't have a specific dpi defined (for instance).

OTHER TIPS

Replace

LinearGradient rainbow = new LinearGradient(0f, 0f, 100f, 0f,
        new int[] { 0xFFFF0000, 0xFFFFFF00, 0xFF00FF00, 
                                0xFF00FFFF, 0xFF0000FF,
                                0xFFFF00FF, 0xFFFF0000}, null, TileMode.CLAMP); 

with

LinearGradient rainbow = new LinearGradient(0f, 0f, 500f, 0f,
        new int[] { 0xFFFF0000, 0xFFFFFF00, 0xFF00FF00, 
                                0xFF00FFFF, 0xFF0000FF,
                                0xFFFF00FF, 0xFFFF0000}, null, TileMode.CLAMP); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top