I have to change the divider color of my listView dynamically.
solution:
Therefore I to create a Drawable programmatically that draws a horizontal line and set it as background of my convertView. Now the line is in the middle of my convertView. How can I align it to the bottom?public class DividerView extends Drawable { private Paint mPaint; public DividerView() { mPaint = new Paint(); mPaint.setStrokeWidth(3); } @Override public void draw(Canvas canvas) { int lvl = getLevel(); Rect b = getBounds(); float x = b.width() * lvl / 10000.0f; float y = (b.height() - mPaint.getStrokeWidth()) / 2; mPaint.setColor(Color.BLACK); canvas.drawLine(x, y, b.width(), y, mPaint); } @Override protected boolean onLevelChange(int level) { invalidateSelf(); return true; } @Override public void setAlpha(int alpha) { } @Override public void setColorFilter(ColorFilter cf) { } @Override public int getOpacity() { return PixelFormat.TRANSLUCENT; } }
solution:
I also tried to use a shape drawable as divider and change its color. But I get the following exception:LayerDrawable cannot be cast to android.graphics.drawable.GradientDrawable
How can I access the stroke in my layer-list?
Here is my code:
divider.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item android:top="-6dp" android:right="-6dp" android:left="-6dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="#000000" />
</shape>
</item>
ListAdapter.java
GradientDrawable drawable = (GradientDrawable)convertView.getBackground();
drawable.setStroke(3, Color.RED);
convertView.setBackground(drawable);