Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java code to show action on x amount of presses?

I have an iOS app that I worked on some time ago, and the function it uses I like, and would like to somehow get it to work on some Android code I have.

At the top of my MainActivity I have

SharedPreferences sharedpreferences;

public static final String MyPREFERENCES = "nShownLobby";

on my onCreate I have

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

And I am calling this method

changeTextButton.setOnClickListener(new View.OnClickListener() {
    
    @Override
    public void onClick(View v){
        long nb_shown_lobby = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).getLong("nShownLobby", 0); 
        ++nb_shown_lobby; 
        if ((nb_shown_lobby % 20) == 0) { 
            this.turnAround(); 
        }
        int random = (int) (Math.random() * manyDifferentStrings.length);
        if (random == oldVaue) {
            random = (int) (Math.random() * manyDifferentStrings.length);
        }
        changingText.setText(manyDifferentStrings[random]);
        oldVaue = random;
        try {
            mySound.start();
        } catch (NullPointerException e) {
            mySound = MediaPlayer.create(MainActivity.this, R.raw.sound);
        }
    }

    private void turnAround() {
        //Do something here
        Log.i("Do Something ", "");
    }
});

The intention is that after every 20 presses, the turnAround() method is called but it does not… It just gets ignored – I am guessing I have missed something?

>Solution :

**According to your question your code should be like this**

private SharedPreferences sharedpreferences;
private SharedPreferences.Editor editor;
private static final String MyPREFERENCES = "nShownLobby";

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

    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    editor = sharedpreferences.edit();

    changeTextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){


            int nb_shown_lobby = sharedpreferences.getInt("nShownLobby", 0);
            if ((nb_shown_lobby % 20) == 0) {
                turnAround();
            }

            nb_shown_lobby += 1;
            editor.putInt("nShownLobby", nb_shown_lobby);
            editor.commit();

            int random = (int) (Math.random() * manyDifferentStrings.length);
            if (random == oldVaue) {
                random = (int) (Math.random() * manyDifferentStrings.length);
            }
            changingText.setText(manyDifferentStrings[random]);
            oldVaue = random;

            try {
                mySound.start();
            } catch (NullPointerException e) {
                mySound = MediaPlayer.create(MainActivity.this, R.raw.sound);

            }

        }

    });


}

//created this in activity, not in the button onclick
private void turnAround() {

    //Do something here
    Log.i("Do Something ", "");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading