Reputation: 193
I am setting the background color for each row in my customadapter as
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.coupon_list_row, null);
int temp_index = ExtraFu.randInt(0, 9);
convertView.setBackgroundColor(color_arr[temp_index]);
Log.d("temp_index", String.valueOf(temp_index));
}
My array of color
int color_arr[]={R.color.cred,R.color.cpink,R.color.cpurple,R.color.cdpurple,R.color.cindigo,R.color.cblue,R.color.cdorange,R.color.cgreen,R.color.cbroun,R.color.ccyan};
this is the function randInt
public static int randInt(int min, int max) {
// Usually this can be a field rather than a method variable
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
the row background is set to two color. Is that the random number is generated same all the time?
Upvotes: 1
Views: 4907
Reputation: 114
Tips: Use modulo operator, to Color each row with 4 colors
export const moduloColor = (rowIndex: number): string => {
let rep
if (rowIndex % 4 === 0) { // if rowIndex is a multiple of 4
rep = '#00838F'
} else if (rowIndex % 3 === 0) { // if rowIndex is a multiple of 3
rep = '#00ACC1'
} else if (rowIndex % 2 === 0) { // if rowIndex is a multiple of 2
rep = '#26C6DA'
} else {
rep = '#80DEEA'
}
return rep
}
Upvotes: 0
Reputation: 357
if (position%4 == 0){
// set convertView Background
} else if (position%4 == 1){
// set convertView Background
} else if (position%4 == 2){
// set convertView Background
} else if (position%4 == 3){
// set convertView Background
}
This will randomly generate the four different color in your listview.
inside adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = lv.inflate(res, null);
holder = new ViewHolder();
holder.textView = (TextView)convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (position%4 == 0){
holder.textView.setBackgroundColor(Color.parseColor("#1e86cf"));
} else if (position%4 == 1){
holder.textView.setBackgroundColor(Color.parseColor("#2ca0ea"));
} else if (position%4 == 2){
holder.textView.setBackgroundColor(Color.parseColor("#2cc4ea"));
} else if (position%4 == 3){
holder.textView.setBackgroundColor(Color.parseColor("#2ceae3"));
}
return convertView;
}
ViewHolder Class:
class ViewHolder{
public TextView textView;
}
adapter custom layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
Upvotes: 4
Reputation: 3815
For Random Color Generation :
private int getRandomColor() {
SecureRandom rgen = new SecureRandom();
return Color.HSVToColor(150, new float[]{
rgen.nextInt(359), 1, 1
});
}
And set BackGroundColor
of each item after retrieving the item position:
holder.textView.setBackGroundColor(getRandomColor());
Upvotes: 3
Reputation: 714
Use Below code to get Random colors
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
convertView.setBackgroundColor(color);
Upvotes: 0
Reputation: 714
I have edited your code
you need to use below line i have checked its working finr for my layout
convertView.setBackgroundResource(color_arr[rnd]);
Your updated code not checked on my side
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.coupon_list_row, null);
int color_arr[]= {R.color.cred,R.color.cpink,R.color.cpurple,R.color.cdpurple,R.color.cindigo,R.color.cblue,R.color.cdorange,R.color.cgreen,R.color.cbroun,R.color.ccyan};
int rnd = new Random().nextInt(color_arr.length);
//convertView.setBackgroundColor(color_arr[temp_index]);
convertView.setBackgroundResource(color_arr[rnd]);
}
Reference Code is which i have tried and works fine so do the needful changes
int color_arr[] = {R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark};
int rnd = new Random().nextInt(color_arr.length);
linearLayout.setBackgroundResource(color_arr[rnd]);
Upvotes: 3