SHARE
TWEET

merubah background alv

alvinputrau Jan 28th, 2016 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.alv.app.tugas_2;
  2.  
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.support.design.widget.FloatingActionButton;
  6. import android.support.design.widget.Snackbar;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.support.v7.widget.Toolbar;
  9. import android.view.View;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.widget.Button;
  13. import android.widget.LinearLayout;
  14. import android.widget.TextView;
  15.  
  16. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  17.  
  18. //JANGAN LUPA UNTUK MENDEKLARASIKAN VARIABEL YANG INGIN DIPAKAI
  19. Button btnRed,btnBlue,btnGreen,btnCyan,btnMagenta,btnYellow;
  20. LinearLayout backgroundLayout;
  21. TextView Tred,Tgreen,Tblue;
  22.  
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. //PROSEDUR INI DIBUAT UNTUK MEMANGGIL FUNGSI,PROSEDUR,DLL YANG TELAH ANDA BUAT
  26.  
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.tugas_2);
  29.  
  30. initialize();
  31. }
  32. private void initialize() { //FUNGSI INI DIBUAT AGAR DAPAT DIPANGGIL SAAT ANDROID DIBUKA
  33.  
  34. //AGAR VARIABEL DAPAT DIPAKAI KITA PERLU ASSIGN ID DALAM LAYOUT KE VARIABEL DALAM JAVA
  35. btnRed = (Button) findViewById(R.id.red);
  36. btnGreen = (Button) findViewById(R.id.green);
  37. btnBlue = (Button) findViewById(R.id.blue);
  38. btnCyan = (Button) findViewById(R.id.cyan);
  39. btnMagenta = (Button) findViewById(R.id.magenta);
  40. btnYellow = (Button) findViewById(R.id.yellow);
  41. backgroundLayout = (LinearLayout) findViewById(R.id.background);
  42. btnRed.setOnClickListener(this);
  43. btnGreen.setOnClickListener(this);
  44. btnBlue.setOnClickListener(this);
  45. btnCyan.setOnClickListener(this);
  46. btnMagenta.setOnClickListener(this);
  47. btnYellow.setOnClickListener(this);
  48. }
  49.  
  50. @Override
  51. public void onClick(View v) {
  52.  
  53. //CODINGAN UNTUK MELIHAT BUTTON MANA YANG DI KLIK
  54. switch(v.getId()){
  55. case R.id.red: //MASUKKAN TIAP BUTTON DALAM CASE
  56. backgroundLayout.setBackgroundColor(Color.parseColor("#ff0000"));
  57. break;
  58. case R.id.green:
  59. backgroundLayout.setBackgroundColor(Color.parseColor("#00ff00"));
  60. break;
  61. case R.id.blue:
  62. backgroundLayout.setBackgroundColor(Color.parseColor("#0000ff"));
  63. break;
  64. case R.id.cyan:
  65. backgroundLayout.setBackgroundColor(Color.parseColor("#00ffff"));
  66. break;
  67. case R.id.magenta:
  68. backgroundLayout.setBackgroundColor(Color.parseColor("#ff00ff"));
  69. break;
  70. case R.id.yellow:
  71. backgroundLayout.setBackgroundColor(Color.parseColor("#ffff00"));
  72. break;
  73. }
  74. }
  75. @Override
  76. public boolean onCreateOptionsMenu(Menu menu) {
  77. // Inflate the menu; this adds items to the action bar if it is present.
  78. getMenuInflater().inflate(R.menu.menu_main, menu);
  79. return true;
  80. }
  81.  
  82. @Override
  83. public boolean onOptionsItemSelected(MenuItem item) {
  84. // Handle action bar item clicks here. The action bar will
  85. // automatically handle clicks on the Home/Up button, so long
  86. // as you specify a parent activity in AndroidManifest.xml.
  87. int id = item.getItemId();
  88.  
  89. //noinspection SimplifiableIfStatement
  90. if (id == R.id.action_settings) {
  91. return true;
  92. }
  93.  
  94. return super.onOptionsItemSelected(item);
  95. }
  96. }
RAW Paste Data
Pastebin PRO Summer Special!
Get 60% OFF on Pastebin PRO accounts!
Top