Skip to content

Instantly share code, notes, and snippets.

@Binary-Finery
Created February 22, 2019 20:59
Show Gist options
  • Save Binary-Finery/5b281235cb8c7c4f3c94ee10c6cee50c to your computer and use it in GitHub Desktop.
Save Binary-Finery/5b281235cb8c7c4f3c94ee10c6cee50c to your computer and use it in GitHub Desktop.
random sentence generator
import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Random random;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
random = new Random();
textView = findViewById(R.id.tv);
}
private String getRandomWord(String[] strings) {
return strings[random.nextInt(strings.length)];
}
private String buildRandomSentence() {
return "\"" +
getRandomWord(Words.PRONOUNS) + " " +
getRandomWord(Words.ADJECTIVES) + " " +
getRandomWord(Words.NOUNS) + " " +
getRandomWord(Words.VERBS) + " " +
getRandomWord(Words.PREPOSITIONS) + " " +
getRandomWord(Words.PRONOUNS) + " " +
getRandomWord(Words.ADJECTIVES) + " " +
getRandomWord(Words.NOUNS) +
".\"";
}
public void generateRandomSentence(View view) {
textView.setText(buildRandomSentence());
}
public void copy(View view) {
String s = textView.getText().toString();
if(!s.isEmpty()){
copySentenceToClipboard(s);
}else{
toast(getString(R.string.tv_empty_message));
}
}
private void copySentenceToClipboard(String s) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", s);
if (clipboard != null) {
clipboard.setPrimaryClip(clip);
toast(getString(R.string.copied_to_clipboard));
}else{
toast(getString(R.string.error_copy_to_clipboard));
}
}
private void toast(String msg){
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment