How to make a horizontal line in Android programmatically

I am trying to fill a linear layout programmatically like this

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ); params.setMargins(dpToPx(6), 0, 0, 0); kac.setLayoutParams(params); LinearLay.addView(kac); 

and I want to add (after each TextView (e.g. kac)) a horizontal line similar to the one I have in my xml

  <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="#B3B3B3" android:padding="10dp" /> 
+7
java android
source share
4 answers
 View v = new View(this); v.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, 5 )); v.setBackgroundColor(Color.parseColor("#B3B3B3")); LinearLay.addView(v); 
+26
source share

I recommend creating a drawing containing a string and setting it as the background for your text box. You would avoid unnecessary views.

The sample selected may be:

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="#LINE_COLOR"/> <padding android:bottom="1dp" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#BACKGROUND_COLOR"/> </shape> </item> </layer-list> 

And you can install it

 textView.setBackgroundResource(R.drawable.resource) 
+2
source share

edit * example:

 @Override protected void onDraw(Canvas canvas) { //this would draw the textview normally super.onDraw(canvas); //this would add the line that you wanted to add at the bottom of your view //but you would want to create your rect and paint outside of the draw call //after onmeasure/onlayout is called so you have proper dimensions canvas.drawRect(new Rect(0, this.getHeight()-1, this.getWidth(), this.getHeight()), mPaint); } 

If I understand that you just want to draw a line after each text view, it is best to simply extend the TextView with a special class, overriding the onDraw method, adding a draw call to draw the line yourself

+1
source share

you just need to set the style (background) in your TextViews, making Views unnecessary in this case

0
source share

All Articles