How can I keep track of a "like" system on android? - java

I am currently making an android app for a friend who is a photographer where he will display his images in a RecyclerView. We would like to have sort of like a "like" feature where they can like his pictures. My question is, how would I keep track of the "likes"? What I mean is, how can I make it so that if someone already liked it, they cannot exit the app, come back and like it again? I thought about adding a +1 every time there was a like to the image properties in a database, but how can I keep them from exiting the app, coming back, and liking it again? TIA!

your problem is just a matter of identity. To achieve your goal, you should have a way to identify every piece of the pictures and each of the person who will give up-votes to these pictures.
If you can identify different pictures and different users, you can achieve your goal by either of the following ways:
For each of the pictures store all the users that have liked it. And every time a user want to like a picture, you should check whether he is in this collection or not. Only add the identity of the user to this collection if he is not in this set which means he did not like this picture before
Or you can store all the pictures that are liked by the user.
Which way is better depends on your other use cases.
To identify your pictures you can just assign a distinct name for each of the picture.
So you can see the key problem it how to identify your users.
Since you do not provide a detailed use case of the like action, I will describe some general ideas.
If you have a server
If the user must login, you can identify your users by their login id
If your user do not need to login, you can use some other information to identify your user for example their device id, MAC address or other device-specific indentity.
If you do not have a server, then how can you identify all the users that use the same device? The only way I can come up with is that, you must implement a local user system and ask the user to log in before they want to like a photo.

If you have so signing in users my guess is that you can't. You could save on a hidden file on the device the ids of the photos the device "liked" but this can be easily deleted if the user deletes the apps cache files or uninstall/reinstall.
Not an android expert here, just mho

With login:
In your Database there are "User", "Image" and "User_like_image"
You just have to check if this user didnt already upvoted the image
without login:
You locally store the ID of every images he upvoted

A rough idea: with each item that can be liked store a list of ids that liked it on the server. Your like count is equals to the list length.
On each device generate a random device id token (UUID.randomUUID() is always a good candidate), persist it locally on the device (e.g. in SharedPreferences - wiping the app data will kill it but that can't be solved reliably) and send it to the server with each request.
The server can now
tell you whether you already liked an image which is nice to see on the device and
reject multiple likes from the same device
A simpler approach: store locally in e.g. SharedPreferences that you liked the item. Then behave nicely and don't send another like request to your server backend when you see that you already did. Can be abused because it relies no the client software to be nice but I doubt a small photographer app needs to care too much about that.
A more elaborate approach: require user accounts. That works accross devices and persists through app uninstalls. People that go far enough to create multiple accounts to vote multiple times can't be stopped anyways. The big downside is that people will most likely not want to have another account just to like some photos. Would not recommend if avoidable.

What I did to avoid having people like the same item twice was:
Have 2 states with the same button. If you clicked on "like" the button state changes to "Remove Like" Save the state of the button in shared prefs and check the state before a click is given.
Example:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String ButtonText = button.getText().toString();
if(ButtonText.equals("Like"){
//code for Like
button.setText("Remove Like");
}
else{
//code for remove Like
button.setText("Like");
}
}
});

Related

So am building an app and it has this feature that i dont know exactly how to go about it

I hope this makes a bit sense, basically, I have this feature in my app for tracking calories which consists of having this page that only appears the first time you use the feature and it asks you to add personal details (so it can make the right calculations), after that you get faced with a simple page that tracks your nutrition with a button for the user to insert the meals he has eaten, this page has to save the inserted data (via firebase) and then restart from 0 each and every day.
my first problem is I don't know how I make the page that only appears one time to save personal data(to be more precise I don't know how to make only appears the first time). and the second problem is how do I make the app automatically sends the given data at the end of each day?
interface in normal state, interface when adding the meals
hopefully, this 2 images will help you get a better grasp of what am trying to explain
don't worry am not looking for someone to straight up solve it all for me, I just need some orientation about what type of things/functions I need to do to solve these 2 problems
While #Narendra_Nath's answer might work, please note that is not a bulletproof solution. Why? Because a SharedPreferences doesn't persist across app uninstalls. This means that your user can install and uninstall the app and see the page as much as they want. So if you indeed want a user to see a screen only once, then you should consider storing that data in a database. Please note that SQLite isn't also a solution because when a user uninstalls the app, everything that is stored locally is wiped out. So what's the solution?
The best way to solve this would be to store the data in the cloud, either in Cloud Firestore or in the Realtime Database. So you can set a boolean variable and always check against it.
If you however intend to implement Firebase Authentication, then another solution would be to display the screen when your users are authenticated for the first time. So even if they will try to sign in on another device, install and uninstall the app, they won't be able to see the screen again.
Regarding the second problem, you should consider using Cloud Function for Firebase. It's the most elegant solution. If you want to somehow schedule an operation, then you should consider using Cloud Scheduler, as explained in my answer in the following post:
Is it not possible to have a code in the background who will be called every 24h?
Make the page that only appears one-time -> store a value in the shared preferences "isInfoShownToUser -> false" then do a check when the app starts to check if this value is false or true. If it is false show the "take the info" page .. then turn the value to false in the shared preferences.
How do I make the app automatically send data -> Use a Workmanager implementation to send data to the server (Firebase) at a particular time ..
Or use a implementation like the first one which uploads the data to the server just once everyday

