how to add like button with a counter to my android app - java

I have wondered the facebook developers docs,
but saw only how to add "like action" and "likes counter" to html page.
I mean for the like counter saved in fb servers for that page.
How is it done in java for an android app?

In Xml layout in which u have button, in button attribute add
android:onclick="like_counter";// this will call that method
in activity,
globally code this
int counter=0;
public int like_counter()
{
counter++;
return counter;
}
hope this is what u r asking...

Related

How can I show a confirm dialog with Vaadin 23 when the user clicks on back button?

how can I show a dialog to stay or leave the current page with Vaadin 23, when a user clicks back button on browser?
Regards
It depends what you wish to achieve.
See this older discussion: Vaadin onbeforeunload event
Generally: use the onBeforeUnload javascript even for this
https://www.w3schools.com/tags/ev_onbeforeunload.asp
This is executed when the user would go away from your vaadin app, but not when using the back button inside your vaadin app.
For these you can use the navigation lifecycle events as documented here
https://vaadin.com/docs/latest/routing/lifecycle
Not sure if it also catches, when a user leaves your app...
Assuming you mean, that is it possible to prevent the navigation happening, you simply can't do that. If disabling back button is important for you, the only way is to enforce your users to use the application via desktop shortcut which starts the app using --app paramater (if using Chrome). This is not a limitation in Vaadin, but a general restriction in browser behavior.
There is already a possibility to handle Browser Back Button Event on Vaadin (https://vaadin.com/docs/v14/flow/routing/tutorial-routing-lifecycle):
public class SignupForm extends Div implements BeforeLeaveObserver {
#Override
public void beforeLeave(BeforeLeaveEvent event) {
if (this.hasChanges()) {
ContinueNavigationAction action = event.postpone();
ConfirmDialog.build("Are you sure you want"+
" to leave this page?")
.ifAccept(action::proceed)
.show();
}
}
private boolean hasChanges() {
// no-op implementation
return true;
}
}
This code works once but when you click on Cancel on Confirm Dialog so that you want to stay on current page and click again on Back Button on Browser, than you don't see any Confirm Dialog again... I can not understand, why...

Prevent chip from double click

I have a chipgroup and some chips inside. I loop through this chipgroup, get every chip and add a checkedChangeListener for it.
for (int i = 0; i < chipGroup.getChildCount(); i++) {
final Chip chip = (Chip) chipGroup.getChildAt(i);
chip.setOnCheckedChangeListener((buttonView, isChecked) -> {
//My code
});
}
So when I open my app and click on chip, e.g. chip1, it selects it. Then when I click on that chip1 second time, it also fires the checkedChangeListener and runs my code inside it. The isChecked status is not helping, because it just changes it value like true->false->true->false on every click. But I just don't want to run my code inside onCheckedChangeListener, if the chip is already checked.
One solution I think is to save my selected chip inside fragment or ViewModel. What's your solution?
I believe you can just set a setOnCheckedChangeListener on the chipGroup instead of each individual chip. That listener will pass in an ID and isChecked as parameters. I believe using this callback on the group will be help resolve your issue. Hopefully that helps with the issue you're having.
More info on https://material.io/components/chips/android#using-chips

How to show different page if user is first time user (Android)

I have a blank-ish Android Project and what I want to do is take the user to a different "page/screen" if it is their first time only.
I know the logic for this but since I'm new to Android, I'm unsure of how to code this.
Below are the steps I believe I need to take in order to accomplish this:
App loads. If Local storage contains setting "FirstTimeUser", then it is not their first time using the app. Show the MainActivity page. If FirstTimeUser setting does not exist, it is their first time using the app (or they have uninstalled and reinstalled it), so instead, show WelcomeActivity page.
After viewing Welcome activity page, create FirstTimeUser setting and set to False.
But how do I code this for an Android app?
Use shared preferences as shown:
//declare as global
SharedPreferences prefs = null;
//and in your onCreate method:
prefs = getSharedPreferences("packageNameHere", MODE_PRIVATE);
if (prefs.getBoolean("firstrun", true)) {
//do stuff here if first run
//make sure to flag the boolean as false
prefs.edit().putBoolean("firstrun", false).commit();
}
else{
//if not first run, do something else
}
There is something called "SharedPreferences" that i believe your looking for.
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html

Android SDK: View Pager not Going Back to Correct View

I have a view which can be accessed in 3 ways depending on the users' actions.
For example, I have a shop description page which can be accessed via:
Searching for the shop
Clicking on the shop via a Nearby Shops Map Feature
Clicking on the shop via a list view
The problem I am having is that when the user is on the shop description page, and clicks back, it doesn't go back to the correct previous page. So if they accessed the shop description via the nearby map feature, clicking back brings them to the listview which is obviously not right.
I tried to solve this problem by using the following code to set a referrer:
SharedPreferences preferences = this.getSharedPreferences("SHARED_PREF",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("referrer", "nearby");
editor.commit();
I then checked against this shared preference in the onBackPressed method:
public static void onBackPressed() {
System.out.println("REFERRER NOW EQUALS= "+referrer);
if(referrer == "nearby") {
TabsViewPagerFragmentActivity.mViewPager.setCurrentItem(13);
} else if(referrer == "search-list"){
TabsViewPagerFragmentActivity.mViewPager.setCurrentItem(15);
} else {
TabsViewPagerFragmentActivity.mViewPager.setCurrentItem(10);
}
}
However it still isn't working right. Is there a better way to solve this? Thanks
You compare strings with :
equals();
Instead of == use :
if(referrer.equals("nearby")) {

Creating a Custom Expanded View in Code

I want to customize the notification area, adding an icon to the right and few buttons.
I've read the tutorial here: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
The problem is that I need to include this code in a library, an SDK that I want to distribute to improve notifications. (See http://hub.buzzbox.com/)
Is it possible to write all the UI in code, without the need of the xml to describe the remote view? This is because resources cannot be included in an SDK, so I would need to ask the users of my SDK to add an xml to their resources and to reference all the resources by name... which I would like to avoid.
I've already written other parts of the SDK user interface completely in Java code but I'm having issues to do the same for the Remove View.
A RemoteView is usually created like this:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
Can I create a RemoteView from I layout that I create with Java code?
Any other solutions?
I thought that you might be able to do this with your own Parcel, but looking at the code, the Parcel simply stores the package name and layout (resource) id, as used by the main constructor.
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mPackage);
dest.writeInt(mLayoutId);
int count;
if (mActions != null) {
count = mActions.size();
} else {
count = 0;
}
dest.writeInt(count);
for (int i=0; i<count; i++) {
Action a = mActions.get(i);
a.writeToParcel(dest, 0);
}
}
I can't see a way of doing this and think it may not be possible.

Categories