LinearLayout set border color dynamically using java code without XML
android, android-layout
Solution
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.
Problem
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; } ```