Christos Baziotis
Christos Baziotis

Reputation: 6035

Programmatically add border to LinearLayout

How do I add programmatically a border to a LinearLayout? Lets say we create this layout:

LinearLayout TitleLayout = new LinearLayout(getApplicationContext());
TitleLayout.setOrientation(LinearLayout.HORIZONTAL);

Then what do I do?

Upvotes: 17

Views: 34164

Answers (4)

Matheus Faleiro
Matheus Faleiro

Reputation: 101

You can create a gradient drawable with a stroke. You can modify the stroke width and color programmatically.

private fun createGradientBackground(@ColorInt startColor: Int, @ColorInt endColor: Int) =
    GradientDrawable(
        GradientDrawable.Orientation.LEFT_RIGHT, intArrayOf(
            startColor,
            endColor
        )
    ).also {
        it.shape = GradientDrawable.RECTANGLE
        it.cornerRadius = GRADIENT_CORNER_RADIUS
        it.setStroke(YOUR_WIDTH, YOUR_COLOR)
    }

Upvotes: 1

Arti
Arti

Reputation: 3071

For Xamarin users:

Add new class border:

public class Border : Android.Graphics.Drawables.Drawable
{
    public Android.Graphics.Paint paint;
    public Android.Graphics.Rect bounds_rect;

    public Border(Android.Graphics.Color colour, int width)
    {
        this.paint = new Android.Graphics.Paint();
        this.paint.Color = colour;
        this.paint.StrokeWidth = width;
        this.paint.SetStyle(Android.Graphics.Paint.Style.Stroke);
    }

    public override int Opacity => 0;
    protected override void OnBoundsChange(Rect bounds)
    {
        base.OnBoundsChange(bounds);
        this.bounds_rect = bounds;
    }

    public override void Draw(Canvas canvas)
    {
        canvas.DrawRect(this.bounds_rect, this.paint);
    }

    public override void SetAlpha(int alpha)
    {
        //throw new NotImplementedException();
    }

    public override void SetColorFilter(ColorFilter colorFilter)
    {
        //throw new NotImplementedException();
    }
}

And use it like this:

TitleLayout.SetBackgroundDrawable(new Border(Color.Black, 5));

Upvotes: 1

Sjoerd Van Den Berg
Sjoerd Van Den Berg

Reputation: 763

I believe the answer above isn't correct: The question asks specifically for a programmatic version to do it and the first thing you see is xml. Secondly, doing partly xml is in my case almost never an option, so here's the correct answer:

    //use a GradientDrawable with only one color set, to make it a solid color
    GradientDrawable border = new GradientDrawable();
    border.setColor(0xFFFFFFFF); //white background
    border.setStroke(1, 0xFF000000); //black border with full opacity
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
      TitleLayout.setBackgroundDrawable(border);
    } else {
      TitleLayout.setBackground(border);
    }

Upvotes: 73

MDMalik
MDMalik

Reputation: 4001

Creat XML called border.xml in drawable folder as below :

 <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item> 
    <shape android:shape="rectangle">
      <solid android:color="#FF0000" /> 
    </shape>
  </item>   
    <item android:left="5dp" android:right="5dp"  android:top="5dp" >  
     <shape android:shape="rectangle"> 
      <solid android:color="#000000" />
    </shape>
   </item>    
 </layer-list> 

then add this to linear layout as backgound as this:

android:background="@drawable/border"

Programmatically

TitleLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.border))

EDIT :

Since Jelly Bean, this method (setBackgroundDrawable has been deprecated), so yet you have to use this one :

TitleLayout.setBackground(getResources().getDrawable(R.drawable.border));

hope this help .

Upvotes: 5

Related Questions