I created a Dynamic list of Todo tasks. I want to create a random color on the background of TextView and in circle.(Like in Gmail App)
Everything works fine but when i scroll the list, the color changes again and again randomly.
Also when the keyboard pops up the color of the circle changes.
I created this list item.
Below is the code of the TextView having letter A.
<TextView
android:text="A"
android:gravity="center"
android:textSize="30sp"
android:textColor="#000"
android:layout_marginRight="20dp"
android:id="@+id/iv"
android:textAllCaps="true"
android:background="@drawable/circle"
android:layout_width="50dp"
android:layout_height="50dp"
/>
And here the @drawable/circle Code. So every time a new item is added the color of circle changes
<shape
xmlns:android="http://ift.tt/nIICcg"
android:shape="oval" >
<size
android:width="50dp"
android:height="50dp"/>
</shape>
My getView code
public View getView(int position,@Nullable View convertView, @NonNull ViewGroup parent) {
View view = inflater.inflate(layoutResource, null);
Drawable circle = ContextCompat.getDrawable(context,R.drawable.circle); //get the drawable circle
TextView name = (TextView) view.findViewById(R.id.name);
TextView date = (TextView) view.findViewById(R.id.dateTextView);
TextView time = (TextView) view.findViewById(R.id.timeTextView);
TextView letter = (TextView) view.findViewById(R.id.iv);
final items items = todoList.get(position);
circle.setTint(items.getColor());
name.setText(items.getName());
date.setText(items.getDate());
time.setText(items.getTime());
letter.setText(items.getLetter());
return view;
}
And this code is in MainActivity when I click the + button
Random rand = new Random();
int max = 255,min=0;
int r = rand.nextInt(max - min +1) - min;
int g = rand.nextInt(max - min +1) - min;
int b = rand.nextInt(max - min +1) - min;
int randomColor = Color.rgb(r,g,b);
item1.setColor(randomColor);
item1.setName(todo);
item1.setLetter(todo.charAt(0)+"");
item1.setDate(date);
item1.setTime(time);
Aucun commentaire:
Enregistrer un commentaire