Solved

How Do I reset Android Button Counter

Posted on 2016-10-02
2
40 Views
Last Modified: 2016-10-22
How do I reset the counter on a button in android studio.

My code is-----

public class MainActivity extends AppCompatActivity {

    private FactBook mFactBook = new FactBook();
    private ArrayList<String> PreviousFacts = new ArrayList<>();
    public int ForwardIndexValue = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);

        final TextView FunFact = (TextView) findViewById(R.id.funFact);
        final Button NextButton = (Button) findViewById(R.id.nextBtn);
        final Button BackButton = (Button) findViewById(R.id.backBtn);

        NextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String AnotherFunFact = mFactBook.getFunFact();
                FunFact.setText(AnotherFunFact);
                PreviousFacts.add(AnotherFunFact);
                ForwardIndexValue++;

            }
        });

        BackButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ForwardIndexValue > 0){
                    ForwardIndexValue--;
                    String HistoryFunFact = PreviousFacts.get(ForwardIndexValue);
                    FunFact.setText(HistoryFunFact);
                }
                else {
                    FunFact.setText("Click Show Next Fun Fact to read more interesting facts about us");

                }

            }

        });

    }
}
0
Comment
Question by:Sol Chikuse
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 17

Accepted Solution

by:
Chris Harte earned 500 total points (awarded by participants)
ID: 41826187
Set it to zero inside the onCreate method. Every time it is created it will be set to zero

       super.onCreate(savedInstanceState);
       setContentView(R.layout.content_main);
       public int ForwardIndexValue = 0;

Open in new window

0
 
LVL 17

Expert Comment

by:Chris Harte
ID: 41855075
This would solve the OP's problem.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question