Is SharedPreferences suitable, or is there a better alternative?

I am trying to become familiar with android application development. I am trying to make a simple app to learn the basic skills, but I am not sure how to handle this part of the app. Therefore I need advice about how to go further.
I have a simple app with a button and a textview with 0 on it: the score. When the button is clicked, score goes up by 1 (by simple java code). When I close the app and restart, the score is 0 again.
What I want: When closing the app, the score must be saved, so that the same score appears on restart. For example, like that egg that you have to click on a million times. If several users had the app, everyone should have their own score (of course). The number should therefore really be linked to phone/account.
I already read about sharedpreferences. It sounds like what I'm looking for: Storing variables. However, I don't know if this is suitable for what I want to use it for. The score should never be lost for that specific phone/account. It may not be possible for the user to adjust the score himself (except by clicking on the button).
My question: Is sharedpreferences suitable for what I want to use it for, or is there a better alternative?
SharedPreferences is used to store small pieces of data locally on your device. So, if the user Uninstall the app or Clears App data, the the data you store in SharedPreferences will be cleared.
If you are okay about data being lost on Uninstall/ Clear Data, SharedPreferences is surely the best way to store such small piece of information as using an Online database is quite an overkill for such small piece of data.
The score should never be lost for that specific phone/account.
But according to this, it seems like don't want to lose the data even if the user Uninstall/Clear Data, clearly you have to use a Cloud Database like Firebase Real-time database/ Firestore.
If you want users to Login/Sign Up, you have to use Firebase Auth otherwise, you can simply store data as PhoneNumber : Score key-value pairs on Firestore.
Hope it helps

get and display user location postcode in edittext on location button click like in just eat app

I am trying to do something like the picture below of just eat app using android studio. I am yet to start building my app but I would like to know how to implement getting user postcode and showing it in the edittext field. Any help will do.
![img1] https://drive.google.com/file/d/186D7fjSvJOhEQdep_fU4dnkUGV-Yi8Ya/view?usp=drivesdk
If you are looking to allow users to identify their post code from their address you might find the Post Office Adress File (PAF) useful although this is a paid for service.
https://en.wikipedia.org/wiki/Postcode_Address_File
https://www.royalmail.com/business/services/marketing/data-optimisation/paf
Some developers may be able to get free sample data for developing their application
https://www.poweredbypaf.com/access-to-free-paf-sample-data/
If you want to allow users to graphically find their postcode e.g. by clicking on a map you would need a map which returns a National Grid Reference and then use Codepoint data (https://digimap.edina.ac.uk/webhelp/os/data_information/os_products/supporting/codepoint_guidance.htm) which is also (an expensive) paid for service to find the nearest postcode co-ordinate using some data structure e.g. quadtree. This will not work correctly in all cases as post code boundaries are not always convex.
Hope that helps

How to specify that user has shared my app x number of times or not?

I am working on android app that asking users to share this app to ten users to get a bonus. App can be shared via any app appearing in dialog. Now the problem is, I cannot determine that user has successfully shared my app to x number of users.
The simple code that I am using to share app is following.
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "playstore link");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, getResources().getString(R.string.share_app_text));
startActivity(Intent.createChooser(sharingIntent, "Share app via"));
I think the best you could get is: find a way to understand whether that "sharing" was actually succesfull (which could basically mean: in the end, the user clicked some OK button).
Then, persist that information.
In other words: you basically need a counter that gets stored, for example using Android's shared preferences (see here for a simple example).
The real problem might be that the "sharing" could be tricked easily. You know, like simply sharing to the same person on Facebook 10 times.
So, if you want something that is less easy to trick, you probably should count something else, like: you create a specific URL on a server of yours, and you count how many times that is visited (from different IPs for example).
Use UTM source.
1) Embade a UTM key unique to a particular user to the redirection URL.
2) when the user clicks on the share button, open that URL.
3) Host an HTML page at the server which opens from the aforesaid URL which will read that UTM code, create a mapping for that install and will redirect the user to the play-store URL.
If you actually want to track if users installed an app after they received a shared link, you should give Firebase Invites a try. This is probably the most elegant way if you don't want to develop this functionality on your own.

How/Where to implement onItemClick handlers with several fragments

I am currently a student taking a Mobile Device Applications development class and am getting stuck on my final project, an Address Book style app. I have used the example from our book (Dietel's Android for Programmers An App-Driver Approach, Volume 1, Second Edition) as the basis and have been editing code from there. The biggest thing I want to change/enable is to allow the user to select different fields within the contact (address, email, etc) and pass an Intent that will open Maps when address is selected, eMail when email is selected and so on. Where I am lost is where to place my listener and declare it. Our book and subsequent searches are not really clearing this up at all. I understand the onItemClick call, but am not sure where to place it, within MainActivity.java or the DetailsFragment.java, which is all the code to handle anything that happens when the contact is open and displayed. I feel like I can put it all within the DetailsFragment.java but have a nagging suspicion that I am missing something. I have not had the time to edit and test this yet as I am actually heading out for work in a minute but I thought I could maybe get a better understanding or maybe even better links to read to learn from as my Google search doesnt seem to address what I am looking for. Also, not sure if it matters or not, but I am not using a ListView at all and everything I find tries to force a ListView use. Thank you guys for your help/consideration and time.

Categories

Resources