Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.26 KB | None | 0 0
  1. package com.example.main.beginningpainttest;
  2.  
  3. import android.app.AlertDialog;
  4. import android.app.Fragment;
  5. import android.app.FragmentManager;
  6. import android.app.FragmentTransaction;
  7. import android.app.ProgressDialog;
  8. import android.content.ClipData;
  9. import android.content.Context;
  10. import android.content.DialogInterface;
  11. import android.graphics.Color;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.os.Bundle;
  14. import android.view.ContextMenu;
  15. import android.view.ContextMenu.ContextMenuInfo;
  16. import android.view.Gravity;
  17. import android.view.MenuInflater;
  18. import android.view.MenuItem;
  19. import android.view.View;
  20. import android.widget.Button;
  21. import android.widget.EditText;
  22. import android.widget.LinearLayout;
  23. import android.widget.PopupMenu;
  24. import android.widget.TextView;
  25. import android.widget.Toast;
  26. import static com.example.main.beginningpainttest.DrawingArea.clearCanvas;
  27. import android.view.View.OnClickListener;
  28.  
  29.  
  30. public class MainActivity extends AppCompatActivity {
  31.  
  32. Button button1, brushSizeButton;
  33. Button button2;
  34. Button button3;
  35. Button button4;
  36. Button button5;
  37. Button button6;
  38. Button button7;
  39. Button button8;
  40. Button button9;
  41. Button button10;
  42. Button button11;
  43. Button button12;
  44. Button customColorBtn;
  45. int customButtonBGColor = Color.rgb(0, 0, 0);
  46.  
  47.  
  48.  
  49. @Override
  50. protected void onCreate(Bundle savedInstanceState) {
  51. super.onCreate(savedInstanceState);
  52.  
  53. setContentView(R.layout.activity_main);
  54.  
  55. button1 = (Button) findViewById(R.id.button1);
  56. button2 = (Button) findViewById(R.id.button2);
  57. button3 = (Button) findViewById(R.id.button3);
  58. button4 = (Button) findViewById(R.id.button4);
  59. button5 = (Button) findViewById(R.id.button5);
  60. button6 = (Button) findViewById(R.id.button6);
  61. button7 = (Button) findViewById(R.id.button7);
  62. button8 = (Button) findViewById(R.id.button8);
  63. button9 = (Button) findViewById(R.id.button9);
  64. button10 = (Button) findViewById(R.id.button10);
  65. button11 = (Button) findViewById(R.id.button11);
  66. button12 = (Button) findViewById(R.id.button12);
  67. brushSizeButton = (Button) findViewById(R.id.brushSizeButton);
  68. customColorBtn = (Button) findViewById(R.id.customColorBtn);
  69.  
  70. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
  71. LinearLayout layout = new LinearLayout(MainActivity.this);
  72. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  73. layout.setOrientation(LinearLayout.VERTICAL);
  74. layout.setLayoutParams(params);
  75.  
  76. layout.setGravity(Gravity.CLIP_VERTICAL);
  77. layout.setPadding(2, 2, 2, 2);
  78.  
  79. TextView tv = new TextView(MainActivity.this);
  80. tv.setText("Custom Hexadecimal Colors");
  81. tv.setPadding(40, 40, 40, 40);
  82. tv.setGravity(Gravity.CENTER);
  83. tv.setTextSize(20);
  84.  
  85. EditText et = new EditText(MainActivity.this);
  86. String etStr = et.getText().toString();
  87. TextView tv1 = new TextView(MainActivity.this);
  88. tv1.setText("Input Hex Value");
  89.  
  90. LinearLayout.LayoutParams tv1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  91. tv1Params.bottomMargin = 5;
  92. layout.addView(tv1,tv1Params);
  93. layout.addView(et, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
  94.  
  95. alertDialogBuilder.setView(layout);
  96. alertDialogBuilder.setTitle("Custom Hex Colors");
  97. alertDialogBuilder.setCustomTitle(tv);
  98.  
  99.  
  100. customColorBtn.setOnClickListener(new OnClickListener() {
  101. @Override
  102. public void onClick(View v) {
  103. //instance of pop-up menu
  104. PopupMenu popup = new PopupMenu(MainActivity.this, customColorBtn);
  105. //inflating using xml
  106. popup.getMenuInflater().inflate(R.menu.customcolormenu, popup.getMenu());
  107.  
  108. popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
  109. public boolean onMenuItemClick(MenuItem item) {
  110. int itemSelected = item.getItemId();
  111. initializeCustomAlertDialogue();
  112.  
  113. switch (itemSelected) {
  114. case R.id.item1:
  115.  
  116. alertDialogBuilder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
  117. public void onClick(DialogInterface dialog, int id) {
  118.  
  119. String hexValue = et.getText().toString();
  120.  
  121. Context context = getApplicationContext();
  122. CharSequence text = "Value entered: " + hexValue;
  123. int duration = Toast.LENGTH_SHORT;
  124.  
  125. Toast toast = Toast.makeText(context, text, duration);
  126. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  127. toast.show();
  128. int color;
  129.  
  130. try {
  131. color = Integer.parseInt(hexValue, 16);
  132. }
  133.  
  134. catch(Exception ex) {
  135. color = 000000;
  136. }
  137.  
  138. final int r = (color >> 16) & 0xFF;
  139. final int g = (color >> 8) & 0xFF;
  140. final int b = (color >> 0) & 0xFF;
  141.  
  142.  
  143. text = "RGB: " + r + " " + g + " " + " " + b;
  144.  
  145. toast = Toast.makeText(context, text, duration);
  146. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  147. toast.show();
  148.  
  149. customButtonBGColor = Color.rgb(r, g, b);
  150. button1.setBackgroundColor(customButtonBGColor);
  151. button1.setOnClickListener(new OnClickListener() {
  152. @Override
  153. public void onClick(View v) {
  154. DrawingArea.paintChangeCustom(r, g, b);
  155. }
  156. });
  157.  
  158.  
  159.  
  160.  
  161. }
  162.  
  163. });
  164.  
  165. alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  166. public void onClick(DialogInterface dialog, int id) {
  167.  
  168. Context context = getApplicationContext();
  169. CharSequence text = "Cancel Button Pressed";
  170. int duration = Toast.LENGTH_SHORT;
  171.  
  172. Toast toast = Toast.makeText(context, text, duration);
  173. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  174. toast.show();
  175.  
  176. }
  177.  
  178. });
  179. alertDialogBuilder.show();
  180. return true;
  181.  
  182. default:
  183. return false;
  184. }
  185.  
  186.  
  187. }
  188. });
  189.  
  190. popup.show();
  191. }
  192. }
  193.  
  194. );
  195.  
  196.  
  197. }
  198.  
  199. public void initializeCustomAlertDialogue(){
  200.  
  201. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
  202. LinearLayout layout = new LinearLayout(MainActivity.this);
  203. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  204. layout.setOrientation(LinearLayout.VERTICAL);
  205. layout.setLayoutParams(params);
  206.  
  207. layout.setGravity(Gravity.CLIP_VERTICAL);
  208. layout.setPadding(2, 2, 2, 2);
  209.  
  210. TextView tv = new TextView(MainActivity.this);
  211. tv.setText("Custom Hexadecimal Colors");
  212. tv.setPadding(40, 40, 40, 40);
  213. tv.setGravity(Gravity.CENTER);
  214. tv.setTextSize(20);
  215.  
  216. EditText et = new EditText(MainActivity.this);
  217. String etStr = et.getText().toString();
  218. TextView tv1 = new TextView(MainActivity.this);
  219. tv1.setText("Input Hex Value");
  220.  
  221. LinearLayout.LayoutParams tv1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  222. tv1Params.bottomMargin = 5;
  223. layout.addView(tv1,tv1Params);
  224. layout.addView(et, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
  225.  
  226. alertDialogBuilder.setView(layout);
  227. alertDialogBuilder.setTitle("Custom Hex Colors");
  228. alertDialogBuilder.setCustomTitle(tv);
  229. }
  230.  
  231.  
  232. public void blueButton(View view){
  233. DrawingArea.paintChangeBlue();
  234. Context context = getApplicationContext();
  235. CharSequence text = DrawingArea.paintColor + "Blue Button Pressed";
  236. int duration = Toast.LENGTH_SHORT;
  237.  
  238. Toast toast = Toast.makeText(context, text, duration);
  239. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  240. toast.show();
  241.  
  242. }
  243.  
  244. public void redButton(View view){
  245. DrawingArea.paintChangeRed();
  246. Context context = getApplicationContext();
  247. CharSequence text = DrawingArea.paintColor + "Red Button Pressed";
  248. int duration = Toast.LENGTH_SHORT;
  249.  
  250. Toast toast = Toast.makeText(context, text, duration);
  251. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  252. toast.show();
  253.  
  254. }
  255.  
  256. public void greenButton(View view){
  257. DrawingArea.paintChangeGreen();
  258. Context context = getApplicationContext();
  259. CharSequence text = DrawingArea.paintColor + "Green Button Pressed";
  260. int duration = Toast.LENGTH_SHORT;
  261.  
  262. Toast toast = Toast.makeText(context, text, duration);
  263. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  264. toast.show();
  265.  
  266. }
  267.  
  268. public void purpleButton(View view){
  269. DrawingArea.paintChangePurple();
  270. Context context = getApplicationContext();
  271. CharSequence text = DrawingArea.paintColor + "Purple Button Pressed";
  272. int duration = Toast.LENGTH_SHORT;
  273.  
  274. Toast toast = Toast.makeText(context, text, duration);
  275. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  276. toast.show();
  277.  
  278. }
  279.  
  280. public void eraserButton(View view){
  281. DrawingArea.paintChangeErase();
  282. Context context = getApplicationContext();
  283. CharSequence text = DrawingArea.paintColor + "Eraser Button Pressed";
  284. int duration = Toast.LENGTH_SHORT;
  285.  
  286. Toast toast = Toast.makeText(context, text, duration);
  287. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  288. toast.show();
  289. }
  290.  
  291. public void whiteButton(View view){
  292. DrawingArea.paintChangeWhite();
  293. Context context = getApplicationContext();
  294. CharSequence text = DrawingArea.paintColor + "White Button Pressed";
  295. int duration = Toast.LENGTH_SHORT;
  296.  
  297. Toast toast = Toast.makeText(context, text, duration);
  298. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  299. toast.show();
  300.  
  301. }
  302.  
  303. public void orangeButton(View view){
  304. DrawingArea.paintChangeOrange();
  305. Context context = getApplicationContext();
  306. CharSequence text = DrawingArea.paintColor + "Orange Button Pressed";
  307. int duration = Toast.LENGTH_SHORT;
  308.  
  309. Toast toast = Toast.makeText(context, text, duration);
  310. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  311. toast.show();
  312.  
  313. }
  314.  
  315. public void brownButton(View view){
  316. DrawingArea.paintChangeBrown();
  317. Context context = getApplicationContext();
  318. CharSequence text = DrawingArea.paintColor + "Brown Button Pressed";
  319. int duration = Toast.LENGTH_SHORT;
  320.  
  321. Toast toast = Toast.makeText(context, text, duration);
  322. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  323. toast.show();
  324.  
  325. }
  326.  
  327. public void pinkButton(View view){
  328. DrawingArea.paintChangePink();
  329. Context context = getApplicationContext();
  330. CharSequence text = DrawingArea.paintColor + "Pink Button Pressed";
  331. int duration = Toast.LENGTH_SHORT;
  332.  
  333. Toast toast = Toast.makeText(context, text, duration);
  334. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  335. toast.show();
  336.  
  337. }
  338.  
  339. public void yellowButton(View view){
  340. DrawingArea.paintChangeYellow();
  341. Context context = getApplicationContext();
  342. CharSequence text = DrawingArea.paintColor + "Yellow Button Pressed";
  343. int duration = Toast.LENGTH_SHORT;
  344.  
  345. Toast toast = Toast.makeText(context, text, duration);
  346. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  347. toast.show();
  348.  
  349. }
  350.  
  351. public void greyButton(View view){
  352. DrawingArea.paintChangeGrey();
  353. Context context = getApplicationContext();
  354. CharSequence text = DrawingArea.paintColor + "Grey Button Pressed";
  355. int duration = Toast.LENGTH_SHORT;
  356.  
  357. Toast toast = Toast.makeText(context, text, duration);
  358. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  359. toast.show();
  360.  
  361. }
  362.  
  363. public void darkGreenButton(View view){
  364. DrawingArea.paintChangeDarkGreen();
  365. Context context = getApplicationContext();
  366. CharSequence text = DrawingArea.paintColor + "Dark Green Button Pressed";
  367. int duration = Toast.LENGTH_SHORT;
  368.  
  369. Toast toast = Toast.makeText(context, text, duration);
  370. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  371. toast.show();
  372.  
  373. }
  374.  
  375. public void blackButton(View view){
  376. DrawingArea.paintChangeBlack();
  377. Context context = getApplicationContext();
  378. CharSequence text = DrawingArea.paintColor + "Black Button Pressed";
  379. int duration = Toast.LENGTH_SHORT;
  380.  
  381. Toast toast = Toast.makeText(context, text, duration);
  382. toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT, 0, 0);
  383. toast.show();
  384.  
  385. }
  386.  
  387. public void clearButton(View view){
  388. clearCanvas();
  389. }
  390.  
  391. public void aboutButton(View view) {
  392. setContentView(R.layout.activity_about);
  393. }
  394.  
  395. public void aboutReturnButton(View view) {
  396. setContentView(R.layout.activity_main);
  397. }
  398.  
  399. public void introContinueButton(View view) {
  400. setContentView(R.layout.activity_main);
  401. }
  402.  
  403.  
  404. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement