What I am trying to do is add a border around the TextView, while the middle of the text view should be transparent. Here is my code:
LayerDrawable borders = getBorders(
Color.TRANSPARENT, // Background color
Color.parseColor("#CCCCCC"), // Border color
2, // Left border in pixels
2, // Top border in pixels
2, // Right border in pixels
2 // Bottom border in pixels
);
TextView cell = (TextView) super.getView(position, convertView, parent);
cell.setBackground(borders);
// Custom method to generate one or multi side border for a view
protected LayerDrawable getBorders(int bgColor, int borderColor,
int left, int top, int right, int bottom){
// Initialize new color drawables
ColorDrawable borderColorDrawable = new ColorDrawable(borderColor);
ColorDrawable backgroundColorDrawable = new ColorDrawable(bgColor);
// Initialize a new array of drawable objects
Drawable[] drawables = new Drawable[]{
borderColorDrawable,
backgroundColorDrawable
};
// Initialize a new layer drawable instance from drawables array
LayerDrawable layerDrawable = new LayerDrawable(drawables);
// Set padding for background color layer
layerDrawable.setLayerInset(
1, // Index of the drawable to adjust [background color layer]
left, // Number of pixels to add to the left bound [left border]
top, // Number of pixels to add to the top bound [top border]
right, // Number of pixels to add to the right bound [right border]
bottom // Number of pixels to add to the bottom bound [bottom border]
);
// Finally, return the one or more sided bordered background drawable
return layerDrawable;
However, the code above produced the text view with light grey color. The middle of the text view is not transparent at all. How do I fix this?
Create drawable file
abc.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#00ffffff" />
<corners android:radius="2dp" />
<stroke android:width="1dp" android:color="#e1e1e1" />
</shape>
</item>
</selector>
and set background
cell.setBackground(R.drawable.abc);
and you can change background color to change <solid android:color="#00ffffff" />
color in xml file