how to create random UUID in Android when button click event happens? how to create random UUID in Android when button click event happens? android android

how to create random UUID in Android when button click event happens?


First time it intialise the variable and next time when you click button it doesn't get null value

Remove if condition from this

if(uniqueId == null) { uniqueId = UUID.randomUUID().toString(); }

Use this

uniqueId = UUID.randomUUID().toString(); 


Your null check for uniqueId causes the problem.

when you click the button for the first time uniqueId is null and a new UUID is generated. But when you click it next time uniqueId is not null, So no new UUID is generated.


You are explicitly avoiding the new UUID creation by:

if(uniqueId == null) {   uniqueId = UUID.randomUUID().toString();}

Remove the check.