Skip to content Skip to sidebar Skip to footer

How To Create Scratch Card Which Allows User To Scratch Only Once

This code is working, I created Scratch image view through which I can scratch the image view to see the the image, but scratch image view is automatically filling with scratch pat

Solution 1:

What you can do is store into Firebase a variable like isScratched = 1 or 0, if it's 1 it's because the user didn't scratch it yet. If the user scratch it that variable will be 0 and then in onStart you put a listener of Firebase database, if the listener finds out the value is 0 the scratch card will not be available.

I will show you some snippet here

private DatabaseReference mDatabase; //First declare your database reference

Then in init() or onCreate()

mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("isScratched").setValue("1"); //here we create the isScratched and set it to 1 , meaning that the photo is not even scratched yet

Now, after the photo is scratched just set that value to 0

//After your scratch method or when the user finishes scratching the pick
mDatabase.child("isScratched").setValue("0");

Now in your onStart() or in the Activity where the image appears just attach a listener

mDatabase.child("isScratched").addSingleValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

            String getScratchedValue = datasnapshot.getValue(String.class);
            Log.e("IsScratched : " ,""+getScratchedValue);
            if(getScratchedValue.equals(0)){

                 //Your picture is already scratched, run the method that will show it scratched

                 }

             }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        System.out.println("Database Error: "+databaseError.getDetails());
    }
});

Post a Comment for "How To Create Scratch Card Which Allows User To Scratch Only Once"