samedi 21 mars 2015

After I randomised colors from XML how to get color's name/tag back?

I have the following code to randomise color from a custom color xml:



final int[] colorArray = getResources().getIntArray(R.array.color_names);
int randomColorName = colorArray[rand.nextInt(colorArray.length)];

final LinearLayout lL4 = (LinearLayout) findViewById(R.id.lL4);
lL4.setBackgroundColor(randomColorName);
//...


I have more items that I randomise after this.


My question how can I for example Toast out that if the user click on the layout or a button that which color is that? Because this code doesnt save the generated colors for the objects.


i tried settings tags on the layout:


lL4.setTag(randomColorName);


Then tried to Toast in onClick the color of the object:



lL4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, lL4.getTag().toString(), Toast.LENGTH_SHORT).show();
}
});


This didnt work because I store them in int array and it gave back a number onClick, I tried to store them in string array but that didnt work at all, couldnt even set the objects to random color.


How could I do this?


Aucun commentaire:

Enregistrer un commentaire