Android - Star Rating Bar - android

I am using badoo Star Bar, and I have it all set up and working expect for when the method public void onFinalRating(int rating, boolean swipe) { is called, the number of stars that I have selected doesn't stay highlighted, it goes back to the default state. Here is the repo on git hub https://github.com/badoo/StarBar
And my set up is exactly the same, haven't changed anything but here it is anyways,
This is my layout
<com.badoo.mobile.views.starbar.StarBar
android:id="#+id/starBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
And then here I implement it
mStarBar = (StarBar)findViewById(R.id.starBar);
mStarBar.setOnRatingSliderChangeListener(new StarBar.OnRatingSliderChangeListener() {
#Override
public boolean onStartRating() {
// The user has initiated a rating by touching the StarBar. This call will
// immediately followed by a call to onPendingRating with the initial rating
// value.
Toast.makeText(DiningDetailActivity.this, "Started rating", Toast.LENGTH_SHORT).show();
return true;
}
#Override
public void onPendingRating(int rating) {
// This method will first be called when the user initiates a rating and then
// subsequently as the rating is updated (by the user swiping his finger along
// the bar).
Log.i(TAG, Integer.toString(rating) + "");
}
#Override
public void onFinalRating(int rating, boolean swipe) {
// If the rating is completed successfully onFinalRating is called with the
// final result. The swipe parameter specifies if the rating was done using
// a tap (false) or a swipe (true).
Toast.makeText(DiningDetailActivity.this, "Final rating " + rating, Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelRating() {
// Called if the user cancels the rating by swiping away from the StarBar and releasing.
}
});
So my question is how when I select lets say 4 stars to get them to say highlighted, instead of going back to the gray state?
I have looked at the readME file and gone over his code and can't seem to find it.
Thanks so much for the help :)

You need to save the ratings. You only show toasts. I dont know the methods of the library but there is probably a method for changing the active stars.

Related

Second Snackbar seems to trigger undo action of first if created too quickly

I have a RecyclerView and have an undo-functionality, where you delete an item but can press undo on the appearing Snackbar to insert the item back into the list.
Now unfortunately, this happens:
When deleting elements quickly in a row, some of them are being restored while deleting another. It seems like creating a second Snackbar too quickly after the first triggers the Action, e.g. in this case the undo.
Could this be the case, or is it my code that is faulty?
The code isn't too complex:
private void deleteSATemp(int position) {
SelfAffirmation saToDelete = selfAffirmations.get(position);
String content = saToDelete.getContent();
String partOfContent = content.substring(0, 20);
selfAffirmations.remove(position);
notifyItemRemoved(position);
createUndoSnackbar(saToDelete, position, partOfContent);
}
This is the "fake" removal part, where the element is removed from the list visually but not from the database yet, in case of an undo action.
And here is the Snackbar part:
private void createUndoSnackbar(SelfAffirmation saToDelete, int position, String pieceOfSAContent) {
Snackbar snackbar = Snackbar.make(((Activity) mContext).findViewById(R.id.sa_activity_parent_layout), mContext.getString(R.string.snackbarItemDeletedPlaceholder, pieceOfSAContent),
5000);
snackbar.setAction(R.string.snackbarUndoText, view -> {
selfAffirmations.add(position, saToDelete);
notifyItemInserted(position);
});
snackbar.addCallback(new Snackbar.Callback() {
#Override
public void onDismissed(Snackbar snackbar, int event) {
if (event != Snackbar.Callback.DISMISS_EVENT_ACTION) {
mCallbackListener.onItemDeleted(saToDelete);
}
}
});
snackbar.show();
}
The database works with LiveData. I can't imagine that this Android feature would be implemented so carelessly, so it has to be my code.
Yes, IMO you're right in your supposition of events popping up too fast.
According to Snackbar's Documentation you can only have one snackbar displayed at a time.
Snackbars appear above all other elements on screen and only one can be displayed at a time.
They automatically disappear after a timeout or after user interaction elsewhere on the screen, particularly after interactions that summon a new surface or activity. Snackbars can be swiped off screen.
When another snackbar appears the first one is automatically dismissed.
In the other hand: please review how you are implementing this selfAffirmations.remove(position); call. If you are removing positions in an array, and two calls to this method arise at about the same time this can produce unexpected behavior. You'd better search and delete by something unique and non-transferable (the current position of all items will change if you delete the first one), like an id. Cheers.

How to check if button has been clicked

I have a bunch of dynamic buttons which I am setting an onClickListeners as they are produced, as well as tagging them with IDs.
Not sure if this a simple one which I have just spent too much time staring at but this is the problem.
If a user clicks a button, it changes colour this is simple and has been achieved by:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter == 0) {
button.setBackgroundColor(Color.parseColor("#FF4DCBBF"));
Toast.makeText(getActivity(), "User Has Been Marked As Present",Toast.LENGTH_LONG).show();
//change boolean value
userPresent = true;
counter++;
} else {
button.setBackgroundColor(Color.parseColor("#FFFFFF"));
Toast.makeText(getActivity(), "User Has Been Marked As Absent",Toast.LENGTH_LONG).show();
//change boolean value
userPresent = false;
counter = 0;
}
}
});
If the user clicks it again, it will change back to the previous colour - but...
If the user clicks one of the other dynamic buttons that hasn't been previously clicked, the counter is thrown out.
I need to know if the button has been clicked and if not, should mark the user as present.
Currently, If on one button I click it and mark the user as present, and then move onto the next button, I will have to click it once (which marks the user as absent due to the counter) then press it again to mark the user as present.
I need the counter to treat each button individually, any ideas how this could be achieved?
Once the user has been marked present,maybe disable the onClick listener for that button since you wouldn't need it anymore?
I don't mean to sound condescending but I'm having trouble understanding what you're trying to achieve, but if each button is supposed to hold different information about a user, why not make a custom button that does just that? Make a class called customButton in your package and paste the following code there:
import android.content.Context;
import android.widget.Button;
public class customButton extends Button {
boolean haveIBeenClicked; //false by default
public customButton(Context context) {
super(context);
}
public void toggleHaveIBeenClicked(){
haveIBeenClicked=!haveIBeenClicked;
updateBackgroundColor();
}
void updateBackgroundColor(){
if (haveIBeenClicked){
this.setBackgroundColor(Color.parseColor("#FF4DCBBF"));
}
else{
this.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
}
}
then, inside the onClick method (in the activity whose snippet you've shown earlier) you can just call
((customButton)v).toggleHaveIBeenClicked();
...after having created a customButton object and setting an on click listener on it.
Please let me know if this achieves what you desired. If you have trouble running this code, make sure to let me know if the comments and we'll work it out

Android bar chart: undo selection

There's some way to undo a selection on Mikephil charting? I have an application which opens an activity when I selects one value in a bar chart. That works fine, however, when I returns to activity that contains the chart, the selection remains. So, when I selects again, the selection is cleared and the activity does not open. What I want is ever I select a value on bar chart the function "onValueSelected" be executed. How can I do that?
That is the code fragment which calls assyncronously an activity when a value is selected.
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
if(e.getVal() == 0);
else {
GetClientesCadastradosDiaAsync task = new GetClientesCadastradosDiaAsync();
task.execute();
}
}
#Override
public void onNothingSelected() {
// do nothing
}
});
At the end of your onValueSelected() method, call:
chart.highlightValues(null);
Now, this will only remove the highlighting. If you select that same bar again, onNothingSelected() will be called.
Therefore, in onNothingSelected(), again call onValueSelected(). You will have to pass the parameters here, but looks like you will only need the Entry parameter, and for the other 2 you can pass null.

