Update TextView numerous times with Buttons - java

I am making a simple shopping cart application where the user can select items (via buttons) and their running total will be displayed in a text view.
I am fine with having the text view being updated on a single click but I am struggling to figure out how to write my code if the same button is pressed more than once. For example a button writes a value of £3 into the TextView, if this button was clicked again I want the TextView to increase to £6 and so on.
Further to this I want to be able to have more than value added to the TextView from different buttons. I imagine this is more of a Java question as opposed to Android but seeing as I'm a bit of a newbie to both all advice is welcome!

Into your class if you have price value add an incerementPriceValue and make get/set functions for the last one.
If user press the button
setIncrementValue(getIncrementValue()+priceValue);
setText(getIncrementValue+simbolstring);

Create a int variable count and on the click increase the count value and set that value on the textView

Related

Load state of particular recyclerview item

so I have recyclerview populated with data from database. Each row contains two buttons one for play and second for stop time. So what I'm trying is to save state of buttons inside recyclerview. And I used
PreferenceManager.getDefaultSharedPreferences(context).edit().
putInt(Constants.numberOfbutton, getAdapterPosition()).apply();
to save particular position. I have inserted that line of code inside button click and I checked it's saving correct position. What I'm trying to achieve is, when user enters the app after closing if he lefts any of the buttons in play mode to restore that state and continue timer. But problem is when user enters the app all buttons are running. I'm using chronometer for presentation of the time. I think here is the problem in this method:
private void startTime(int position ) {
Data data = datas.get(position);
chronometer.setBase(SystemClock.elapsedRealtime() + Long.parseLong(data.getTime()));
chronometer.start();
}
UPDATE EXPLANATION:
So I can load from database for each item of recyclerview it's time, date, name etc. But I can't figure it out how to play chronometer of just particular item when user enters the app again. NOTE AGAIN: I'm performing correct saving getAdapterAtPosition and loading data from PreferenceManager. I've checked that. Even I tried putting the number of one of rows but same issue appears.
Any help very appreciated. Thanks.
I have found the answer. I save the boolean in database actually I used 1 or 0 than parse that to true or false acordingly is button play pressed or not. Thank you all for your comments and trying to help.
Try to use handler or alarm service instead of chronometer

Android - how to turn numbers in a TextView into buttons

My utility app requires the user to select a meeting from the calendar, then it removes the non-numerical text, and parses what remains using some logic, with the final aim of identifying two numbers only.
Whenever the app fails to remain with 2 numbers only, I want to open a new activity in which the user will "tell" the app which 2 numbers are the relevant ones.
So I want to display the meeting title, location, and description in 3 TextViews but all numbers should be clickable with a special onClick action that will trigger some methods that handle the identified two numbers...
My question is about how to turn the numbers in the textViews to be clickable and have the special onClick action.
I don't think I can use Linkify as I cannot put an onClickListener on the links.
My only idea now is to turn the numbers into clickable buttons and add the relevant listener to the buttons.
1) Any suggestion on how to do this conversion other than simple step by step parsing?
2) Any better idea than using buttons?
Thanks a million
call setOnClickListener(this) on the TextView and supply a onClick method in your code

Reduce amount of spinner items

I have a huge sqlite database (round a about 450 entries)
The user has to pick one of those.
Is there a way to create a textinput field which -after 3 characters are entered- turns into a spinner, where the only elements left are the ones with those charakters starting?
I guess i would have to make a View myself, right?
On the other hand i could make a textview above the spinner, and read it out when the user clicks on the spinner, but i wanted to make it "one" element...

Gain edittext focus tapping next in soft keyboard

I have numerous edittexts inside a listview. I want to type in each of the edittexts one by one without having to scroll through the listview. How can I do that?
Use the android:nextFocusDown param to specify which field you'd like to get focus when the user hits the enter key.

How can user change value of textview with long click only?

I'm creating an app to track a score. The score is displayed in a TextView and when the user clicks the TextView it increments. This works perfectly! However, I would like to setup an onLongClickListener() to edit the text. So, when the user does a LongClick then I would like a digits only editor to pop up on the screen and when the user clicks OK or Done it will update the TextView value to the user inputted amount.
Can someone tell me how I can accomplish this please? This is my first real app so I'm a little puzzled as to how to accomplish this and all the searching on Google/Stackoverflow isn't helping.
Since the TextView is read only, what I've done in the past is have an EditText and a TextView in the same place. Done this way, you'll have to have code to show and hide the two views when you want to enter and exit "edit mode". So, in your onLongClick event, you'll hide the TextView and show the EditText. And on the Enter key (use a key listener on the EditText) and when the EditText loses focus (there's a focus listener as well), you'll do the opposite.
The other piece is how to limit the input to numeric input. Check this out:
https://stackoverflow.com/a/7300516/53501
As far as what to do when the EditText loses focus, I would prompt the user to confirm or cancel the change. I'd personally use an AlertDialog for this (via AlertDialog.Builder).
Having read the comment that this is your first app, please comment if you'd like clarification on any of these things.
Firstly create the view, which you would like to display (see here how to do that). Construct the dialog and inside the onLongClickListener - show the dialog.
Also - assign an onClickListener for a button inside your dialog, which will apply the input to your TextView.
you can do by this way...
text1.setOnLongClickListener(new OnLongClickListener()
{
#Override
public boolean onLongClick(View arg0)
{
text.setText("Your Text");
return false;
}
});

Categories