Created
March 23, 2021 15:27
-
-
Save mattiaferigutti/65f1aebac0d020072ca6e9a843016fe2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.studio.mattiaferigutti.randomwords; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Random; | |
public class MainActivity extends AppCompatActivity { | |
//site from where to get random words https://randomwordgenerator.com/ | |
private Button nextButton, previousButton; | |
private TextView wordText; | |
private List<String> words; | |
private int index = 0; | |
private static final String SAVE_VALUE = "save_index"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
nextButton = findViewById(R.id.nextButton); | |
previousButton = findViewById(R.id.previousButton); | |
wordText = findViewById(R.id.wordText); | |
fillList(); | |
if (savedInstanceState != null) { | |
index = savedInstanceState.getInt(SAVE_VALUE); | |
} | |
wordText.setText(words.get(index)); | |
nextButton.setOnClickListener( view -> { | |
// wordText.setText(getRandomItem()); | |
wordText.setText(getNextItem()); | |
}); | |
previousButton.setOnClickListener(view -> { | |
wordText.setText(getPreviousItem()); | |
}); | |
} | |
private String getRandomItem() { | |
Random random = new Random(); | |
int index = random.nextInt(words.size()); | |
return words.get(index); | |
} | |
private String getNextItem() { | |
index++; | |
return words.get(index % words.size()); | |
} | |
private String getPreviousItem() { | |
if (index > 0) index--; | |
return words.get(index % words.size()); | |
} | |
private void fillList() { | |
//20 words | |
words = createList( | |
"stand", | |
"tank", | |
"food", | |
"mirror", | |
"message", | |
"investment", | |
"dead", | |
"scandal", | |
"rock", | |
"resort", | |
"clown", | |
"employ", | |
"house", | |
"bed" | |
); | |
} | |
// How does SafeVarargs work? | |
// https://stackoverflow.com/questions/14231037/java-safevarargs-annotation-does-a-standard-or-best-practice-exist | |
@SafeVarargs | |
private final <T> List<T> createList(T... listT) { | |
List<T> list = new ArrayList<>(); | |
Collections.addAll(list, listT); | |
// for (T item : listT) { | |
// list.add(item); | |
// } | |
return list; | |
} | |
@Override | |
protected void onSaveInstanceState(@NonNull Bundle outState) { | |
super.onSaveInstanceState(outState); | |
outState.putInt(SAVE_VALUE, index); | |
} | |
// @Override | |
// protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { | |
// super.onRestoreInstanceState(savedInstanceState); | |
// | |
// index = savedInstanceState.getInt(SAVE_VALUE); | |
// wordText.setText(words.get(index)); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment