Dr.Noname
Dr.Noname

Reputation: 73

Programmatically draw line under TextView

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>

Upvotes: 1

Views: 1415

Answers (3)

user3705007
user3705007

Reputation: 295

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).

Upvotes: 0

Innocent
Innocent

Reputation: 733

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);

Upvotes: 1

Aubtin Samai
Aubtin Samai

Reputation: 1361

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));

Upvotes: 0

Related Questions