Reputation: 921
I have done some research on that issue , but I have not found anything similar just yet.
First I make a border of Linear Layout using ShapeDrawable then I have try to set background color on LinearLayout but color is not set then I have comment two line of ShapeDrawable then after I have set the background color of Linear Layout but problem occur in border color.
That's value I have got through JSON of background color and border color.
I want to set that value dynamically Background color and border color of Linear Layout through java code.
Please Guide me.
Thanks
LinearLayout linearToAdd = new LinearLayout(getActivity());
linearToAdd.setOrientation(VERTICAL);
float d = getActivity().getResources().getDisplayMetrics().density;
linearToAdd.setBackgroundColor(Color.parseColor((String)(mPod.getBackground())));
switch(parentType){
case LINEAR_LAYOUT:
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams((int)(mPod.getWidth()*d),(int)(mPod.getHeight()*d));
Log.d(TAG,"LinLay, W,H,T,L: "+mPod.getWidth()+", "+mPod.getHeight()+", "+mPod.getLeft()+", "+mPod.getTop());
linearParams.setMargins(mPod.getLeft(), mPod.getTop(), 0,0);
linearToAdd.setLayoutParams(linearParams);
ShapeDrawable rectShapeDrawable1 = new ShapeDrawable();
Paint paint1 = rectShapeDrawable1.getPaint();
// paint1.setColor(Color.rgb(0, 0, 0));
paint1.setStyle(Paint.Style.STROKE);
paint1.setStrokeWidth(3);
// linearToAdd.setBackgroundDrawable(rectShapeDrawable1);
break;
}
Upvotes: 2
Views: 4466
Reputation: 2146
You can try doing it by GradientDrawable. I tried this one. Hope it might help you.
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(3, Color.BLACK);
drawable.setCornerRadius(8);
drawable.setColor(Color.BLUE);
linearToAdd.setBackgroundDrawable(drawable);
In your XML layout give android:padding="1dp" to linearToAdd.
Upvotes: 10
Reputation: 93
overide on windows focuschanged listener like below
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams((int)(mPod.getWidth()*d),(int)(mPod.getHeight()*d));
Log.d(TAG,"LinLay, W,H,T,L: "+mPod.getWidth()+", "+mPod.getHeight()+", "+mPod.getLeft()+", "+mPod.getTop());
linearParams.setMargins(mPod.getLeft(), mPod.getTop(), 0,0);
linearToAdd.setLayoutParams(linearParams);
Bitmap bitMap = Bitmap.createBitmap(linearParams.width, linearParams.height, Bitmap.Config.ARGB_8888);
Canvas canvas=new Canvas(bitMap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.parseColor((String)(mPod.getBackground())));
Paint borderPaint = new Paint();
borderPaint.setAntiAlias(true);
borderPaint.setColor(Color.parseColor((String)(mPod.getBorder())));
canvas.drawRect(linearToAdd.getLeft(), linearToAdd.getTop(), linearToAdd.getRight(), linearToAdd.getBottom(), borderPaint);
canvas.drawRect(linearToAdd.getLeft()+2, linearToAdd.getTop()+2, linearToAdd.getRight()-2, linearToAdd.getBottom()-2, paint);
Drawable linearToAdd_bg=new BitmapDrawable(bitMap);
linearToAdd.setBackground(linearToAdd_bg);
}
Upvotes: 0