1

I have a layout that adds TextView's in loop dynamically:

TextView msg = new TextView(this);
TextView Bmsg = new TextView(this);
msg.setText(splittedItem[0]);
msg.setTextColor(0xFF2C85A6);
msg.setTextSize(22);
msg.setPadding(10, 10, 0, 10);
Bmsg.setText("- "+splittedItem[1]);
Bmsg.setPadding(20, 0, 10, 30);
Bmsg.setTextSize(18);
linearLayout.addView(msg);
linearLayout.addView(Bmsg);

And I want to divide each TextView with a line like this:

I need a line under Bmsg. I have found this - draw line under TextView on Android, but I do not understand how to make this programmatically.

in XML:

 <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/buttonlayout">
 </LinearLayout>
Community
  • 1
  • 1
Dr.Noname
  • 73
  • 10

3 Answers3

1

use this and let me know if it works

View view = new View(this);
view.setLayoutParams(new LinearLayout.LayoutParams(
    LayoutParams.MATCH_PARENT,2
));
view.setBackgroundColor(Color.parseColor("#000000"));

linearlayout.addView(view);
Innocent
  • 703
  • 10
  • 21
0

You can add this to your styles list:

   <style name="DividerHorizontal">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">1dp</item>
        <item name="android:background">?android:attr/listDivider</item>
    </style>

    <style name="DividerVertical">
        <item name="android:layout_width">1dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:background">?android:attr/listDivider</item>
    </style>

Once you do that, you can call it with a View like this:

   <View style="@style/DividerHorizontal"
        android:layout_alignParentTop="true"
        android:id="@+id/topDivider" />

EDIT:

Since you seem to want to make a list, just use a ListView and do something similar or use this in the code:

    mListView = getListView();
    mAdapter = new ServersListAdapter(this, new String[] {},
            new String[] {}, new String[] {});
    int[] colors = { Color.parseColor("#D3D3D3"), Color.parseColor("#D3D3D3"), Color.parseColor("#D3D3D3") };
    mListView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
Aubtin Samai
  • 1,281
  • 13
  • 24
0

I suggest using a RecyclerView to display your TextViews, this could give you better flexibility and depending on what you do, better performance too.

You can use RecyclerView.ItemDecoration to make dividers (and other things).

user3705007
  • 265
  • 4
  • 12