jason
jason

Reputation: 7164

Drawing dashed line on Android programmatically

I want to draw horizontal dashed lines between programmatically generated TextViews. I tried this code :

Paint fgPaintSel = new Paint();
fgPaintSel.setARGB(255, 0, 0, 0);
fgPaintSel.setStyle(Paint.Style.STROKE);
fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));

But nothing happened. I just copied and pasted this code. What should I do to draw a dashed line? Thanks.

Upvotes: 3

Views: 7674

Answers (3)

Mikelis Kaneps
Mikelis Kaneps

Reputation: 4584

Create a dotted_line.xml file in drawable folder:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:left="-3px"
        android:right="-3px"
        android:top="-3px">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">

            <stroke
                android:width="2px"
                android:color="@color/dark_blue"
                android:dashGap="2px"
                android:dashWidth="3px" />
        </shape>
    </item>

</layer-list>

Add this drawable as background:

view.setBackground(getResources().getDrawable(R.drawable.dotted_line));

Result:

enter image description here

Upvotes: 8

user3956566
user3956566

Reputation:

Give the Activity Layout an id. I've used a button for this demonstration with an onclick handler PaintDashedLines().

In the content_main.xml layout.

<LinearLayout android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" .../>

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="PaintDashedLines"
        android:text="Press Me"/>
</LinearLayout>

Used a static int to count for purposes of demo, with a separate method to create the drawable, for modularisation.

In your activity:

static int tvCount = 0;

public void PaintDashedLines(View v) {
    LinearLayout ll = (LinearLayout) findViewById(R.id.main);
    TextView tv = new TextView(MainActivity.this);
    tv.setGravity(Gravity.CENTER);
    tv.setTextSize(25);
    tv.setPadding(0, 5, 0, 5);
    ll.addView(tv);
    tv.setText("TextView " + tvCount);
    ImageView divider = new ImageView(MainActivity.this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            ll.getWidth(), 2);
    lp.setMargins(0, 5, 0, 5);
    divider.setLayoutParams(lp);
    divider.setBackground(CreateDashedLined());
    ll.addView(divider);
    tvCount++;
}

public static Drawable CreateDashedLined() {
    ShapeDrawable sd = new ShapeDrawable(new RectShape());
    Paint fgPaintSel = sd.getPaint();
    fgPaintSel.setColor(Color.BLACK);
    fgPaintSel.setStyle(Paint.Style.STROKE);
    fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));
    return sd;
}

enter image description here

-MyActivity
--int count;
--oncreate
--PaintDashedLines(View v)
--public static Drawable CreateDashedLined()

In the build.gradle (though this is not set in stone)

    minSdkVersion 18
    targetSdkVersion 23

You do not need to do anything else.

Upvotes: 12

Ravi
Ravi

Reputation: 35549

create dashed_line.xml in drawable

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">

  <stroke
     android:color="#C7B299"
     android:dashWidth="10px"
     android:dashGap="10px"
     android:width="1dp"/>
</shape>

Programmatically add View

View v = new View(this);

and set its background

v.setBackgroundResource(R.drawable.dashed_line);

set height and width of your View according to your need.

Upvotes: 1

Related Questions