user1635224
user1635224

Reputation: 1673

border for textview via coding

I have a TextView created via java coding and I need to draw the border via coding. Please refer my code below,

TextView txt = new TextView(this);
txt.setText("SEARCH TABLE");
txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
txt.setFadingEdgeLength(10);
txt.setTextColor(Color.WHITE);
txt.setBackgroundColor(Color.rgb(83,82,82));
txt.setPadding(10, 5, 10, 0);
txt.setGravity(Gravity.CENTER);
txt.setTypeface(Typeface.SERIF,Typeface.BOLD);

How to do it ?

Upvotes: 2

Views: 6010

Answers (1)

Adam
Adam

Reputation: 418

You can set a shape drawable (a rectangle) as background for the view.

txt.setBackgroundDrawable(getResources().getDrawable(R.drawable.black));

And rectangle drawable back.xml (put into res/drawable folder):

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >    
<solid android:color="#ffffff" />    
<stroke android:width="1dip" android:color="#4fa5d5"/>    
</shape>

Upvotes: 5

Related Questions