Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. package com.example.textjumble;
  2.  
  3. import android.app.AlertDialog;
  4. import android.os.Bundle;
  5.  
  6. import android.content.DialogInterface;
  7.  
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13.  
  14. import androidx.appcompat.app.AppCompatActivity;
  15.  
  16. import java.util.ArrayList;
  17. import java.util.List;
  18.  
  19.  
  20. public class MainActivity extends AppCompatActivity implements OnClickListener
  21. {
  22. private Button exitButton;
  23. private EditText nameEntry;
  24. private String name;
  25. /** Called when the activity is first created. */
  26. @Override
  27. public void onCreate(Bundle savedInstanceState)
  28. {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31. exitButton = (Button)findViewById(R.id.exitButton);
  32. nameEntry = (EditText)findViewById(R.id.nameEntry);
  33. nameEntry.setWidth(120);
  34. exitButton.setOnClickListener(this);
  35. nameEntry.setFocusable(true);
  36. }// End of onCreate
  37. @Override
  38. public void onClick(View v)
  39. {
  40. // Check for exit button. Pop up dialogue if found
  41. if (v == exitButton)
  42. {
  43. name = nameEntry.getText().toString();
  44. showtbDialog(name);
  45. }
  46. }// End of onClick
  47. private void showtbDialog(String s)
  48. {
  49. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  50. char [] array = s.toCharArray();
  51. int len = s.length();
  52. for (int i = 0; i < 10; i++)
  53. {
  54. int max = array.length;
  55. int min = 1;
  56. int a = (int)(Math.random() * ((max - min) + 1)) + min;
  57. int b = (int)(Math.random() * ((max - min) + 1)) + min;
  58. a--;
  59. b--;
  60. char temp = array[a];
  61. array[a] = array[b];
  62. array[b] = temp;
  63. }
  64.  
  65.  
  66. String newS = new String (array);
  67.  
  68. builder.setMessage(newS);
  69. builder.setCancelable(true);
  70.  
  71.  
  72. AlertDialog alert = builder.create();
  73. alert.show();
  74. }
  75. }// End of Activity class
  76.  
  77.  
  78.  
  79.  
  80.  
  81. -------------------------------------------------------------------
  82.  
  83.  
  84. <?xml version="1.0" encoding="utf-8"?>
  85. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  86. android:orientation="vertical"
  87. android:layout_width="fill_parent"
  88. android:layout_height="fill_parent"
  89. >
  90. <TextView
  91. android:layout_width="fill_parent"
  92. android:layout_height="wrap_content"
  93. android:text="Welcome to Mobile Device Programming"
  94. />
  95. <LinearLayout
  96. android:orientation="horizontal"
  97. android:layout_width="fill_parent"
  98. android:layout_height="wrap_content"
  99. >
  100. <TextView
  101. android:id="@+id/nameLabel"
  102. android:layout_width="wrap_content"
  103. android:layout_height="wrap_content"
  104. android:text="Enter string to jumble "
  105. />
  106. <EditText
  107. android:id="@+id/nameEntry"
  108. android:layout_width="wrap_content"
  109. android:layout_height="wrap_content"
  110. android:gravity="center"
  111. android:inputType="text"
  112. android:text=""
  113. />
  114. <requestFocus />
  115. </LinearLayout>
  116. <Button
  117. android:id="@+id/exitButton"
  118. android:layout_width="fill_parent"
  119. android:layout_height="wrap_content"
  120. android:text="Press to Jumble"
  121. />
  122.  
  123.  
  124. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement