Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. import android.os.Bundle;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.view.View;
  4. import android.widget.Button;
  5. import android.widget.CheckBox;
  6. import android.widget.RadioButton;
  7. import android.widget.TextView;
  8.  
  9. import java.util.Random;
  10.  
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14. Button btnGenPassword;
  15. Button btnClear;
  16. TextView lblResult;
  17. RadioButton radioCurrent;
  18. CheckBox checkUpper;
  19. CheckBox checkLower;
  20. CheckBox checkSpecial;
  21. CheckBox checkNumber;
  22. int pwLength;
  23. String validChars = "";
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29.  
  30. btnGenPassword = (Button) findViewById(R.id.btnGeneratePassword);
  31. btnClear = (Button) findViewById(R.id.btnClear);
  32. lblResult = (TextView) findViewById(R.id.lblResult);
  33.  
  34. checkLower = (CheckBox) findViewById(R.id.checkLower);
  35. checkUpper = (CheckBox) findViewById(R.id.checkUpper);
  36. checkSpecial = (CheckBox) findViewById(R.id.checkSpecial);
  37. checkNumber = (CheckBox) findViewById(R.id.checkNumber);
  38.  
  39. //Start the radio
  40. radioCurrent = (RadioButton) findViewById(R.id.radio_8);
  41. radioCurrent.isChecked();
  42. pwLength = 8;
  43.  
  44. //set up onClick sending result to textfield
  45. btnGenPassword.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. String result = passwordGen();
  49. lblResult.setText(result);
  50. CheckBox();
  51.  
  52. }
  53. });
  54.  
  55. //Clear result
  56. btnClear.setOnClickListener(new View.OnClickListener() {
  57. @Override
  58. public void onClick(View v) {
  59. lblResult.setText("");
  60. }
  61. });
  62. }
  63.  
  64. public String passwordGen() {
  65. char[] usableChars = validChars.toCharArray();
  66. String password = new String();
  67. Random generator = new Random();
  68.  
  69. for (Integer i = 0; i < pwLength; i++) {
  70. password = password + usableChars[generator.nextInt(usableChars.length)];
  71. }
  72.  
  73. return password;
  74.  
  75. }
  76. //Radio Button
  77. public void onRadioButtonClicked(View view) {
  78. //Is the button now checked
  79. boolean checked = ((RadioButton) view).isChecked();
  80. //check which radio button was clicked
  81. switch (view.getId()) {
  82. case R.id.radio_8:
  83. if (checked) {
  84. pwLength = 8;
  85. }
  86. break;
  87. case R.id.radio_10:
  88. if (checked) {
  89. pwLength = 10;
  90. }
  91. break;
  92. case R.id.radio_12:
  93. if (checked) {
  94. pwLength = 12;
  95. }
  96. break;
  97. }
  98. }
  99.  
  100. public void CheckBox() {
  101.  
  102. validChars = "";
  103. // checkbox checked and and listing valid characters
  104. if (checkNumber.isChecked()) {
  105. validChars = "123456789";
  106. }
  107. if (checkSpecial.isChecked()) {
  108. validChars = "!@#$%^&*()";
  109. }
  110. if (checkLower.isChecked()) {
  111. validChars = "abcdefghijklmnopqrstuvwxyz";
  112. }
  113. if (checkUpper.isChecked()) {
  114. validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  115. }
  116. }
  117.  
  118. }
  119.  
  120. Here is my xml file
  121.  
  122. <EditText
  123. android:id="@+id/lblResult"
  124. android:layout_width="368dp"
  125. android:layout_height="wrap_content"
  126. android:ems="10"
  127. android:inputType="text"
  128. tools:layout_editor_absoluteX="8dp"
  129. tools:layout_editor_absoluteY="16dp" />
  130.  
  131.  
  132. <RadioGroup
  133. android:layout_width="wrap_content"
  134. android:layout_height="wrap_content"
  135. android:orientation="horizontal">
  136.  
  137. <RadioButton
  138. android:id="@+id/radio_8"
  139. android:layout_width="wrap_content"
  140. android:layout_height="wrap_content"
  141. android:layout_marginLeft="20sp"
  142. android:layout_weight="1"
  143. android:onClick="onRadioButtonClicked"
  144. android:text="8 Char"
  145. tools:layout_editor_absoluteX="8dp"
  146. tools:layout_editor_absoluteY="77dp" />
  147.  
  148. <RadioButton
  149. android:id="@+id/radio_10"
  150. android:layout_width="wrap_content"
  151. android:layout_height="wrap_content"
  152. android:layout_weight="1"
  153. android:onClick="onRadioButtonClicked"
  154. android:text="10 Char"
  155. tools:layout_editor_absoluteX="139dp"
  156. tools:layout_editor_absoluteY="77dp" />
  157.  
  158. <RadioButton
  159. android:id="@+id/radio_12"
  160. android:layout_width="wrap_content"
  161. android:layout_height="wrap_content"
  162. android:layout_weight="1"
  163. android:onClick="onRadioButtonClicked"
  164. android:text="12 Char"
  165. tools:layout_editor_absoluteX="287dp"
  166. tools:layout_editor_absoluteY="77dp" />
  167.  
  168. </RadioGroup>
  169.  
  170. <TableRow
  171. android:layout_width="match_parent"
  172. android:layout_height="match_parent"
  173. android:orientation="horizontal"
  174. tools:layout_editor_absoluteX="173dp"
  175. tools:layout_editor_absoluteY="192dp">
  176.  
  177. <CheckBox
  178. android:id="@+id/checkNumber"
  179. android:layout_width="wrap_content"
  180. android:layout_height="wrap_content"
  181. android:layout_marginLeft="60sp"
  182. android:text="Numbers"
  183. tools:layout_editor_absoluteX="76dp"
  184. tools:layout_editor_absoluteY="118dp" />
  185.  
  186. <EditText
  187. android:id="@+id/editText2"
  188. android:layout_width="0dp"
  189. android:layout_height="wrap_content"
  190. android:background="@null"
  191. android:selectAllOnFocus="false" />
  192.  
  193. <CheckBox
  194. android:id="@+id/checkSpecial"
  195. android:layout_width="wrap_content"
  196. android:layout_height="wrap_content"
  197. android:layout_marginLeft="35sp"
  198. android:text="Spec Char"
  199. tools:layout_editor_absoluteX="210dp"
  200. tools:layout_editor_absoluteY="119dp" />
  201.  
  202. <EditText
  203. android:id="@+id/editText4"
  204. android:layout_width="0dp"
  205. android:layout_height="wrap_content"
  206. android:background="@null"
  207. android:selectAllOnFocus="false" />
  208. </TableRow>
  209.  
  210. <TableRow
  211. android:layout_width="match_parent"
  212. android:layout_height="match_parent"
  213. tools:layout_editor_absoluteX="192dp"
  214. tools:layout_editor_absoluteY="255dp"
  215. android:orientation="horizontal">
  216.  
  217. <CheckBox
  218. android:id="@+id/checkLower"
  219. android:layout_width="wrap_content"
  220. android:layout_height="wrap_content"
  221. android:layout_marginLeft="60sp"
  222. android:text="Lower Case"
  223. tools:layout_editor_absoluteX="210dp"
  224. tools:layout_editor_absoluteY="160dp" />
  225.  
  226. <EditText
  227. android:id="@+id/editText3"
  228. android:layout_width="0dp"
  229. android:layout_height="wrap_content"
  230. android:background="@null" />
  231.  
  232. <CheckBox
  233. android:id="@+id/checkUpper"
  234. android:layout_width="wrap_content"
  235. android:layout_height="wrap_content"
  236. android:layout_marginLeft="35sp"
  237. android:text="Upper Case"
  238. tools:layout_editor_absoluteX="76dp"
  239. tools:layout_editor_absoluteY="160dp" />
  240. </TableRow>
  241.  
  242. <Button
  243. android:id="@+id/btnGeneratePassword"
  244. android:layout_marginTop="5dp"
  245. android:layout_width="wrap_content"
  246. android:layout_height="wrap_content"
  247. android:onClick="CheckBox"
  248. android:background="?android:attr/colorActivatedHighlight"
  249. android:text="Generate Password" />
  250.  
  251. <Button
  252. android:id="@+id/btnClear"
  253. android:layout_width="wrap_content"
  254. android:layout_height="wrap_content"
  255. android:layout_marginTop="10sp"
  256. android:background="?android:attr/colorActivatedHighlight"
  257. android:onClick="onRadioButtonClicked"
  258. android:text="Clear" />
  259. </TableLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement