I'm trying to display a ListView
with coloured background item. Every row should have different gradient background. I've searched for some time but couldn't fix my problem. Every row has same background now - last saved profile. Moreover, I failed to set gradient as background of TextView
that uses rounded.xml
as background. Thanks for any help.
Here's my CustomAdapter
:
public class CustomAdapterProfiles extends ArrayAdapter<Profile> {
private static final String TAG = "MyActivity";
ArrayList<Profile> myArrayList = null;
PaintDrawable paint;
int[] arrColors;
int numColors;
float[] result;
Profile i;
CustomAdapterProfiles(Context context, ArrayList<Profile> menuAdapter){
super(context, R.layout.customrow , menuAdapter);
this.myArrayList = menuAdapter;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater listInflater = LayoutInflater.from(getContext());
View customView = listInflater.inflate(R.layout.customrow, parent, false);
i = myArrayList.get(position);
String singleItem = i.getObjectName();
TextView mobileText = (TextView) customView.findViewById(R.id.listID);
mobileText.setText(singleItem);
numColors = i.getArrayList().size();
arrColors = new int[i.getArrayList().size()];
if (numColors>1) {
//positions of colors defined by user
result = new float[numColors];
for (int a = 0; a < numColors; a++) {
result[a] = (float) i.getGradients().get(a);
}
//make sure user didnt write error values (not fixed yet)
result[0]=0;
result[numColors - 1] = 1;
//colors
for (int j = 0; j < numColors; j++) {
arrColors[j] = Integer.parseInt(i.getArrayList().get(j).toString(), 16) + 0xFF000000;
}
ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
LinearGradient linearGradient = new LinearGradient(0, 0, width, height,
arrColors, //pouzity array farieb
result,
Shader.TileMode.REPEAT);
return linearGradient;
}
};
paint = new PaintDrawable();
paint.setShape(new RectShape());
paint.setShaderFactory(shaderFactory);
mobileText.setBackgroundDrawable((Drawable) paint);
}
else {
//cant set shaderFactory becouse it needs 2 or more colors
mobileText.getBackground().setColorFilter(Color.parseColor("#" + i.getArrayList().get(0).toString()), PorterDuff.Mode.SRC_ATOP);
}
return customView;
}
}