Scratch image in android?

I want to show image only when user scratch on that, image will be overlay with colors. So when user scratch on it they can see the image. I will finished that part by using this link.
But what i need is while user scratch on that image, progress bar want to show the current progress. if user scratches all the portion of image progress bar should finish 100%.
If you use Android-WScratchView library, you can register OnScratchCallback and assign the percentage of cleared part to your progressbar
scratchView.setOnScratchCallback(new WScratchView.OnScratchCallback() {
#Override
public void onScratch(float percentage) {
yourProgressBar.setProgress(percentage);//Assign progressbar value here
}
#Override
public void onDetach(boolean fingerDetach) {
if(mPercentage > 50){
scratchView.setScratchAll(true);
updatePercentage(100);
}
}
});
You can check full sample at here

Adding features to Clipboard ActionBar

In my app, I want to be able to start my own function when user copy a text to the clipboard.
I'm overriding function onActionModeFinished and as I want to be sure user tapped on Copy I'm getting selected navigation index. Unfortunately if always return -1. Is there something I'm doing wrong here ?
#Override
public void onActionModeFinished(ActionMode mode) {
super.onActionModeFinished(mode);
if (_actionBar != null) {
int index = _actionBar.getSelectedNavigationIndex();
// here index is always -1
}
}
ActionBar can contain tabs and list selector on the left side. So, getSelectedNavigationIndex is used to get the position in this navigation items not in the menu items located in the right side.

Categories

Resources