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);
}
Related
I am setting the background of a TextViewin android as a pie diagram. I have used a LayerDrawable of ArcShape(s) to do so and set it as the background. Here is the code for that:
final ShapeDrawable arcGreen = new ShapeDrawable(new ArcShape(-90, correctAngle));
arcGreen.getPaint().setColor(ContextCompat.getColor(this, R.color.feedback_score_3));
final ShapeDrawable arcRed = new ShapeDrawable(new ArcShape(-90 + correctAngle, 360 - correctAngle));
arcRed.getPaint().setColor(ContextCompat.getColor(this, R.color.feedback_score_1));
final Drawable[] layer = {arcGreen, arcRed, mask};
final LayerDrawable ld = new LayerDrawable(layer);
mSvaraLayout.getChildAt(i).setBackground(ld);
Now, what I want to do is to animate this background when the value of correctAngle changes. One of the options that I see is to use ValueAnimator and go from the initial value to the new correctValue and each time inside the onAnimationUpdate() callback I create a new LayerDrawable object with the changed values and set it as a background. Other way could have been to get all the Drawables(s) in the background first(which are the ShapeDrawable(<ArcShape>)objects and then set the angles of the arcs. But I guess there is no way to do so. Is there any other way to achieve this?
I have to create SeekBar with primary and secondary progress at runtime. Looking at tileify function for how should I treat LayerDrawable, and progress_horizontal_holo_dark.xml for proper IDs, i came up with following:
// parent view
LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
// create widget
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
SeekBar seekBar = new SeekBar(this);
seekBar.setLayoutParams(params);
// set bg resources
Drawable background = getResources().getDrawable(R.drawable.scrubber_track_holo_light);
ScaleDrawable primary = new ScaleDrawable(getResources().getDrawable(R.drawable.scrubber_primary_holo), 0x10|0x03, 100.0f, 100);
ScaleDrawable secondary = new ScaleDrawable(getResources().getDrawable(R.drawable.scrubber_secondary_holo), 0x100x03, 100.0f, 85);
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { background, secondary, primary});
layerDrawable.setId(0, android.R.id.background);
layerDrawable.setId(1, android.R.id.secondaryProgress);
layerDrawable.setId(2, android.R.id.progress);
seekBar.setBackgroundDrawable(layerDrawable);
seekBar.setThumb(getResources().getDrawable(R.drawable.seekbar_btn));
// set values
seekBar.setMax(50);
seekBar.setSecondaryProgress(25);
seekBar.setProgress(15);
parent.addView(seekBar);
This is how SeekBar should look like:
And this are mine resource files:
- scrubber_primary_holo.9.png
- scrubber_secondary_holo.9.png
Problem:
setProgressDrawable(layerDrawable) doesn't work as expected. I use this function in XML code, but if I use it at runtime, I get resultult as shown at the picture below.
Any ideas?
In case someone else has the same problem, I found the solution. There were two issues. First one is that ScaleBitmap although used in XML doesn't work from code. I had to use ClipDrawable. The second issue was with showing primary and secondary progress - they were shown after the first progress change. This is solved by adding seekBar to parent view first and then setting primary and secondary progress. You can see the source code below. Cheers!
// CREATE WIDGET
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
SeekBar seekBar = new SeekBar(this, null, android.R.attr.seekBarStyle);
seekBar.setLayoutParams(params);
parent.addView(seekBar);
seekBar.setMax(50);
seekBar.setProgress(15);
seekBar.setSecondaryProgress(25);
// CREATE PROGRESS BAR
LayerDrawable layerDrawable = (LayerDrawable) seekBar.getProgressDrawable();
seekBar.setThumb(getResources().getDrawable(R.drawable.seekbar_btn));
Drawable progress = (Drawable) layerDrawable.findDrawableByLayerId(android.R.id.progress);
Bitmap progressBmp = BitmapFactory.decodeResource(getResources(), R.drawable.scrubber_secondary_holo);
progress = new ClipDrawable(new BitmapDrawable(getResources(), progressBmp), Gravity.AXIS_PULL_BEFORE, ClipDrawable.HORIZONTAL);
layerDrawable.setDrawableByLayerId(android.R.id.progress, progress);
Drawable secondary = (Drawable) layerDrawable.findDrawableByLayerId(android.R.id.secondaryProgress);
Bitmap secondaryBmp = BitmapFactory.decodeResource(getResources(), R.drawable.scrubber_primary_holo);
secondary = new ClipDrawable(new BitmapDrawable(getResources(), secondaryBmp), Gravity.AXIS_PULL_BEFORE, ClipDrawable.HORIZONTAL);
layerDrawable.setDrawableByLayerId(android.R.id.secondaryProgress, secondary);
Drawable background = (Drawable) layerDrawable.findDrawableByLayerId(android.R.id.background);
Bitmap backgroundBmp = BitmapFactory.decodeResource(getResources(), R.drawable.scrubber_track_holo_light);
background = new BitmapDrawable(getResources(), backgroundBmp);
layerDrawable.setDrawableByLayerId(android.R.id.background, background);
I need to create a simple Drawable object with needed color as background color, but I don't know how I can do it programmatically without using a XML schema (is it possible really?). Please, tell me, I need it to do for making a LayerDrawable and working with SeekBar (changing background of SeekBar programmatically). Thank you in advance.
You should try using a ColorDrawable. It can be constructed with a color using the constructor ColorDrawable(int color)
As it is suggested by #Thrakbad, you can use ColorDrawable:
TextView textView = new TextView(this);
ColorDrawable colorDrawable = new ColorDrawable(0xFFFF0000);
textView.setBackgroundDrawable(colorDrawable);
ColorDrawable will be helpful you in your case, you can pass parameter color for your drawable.
or you can do something like below:
ImageView imgStatus = (ImageView) findViewById(R.id.imgInfoIcon);
// Load the icon as drawable object
Drawable d = getResources().getDrawable(R.drawable.ic_menu_info_details);
// Get the color of the icon depending on system state
int iconColor = android.graphics.Color.BLACK
if (systemState == Status.ERROR)
iconColor = android.graphics.Color.RED
else if (systemState == Status.WARNING)
iconColor = android.graphics.Color.YELLOW
else if (systemState == Status.OK)
iconColor = android.graphics.Color.GREEN
// Set the correct new color
d.setColorFilter( iconColor, Mode.MULTIPLY );
// Load the updated drawable to the image viewer
imgStatus.setImageDrawable(d);
above code is originally posted here
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?
I'm trying to create a dynamic icon menu for my android application.
The icon is made from 2 drawable, a 9-patch background image with the icon image overlay on it.
With reference to overlay two images in android to set an imageview, I've the following code which have the overlay nicely.
Resources res = parent.getResources();
Drawable icon_bg = res.getDrawable(R.drawable.menu_icon_bg);
Drawable icon = res.getDrawable(R.drawable.menu_icon);
// Not working
int int_icon_height = icon.getIntrinsicHeight();
int int_icon_width = icon.getIntrinsicWidth();
Rect rect_icon_bg_bound = new Rect();
rect_icon_bg_bound.left = 0;//(int) Math.round(int_icon_width*-1.5);
rect_icon_bg_bound.right = (int) Math.round(int_icon_width*1.5);
rect_icon_bg_bound.top = 0;//(int)Math.round(int_icon_height*-1.5);
rect_icon_bg_bound.bottom = (int)Math.round(int_icon_height*1.5);
icon_bg.setBounds(rect_icon_bg_bound);
Drawable[] layers = new Drawable[2];
layers[0] = icon_bg;
layers[1] = icon;
LayerDrawable layerDrawable = new LayerDrawable(layers);
ImageView iv_icon_combined = new ImageView(getApplicationContext());
iv_icon_combined.setImageDrawable(layerDrawable);
The problem I'm facing right now is adding a padding so that the icon will have some spacing within the background.
Hope to get some enlightenment here, thanks in advance.
I just ran into this same issue. Take a look at the setLayerInset method on LayerDrawable. It takes the layer level as an argument and then the modifiers for the left, top, right, bottom bounds of the drawable.