Search code examples
androidlinetablerowstroke

draw line on tablerow programmatically


I want to draw a horizontal line in middle of a tablerow like this

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
<solid android:color="#ffffff" />    
<stroke android:width="2dip" 
    android:color="#FF0000" android:dashWidth="2dp"/>
</shape>

but with java code


Solution

  • Since you didn't added the java code for your table row, I'll do assumptions:

    Assume the code you have is in the xml file named line.xml inside Drawable folder, Assume we have TableRow myRow = new TableRow(this);

    Now we will convert line.xml to a Drawable in Java code,

    Resources res = getResources();
    Drawable line = res.getDrawable(R.drawable.line);
    

    Now you just have to add it inside a view to put it in middle of your table row, for example:

    TextView tv = new TextView(this);
    tv.setBackground(line);
    

    Then add it inside your table row,

    myRow.addView(tv);
    

    I didn't try it but you can give it a shot, Good luck