Drawing shapes in background TextView - android

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?

Related

How to dynamically set drawable resource to ImageView?

I use function drawCircle(int diameter) to return ShapeDrawable object, which is an OvalShape. Then I use ImageView.setImageDrawable() to set the OvalShape. But the ImageViews are still empty. How to do it correctly?
Function drawCircle():
public ShapeDrawable drawCircle (int diameter) {
OvalShape ovalShape = new OvalShape();
ShapeDrawable drawable = new ShapeDrawable(ovalShape);
drawable.getPaint().setColor(getResources().getColor(R.color.textColorHint));
drawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
drawable.getPaint().setStrokeWidth((float)2);
drawable.setBounds(0 ,0, diameter, diameter);
return drawable;
}
Set ImageView:
one_img.setImageDrawable(drawCircle(selector_one_diameter));
two_img.setImageDrawable(drawCircle(selector_two_diameter));
Above all, I tried using .xml file to describe oval shapes. But since values in dimens.xml cannot be re-written in Java code, I can't dynamically change the size of oval shapes. It couldn't be better if you can suggest another way!
It could be due to you have not set widht, height to your drawable. You can set a default size using below code.
drawable.setIntrinsicWidth();
drawable.setIntrinsicHeight();

Custom View with animation,color and border containing inner textview

Referring to below image, I want that view with yellow background and right border to slide in from right with some text. So this requirements include:
creating a custom view (which will need canvas I guess)
giving it width, height, position, color, border, border-color, etc.
animation to slide from right
textview within this custom view to display text
I referred this tutorial to understand how to include view with a canvas in layout. But instead of hard coded point positions, I tried following:
public MyView(Context context) {
super(context);
WindowManager wm = (WindowManager) DashBoardActivity.mContext
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
width -= 10;
height -= 50;
myPath = new Pt[6];
myPath[0] = new Pt(width, height);
myPath[1] = new Pt(width - 400, height);
myPath[2] = new Pt(width - 500, height - 100);
myPath[3] = new Pt(width - 400, height - 200);
myPath[4] = new Pt(width, height - 200);
myPath[5] = new Pt(width, height);
}
But view is not getting positioned as per the expectation. I tried with different hard-coded positions, but either it's getting displayed at wrong position or not getting displayed at all.
Also, to show the text, I added textview in the layout itself but it is getting displayed at top left of the screen instead of the position shown in below image.
How I can achieve this ? Any suggestions appreciated.
You dont need any custom view for this, you can set shape or 9patch image as background to the textview and animate that textview using object animator.
Border in shape xml

Android shape doesn't stretch as progressDrawable on seekbar

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.

Create Custom Seekbar Programmatically (No XML)

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);
}

Android: Setting padding in a overlay image background

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.

Categories

Resources