Advertisement
Nepozor

Random coloring + getting color names back demo

Mar 21st, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | None | 0 0
  1. package hu.dani.teszt.examples.teszt;
  2.  
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.support.v7.app.ActionBarActivity;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.LinearLayout;
  9. import android.widget.RelativeLayout;
  10.  
  11. import java.util.Random;
  12.  
  13.  
  14. public class MainActivity extends ActionBarActivity {
  15.  
  16.     private String[] colorCodes={"#FFFFFF", "#000000", "#FFFF00", "#FF99FF", "#99FF00", "#FF3300", "#996600"};
  17.  
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22.  
  23.         final Random rand= new Random();
  24.         int number1;
  25.  
  26.         RelativeLayout rL1= (RelativeLayout) findViewById(R.id.rL);
  27.         rL1.setBackgroundColor(Color.parseColor(colorCodes[0]));
  28.  
  29.         /*number1= rand.nextInt(6)+1;
  30.         final LinearLayout lL1 = (LinearLayout) findViewById(R.id.lL1);
  31.         lL1.setBackgroundColor(Color.parseColor(colorCodes[number1]));
  32.  
  33.         number1= rand.nextInt(6)+1;
  34.         final LinearLayout lL2 = (LinearLayout) findViewById(R.id.lL2);
  35.         lL2.setBackgroundColor(Color.parseColor(colorCodes[number1]));
  36.  
  37.         number1= rand.nextInt(6)+1;
  38.         final LinearLayout lL3 = (LinearLayout) findViewById(R.id.lL3);
  39.         lL3.setBackgroundColor(Color.parseColor(colorCodes[number1]));
  40.  
  41.         Button btnNewRandom= (Button) findViewById(R.id.btnNewRandom);
  42.         btnNewRandom.setOnClickListener(new View.OnClickListener() {
  43.             @Override
  44.             public void onClick(View v) {
  45.                 int number2;
  46.                 number2= rand.nextInt(6)+1;
  47.                 lL1.setBackgroundColor(Color.parseColor(colorCodes[number2]));
  48.                 number2= rand.nextInt(6)+1;
  49.                 lL2.setBackgroundColor(Color.parseColor(colorCodes[number2]));
  50.                 number2= rand.nextInt(6)+1;
  51.                 lL3.setBackgroundColor(Color.parseColor(colorCodes[number2]));
  52.             }
  53.         });
  54.  
  55.         Button btnBlueee= (Button) findViewById(R.id.btnBlueee);
  56.         btnBlueee.setOnClickListener(new View.OnClickListener() {
  57.             @Override
  58.             public void onClick(View v) {
  59.                 lL1.setBackgroundColor(getResources().getColor(R.color.blue));
  60.                 lL2.setBackgroundColor(getResources().getColor(R.color.blue));
  61.                 lL3.setBackgroundColor(getResources().getColor(R.color.blue));
  62.             }
  63.         });*/
  64.  
  65.  
  66.  
  67.         final int[] colorArray = getResources().getIntArray(R.array.color_names);
  68.         int randomColorName = colorArray[rand.nextInt(colorArray.length)];
  69.  
  70. /*        String resName= getResources().getResourceEntryName(randomColorName);
  71.         TextView tvResName= (TextView) findViewById(R.id.textView);
  72.         tvResName.setText(resName);*/
  73.  
  74.         final LinearLayout lL4 = (LinearLayout) findViewById(R.id.lL4);
  75.         lL4.setBackgroundColor(randomColorName);
  76.         final LinearLayout lL5 = (LinearLayout) findViewById(R.id.lL5);
  77.         lL5.setBackgroundColor(randomColorName);
  78.         final LinearLayout lL6 = (LinearLayout) findViewById(R.id.lL6);
  79.         lL6.setBackgroundColor(randomColorName);
  80.  
  81.         Button btnNewRandom2= (Button) findViewById(R.id.btnNewRandom2);
  82.         btnNewRandom2.setOnClickListener(new View.OnClickListener() {
  83.             @Override
  84.             public void onClick(View v) {
  85.                 int randomColorName2;
  86.                 randomColorName2 = colorArray[rand.nextInt(colorArray.length)];
  87.                 lL4.setBackgroundColor(randomColorName2);
  88.                 randomColorName2 = colorArray[rand.nextInt(colorArray.length)];
  89.                 lL5.setBackgroundColor(randomColorName2);
  90.                 randomColorName2 = colorArray[rand.nextInt(colorArray.length)];
  91.                 lL6.setBackgroundColor(randomColorName2);
  92.             }
  93.         });
  94.  
  95.       lL4.setOnClickListener(new View.OnClickListener() {
  96.             @Override
  97.             public void onClick(View v) {
  98.                // Toast.makeText(MainActivity.this, lL4.getTag().toString(), Toast.LENGTH_SHORT).show();
  99.             }
  100.         });
  101.  
  102.         lL5.setOnClickListener(new View.OnClickListener() {
  103.             @Override
  104.             public void onClick(View v) {
  105.               //  Toast.makeText(MainActivity.this, lL5.getTag().toString(), Toast.LENGTH_SHORT).show();
  106.             }
  107.         });
  108.  
  109.         lL6.setOnClickListener(new View.OnClickListener() {
  110.             @Override
  111.             public void onClick(View v) {
  112.               //  Toast.makeText(MainActivity.this, lL6.getTag().toString(), Toast.LENGTH_SHORT).show();
  113.             }
  114.         });
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement