Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding border to TextView programmatically

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?

like image 344
UserError404 Avatar asked Jun 07 '26 08:06

UserError404


2 Answers

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

like image 93
Gautam Surani Avatar answered Jun 08 '26 23:06

Gautam Surani


You can try this. It will help you to add border on textview programatically

GradientDrawable gradientDrawableDefault = new GradientDrawable();
gradientDrawableDefault.setStroke((int) context.getResources().getDimension(R.dimen.dp1), ContextCompat.getColor(context,R.color.color_player_line));
gradientDrawableDefault.setCornerRadius(
context.getResources().getDimension(R.dimen.dp5));
gradientDrawableDefault.setColor(Color.TRANSPARENT);

set gradientDrawableDefault in background of textview.

like image 40
Arti Patel Avatar answered Jun 08 '26 22:06

Arti Patel