34

in my app I use XML defined vertical dotted line.

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="90"
    android:toDegrees="90">
<shape

    android:shape="line">
    <stroke

        android:width="1dp"
        android:color="@color/light_gray"
        android:dashWidth="2dp"
        android:dashGap="4dp"
        />
</shape>

When I am nitpicking my layout in Android Studio, the line is rendered properly dotted, as it should, but problem comes out when I run the app on real device. The line is just solid with no gaps. What could be the problem? I tried many different devices including those running latest 4.3 Android. It look everywhere the same.

Braiam
  • 1
  • 11
  • 47
  • 78
simekadam
  • 7,334
  • 11
  • 56
  • 79
  • No needed to controlling hardware acceleration [This will solved issue of dotted line](http://stackoverflow.com/questions/24536617/android-l-is-not-able-to-draw-dotted-line-as-drawable-background) – Anant Shah Jan 22 '16 at 07:52

2 Answers2

76

This is most likely related to hardware acceleration: Dashed lines are not supported in GL mode.

Its documented here: https://code.google.com/p/android/issues/detail?id=29944

Turn off your HW-acceleration in your AndroidManifest.xml like this:

android:hardwareAccelerated="false"

or:

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null) 

For more information how to use first solution: http://developer.android.com/guide/topics/graphics/hardware-accel.html

cyroxis
  • 3,661
  • 22
  • 37
stoefln
  • 14,498
  • 18
  • 79
  • 138
  • 1
    Upvoting. In my case it was `android:layerType="software"` – ror Apr 02 '19 at 15:26
  • See https://stackoverflow.com/questions/10843402/android-dashed-line-drawable-potential-ics-bug. Don't use `android:hardwareAccelerated="false"`, use `android:layerType="software"` in a view. – CoolMind Nov 28 '19 at 10:01
  • perfect answer. working 100%. Great job brother. Keep up your posting for question like this – Pir Fahim Shah Mar 17 '20 at 10:13
16

I found a same solution but in View attributes (xml):

        <View
        android:layout_width="wrap_content"
        android:layout_height="4dp"
        ...
        android:layerType="software"/>

I suppose it's a View level as said above. Other variants for attribute ("none" and of course "hardware") didn't work for me.

Dmitry Lvov
  • 347
  • 3
  • 9