I use a vertical Seekbar library as a Color Picker. I managed to set the ProgressDrawable as a shape with gradient that represent the Colors of the seekbar.
LinearGradient test = new LinearGradient(0.f, conv.toPixels(170), 0.0f, 0.0f,
new int[]{0xFF000050, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF,
0xFFFF0000, 0xFFFF00FF, 0xFFFFFF00, 0xFFFFFFFF},
null, Shader.TileMode.CLAMP);
ShapeDrawable shape = new ShapeDrawable(new RectShape());
shape.getPaint().setShader(test);
Here I am creating the gradient and parse him as a Shape.After this I set the shape as ProgressDrawable of the seekbar.
colorSeekbar.setMax(252 - 1);
colorSeekbar.setProgressDrawable(shape);
Finally I add my seekbar:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(conv.toPixels(80), conv.toPixels(200));
params.setMargins(conv.toPixels(5), conv.toPixels(5), conv.toPixels(5), conv.toPixels(5));
colorSeekbar.setThumb(ResourcesCompat.getDrawable(getResources(), R.drawable.seek_thumb_color, null));
colorSeekbar.setOrientation(UniversalSeekBar.BOTTOM_TO_TOP);
popUpBulbContainer.addView(colorSeekbar, params);
The red background is the background of the seekbar.The gradient is the progressDrawable and the blue background is the background of a Horizontal LinearLayout that hold all the views.
You can notice the thumb being set on the top of the seekbar where the progressDrawable can't get there.
Any Ideea how can I solve this ?
LinearLayout seems correct. It must be something to do with your parent layout in activity layout file.
If you are adding all those elements programatically make sure you attach the elements (popUpBulbContainer) to the top layout of the activity.
Related
Colour Code : e32b0f, d03559, I want to design its background drawable please help me
Try,
int colors[] = { 0xff255779, 0xfcc6c0cd };
GradientDrawable gradientDrawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
view.setBackgroundDrawable(gradientDrawable);
1. choose color and add color hex code in colors array.
2. GradientDrawable using and set orientation properties.[ex. GradientDrawable.Orientation.TOP_BOTTOM]
3. set view background [ex. Linearlayout.setBackgroundDrawable(gradientDrawable);]
I made this Icon in Photoshop, but now I want to make the gradiant in Android and use it as the Textcolor for a Textview
The colors and settings from Photoshop is:
Style: Linear
Angle: 90*
Colors: #00a8d9 (0%), #223595 (20%), #f1328f(40%), #ee3031(60%), #f9e63a(80%), #009958(100%)
The numbers in the parentes is the location value.
I have tried the following, but its only showing one color
LinearGradient linearGradient = new LinearGradient(20,30,40,50, new int[] {Color.parseColor("#00a8d9"), Color.parseColor("#223595"), Color.parseColor("#f1328f"), Color.parseColor("#ee3031"), Color.parseColor("#f9e63a"), Color.parseColor("#009958")},new float[]{0,0,0,0,0,0},
Shader.TileMode.REPEAT);
textView.getPaint().setShader(linearGradient);
I managed to replicate the gradient from the picture to the code with this solution:
textView = (TextView) v.findViewById(R.id.fontcolorwhite);
LinearGradient linearGradient = new LinearGradient(60,-70,60,70, new int[]{Color.parseColor("#00a8d9"), Color.parseColor("#4244b8"), Color.parseColor("#f1328f"), Color.parseColor("#ee3031"), Color.parseColor("#fde92d"), Color.parseColor("#009e54")},null,
Shader.TileMode.MIRROR);
textView.getPaint().setShader(linearGradient);
Another problem is that the gradient dosn't look the same with different screen sizes. This will be in a another question
You may Try this.
Try to set this as your view background colour and set colour as per your need.
GradientDrawable rainbow = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED});
textview.setBackground(rainbow);
Currently I am trying to create a custom Seekbar without using xml as the images for the background and progress bar need to be changed. This is what I have at the moment:
Drawable drawable = new BitmapDrawable(appState.images.get(Integer.parseInt(image)));
this.setBackgroundDrawable(drawable);
Drawable drawable2 = new BitmapDrawable(appState.images.get(Integer.parseInt(image_a)));
this.setProgressDrawable(drawable2);
this.setProgress(0);
But the progressDrawable is just set to the entire size of Seekbar and when I move the thumb from side to side nothing happens. Does anybody have any ideas as I am completely stumped.
So this previous answer does the trick for you:
Changing the size of the seekbar programmatically but cant get the thumb to be larger than the bar
From the original asker:
In the end I am using this code:
public void setSeekBarImages(){
Drawable drawable = new
BitmapDrawable(appState.images.get(Integer.parseInt(image_a)));
ClipDrawable clip = new ClipDrawable(drawable,
Gravity.LEFT,ClipDrawable.HORIZONTAL);
Drawable drawable2 = new
BitmapDrawable(appState.images.get(Integer.parseInt(image)));
InsetDrawable d1= new InsetDrawable(drawable2,5,5,5,5);
//the padding u want to use
setThumb(null);
LayerDrawable mylayer = new LayerDrawable(new Drawable[]{d1,clip});
setProgressDrawable(mylayer);
setProgress(0);
}
I have to make a seekbar where i can change almost everything programmatically. And now I'm having a problem to change the thumb and the bar size. Everything works great if the bar is larger or the same size as the thumb, but if the thumb is larger than the bar, I can't get it to show the entire thumb with the correct size of the bar.
Here's my code to create the thumb:
public void setSeekBarThumb(int width, int height, int rColor, int gColor, int bColor){
ShapeDrawable thumb = new ShapeDrawable( new OvalShape() );
thumb.setIntrinsicHeight( height );
thumb.setIntrinsicWidth( width );
thumb.setBounds(new Rect(0, 0, width, height));
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},thumb );//add to the state list with the state pressed
thumb = new ShapeDrawable( new OvalShape() );
thumb.setIntrinsicHeight( height );
thumb.setIntrinsicWidth( width );
thumb.setBounds(new Rect(0, 0, width, height));
states.addState(new int[] { },thumb );//add to the state list with no state
seekbar.setThumb(states);
seekbar.setThumbOffset(0);
}
heres my code to create the bar:
public void setSeekBarProgress(int width, int height, int rColor, int gColor, int bColor){
GradientDrawable shape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE,Color.rgb(rColor, gColor, bColor)});
shape.setCornerRadius(50);
shape.setSize(width, height);
shape.setBounds(0, 0, width, height);
ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,ClipDrawable.HORIZONTAL);
shape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE, getResources().getColor(R.color.DimGray)});
shape.setCornerRadius(50);//change the corners of the rectangle
shape.setSize(width, height);
shape.setBounds(0, 0, width, height);
LayerDrawable mylayer= new LayerDrawable(new Drawable[]{shape, clip,});
seekbar.setProgressDrawable(mylayer);
seekbar.setProgress(50);
}
Here is my code to put everything together, where I set the height of the seek bar:
public MySeekBar(Activity activity, AbsoluteLayout ParentLayout, SeekBarParameters parameters)
{
super(activity.getApplicationContext());
seekbar =(SeekBar)activity.getLayoutInflater().inflate(R.layout.horizontal_seek_bar, ParentLayout,false);
seekbar.setMax(100);
setSeekBarThumb(parameters.widthThumb, parameters.heightThumb,0,100,0);
setSeekBarProgress(parameters.width, parameters.height,255,69,0);
int drawHeight;
if(parameters.height>parameters.heightThumb) drawHeight=parameters.height;
else drawHeight=parameters.heightThumb;
AbsoluteLayout.LayoutParams params = new AsoluteLayout.LayoutParams(parameters.width,drawHeight,parameters.xPosition, parameters.yPosition);
ParentLayout.addView(seekbar, params);
}
The XML code where I inflate the bar from:
<SeekBar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:max="100"
android:progress="0"
android:secondaryProgress="0"
android:maxHeight="10000dip"
android:id="#+id/slider"
/>
To change the size of the seek bar I use ParentLayout.addView(seekbar, params).
When adding the view, if I use the height that I want to the bar, the thumb gets cutted. If I use the height of the thumb, the bar gets to the same height of the thumb. Android resizes the bar to the size I use to add the view.
I tryed setting the size of the GradientDrawable that is the bar with shape.setSize(width, height), I also tried shape.setBounds(0, 0, width, height), and I tried creating another image with the right size and adding to the LayerDrawable mylayer. But nothing worked.
When creating this through XML it's possible to use paddingTop and paddingBottom to get the bar to the right size. But I don't know how to do this to the GradientDrawable programmatically.
well, too bad there were no responses but i was able to answer the question myself. The answer is to use InsetDrawable. You have to put the one of the drawables of the bar into a InsetDrawable.
Thats the code:
public void setSeekBarProgress(int width, int height, int rColor, int gColor, int bColor){
GradientDrawable shape2 = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE,Color.rgb(rColor, gColor, bColor)});
shape2.setCornerRadius(50);
ClipDrawable clip = new ClipDrawable(shape2, Gravity.LEFT,ClipDrawable.HORIZONTAL);
GradientDrawable shape1 = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE, getResources().getColor(R.color.DimGray)});
shape1.setCornerRadius(50);//change the corners of the rectangle
InsetDrawable d1= new InsetDrawable(shape1,5,5,5,5);//the padding u want to use
LayerDrawable mylayer = new LayerDrawable(new Drawable[]{d1,clip});
seekbar.setProgressDrawable(mylayer);
seekbar.setProgress(50);
}
I hope this can help someone else with the same problem.
I,m trying to paint circle in background of my textView. It should by a small object in up/left corner.
temporar = new TextView(cxt);
OvalShape oval = new OvalShape();
//oval.resize(10, 10);
qualitySignalDrawable = new ShapeDrawable(oval);
qualitySignalDrawable.getPaint().setColor(0xff74AC23);
qualitySignalDrawable.setBounds(2,2,10,10);
signalQualityText.setGravity(Gravity.CLIP_HORIZONTAL);
temporar.setBackgroundDrawable(qualitySignalDrawable);
temporar.setText("aaaaaaaaaaaaa");
I tried set difrents gravity and size and Bounds but id allways stretch shape to container size. I'm missing something?