5

I wanted to implement Pull to Refresh feature in my android application, so I implemented this library: Android-PullToRefresh. However, I can't seem to set custom style to divide programmatically.

The code is simple:

list = (PullToRefreshListView) findViewById(R.id.list);
int[] colors = {0, 0xFF97CF4D, 0}; 
list.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
list.setDividerHeight(1);

However, it is throwing this error: The method setDivider(GradientDrawable) is undefined for the type PullToRefreshListView and The method setDividerHeight(int) is undefined for the type PullToRefreshListView.

What am I doing wrong here?

input
  • 7,503
  • 25
  • 93
  • 150

1 Answers1

8

PullToRefreshListView is not a ListView, hence that error. You should access the ListView inside PullToRefreshListView and invoke setDivider* methods on that.

list = (PullToRefreshListView) findViewById(R.id.list);
int[] colors = {0, 0xFF97CF4D, 0};
ListView inner = list.getRefreshableView();
inner.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
inner.setDividerHeight(1);

As an alternative you could define your gradient as an XML drawable and set the attributes right in your layout like shown in the sample here

eg:

<com.handmark.pulltorefresh.library.PullToRefreshListView
  android:divider="@drawable/fancy_gradient"
  android:dividerHeight="@dimen/divider_height"...
a.bertucci
  • 12,142
  • 2
  • 31
  • 32
  • It gives the error: `Type mismatch: cannot convert from ListView to PullToRefreshListView` – input Apr 20 '13 at 19:48
  • 1
    check the code above: you should invoke `getRefreshableView()` on your `PullToRefreshListView`. Casting to ListView is useless in this case. I'll get rid of that in my answer. – a.bertucci Apr 20 '13 at 19:55
  • The code didn't work. I ended up styling the divider with an XML drawable. Thanks for the help! :) – input Apr 20 '13 at 20:00
  • 2
    XML-based solution is working out-of-the-box. But I'm sure of the code-based solution as well. I suppose your `list` reference is a `PullToRefreshListView`, while in the modified snippet I assumed that as a `ListView` one. I'll slightly modify my answer in order to recover this issue. – a.bertucci Apr 20 '13 at 20:36