Android bar chart: undo selection - android

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.

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.

Libgdx - How to close Dialog and continue operations on current screen?

I have a problem with closing the Dialog. I can close it, but I have to press the button more than once. I have two buttons and one that is used for closing the application works fine, but the second one for closing the dialog doesn't work properly.
The button: button("No", "continue");
I'm using remove() method in overriden method result(Object object) when a parameter object is equal to "continue".
#Override
protected void result(Object object) {
if (object.equals("exit")){
Gdx.app.exit();
}
else if (object.equals("continue")){
remove();
}
}
I have also tried to use hide(null); but I got the same result.
This is my working dialog code for "Are you sure you want to quit?"
Dialog dialog = new Dialog("Exit", Assets.skin1) {
#Override
protected void result(Object object) {
if ((Boolean) object) {
Gdx.app.exit();
}else{
//not necessary but if dialog not hide, call hide() here
//hide();
}
}
};
dialog.text("Are you sure you want to quit?");
dialog.button("Yes", true).button("No", false);
dialog.getContentTable().pad(20);
dialog.getTitleTable().pad(20);
dialog.padTop(60); // set padding on top of the dialog title
dialog.setModal(true);
dialog.setMovable(false);
dialog.setResizable(false);
dialog.show(stage);
"No" button will close the dialog.

Android - Star Rating Bar

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.

how to implement simple like button in listview of each item

I have certain entries in my list view item. There I have a simple "like button" (not facebook like button). You can see the above mentioned SCREENSHOT; for the reference.
The moment I click on like button; i want the like button color to be changed and the like button color should remain same(changed on like) when I'll login again.
Also, all the entries must get filled in Database with cust_id, bus_id, Offer_id using json; that I know very well.
When I again click on the same button(like button), whose color has been changed. It must be changed back to the default color and data must get removed from database.
How can I do this...?
1. How to get value of click button.
2. How to bring back the changed color to default; once the button has been re-clicked.
Plz suggest me...
this is button code
holder.b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clicked) {
holder.b1.setBackgroundResource(R.drawable.like_icon_hover);
} else {
holder.b1.setBackgroundResource(R.drawable.like_icon);
}
clicked = true;
}
});
You need to add a listener to the button and using ValueAnimator you can change the button color and reverse it back when you click again.
Here is a simple and best approach to achieve your scenario. Add the onClick listener for the button in your list item like this.. I have explained each line ..
// set a default background color to the button
placeHolder.likeButton.setBackgroundColor(Color.RED);
placeHolder.likeButton.setOnClickListener(new View.OnClickListener() {
ValueAnimator buttonColorAnim = null; // to hold the button animator
#Override
public void onClick(View v) {
// first time this will be null
if(buttonColorAnim != null){
// reverse the color
buttonColorAnim.reverse();
// reset for next time click
buttonColorAnim = null;
// add your code here to remove from database
}
else {
final Button button = (Button) v;
// create a color value animator
buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.BLUE);
// add a update listener for the animator.
buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
// set the background color
button.setBackgroundColor((Integer) animator.getAnimatedValue());
}
});
// you can also set a delay before start
//buttonColorAnim.setStartDelay(2000); // 2 seconds
// start the animator..
buttonColorAnim.start();
// add your code here to add to database
}
}
});
This will change the button color on your first click and then revert the color back on the next click. You can also set a delay to change the color.
Note: You have to set the default button color based on your logic.
#Override
public void onClick(View view) {
if(!check)
{
personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup_blue);
check = true;
}
else
{
personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup);
check = false;
}
}
you can use custom adapter for your listview(it has own layout.xml),and you can set your clicklistener in it.
You can change color or what you want. Actually I did have project like you want.I put some link if you can t do it.
Try following:
use setOnClickListener() on the button.
eg.
viewHolder.imgVwFbLike.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
// TODO :
// 1. make webservice call to update like status (Assuming a web service call)
// 2. Implement a callback for webservice call, to get the status of request.
if(success)
a) change the colour of like btn. and insert the data in Db.
b) Also maintain a column in db for likestatus(by default set it false).
}
}
);
Assuming you are fetching the data from db when you login, you can check the likestatus and set the color of button accordingly.

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