title: Change background color on button click?
body: <p>I want the background color of my second activity to change once a button is pressed. This is my_activity2.xml:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MyActivity2"
  android:background="#ffdb4b5e"&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Next"
      android:id="@+id/button"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:textColor="#ffffffff"
      android:textSize="72sp"
      android:background="#00000000"
      /&gt;
  
  &lt;TextView
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:text="@string/thing1"
      android:id="@+id/textView"
      android:textColor="#ffffffff"
      android:textSize="36sp"
      android:gravity="center"
      android:layout_above="@+id/button"
      /&gt;
  </code></pre>
  
  <p></p>
  
  <p>This is my MyActivity2.java:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_my_activity2);
      Button n = (Button) findViewById(R.id.button);
      Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
      n.setTypeface(typeface);
      final TextView tv = (TextView) findViewById(R.id.textView);
      Typeface face = Typeface.createFromAsset(getAssets(),
              "OSP-DIN.ttf");
      tv.setTypeface(face);
  
   final String[] values = getResources().getStringArray(R.array.things_array);
  n.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Random RAND=new Random();
          String nextValue = values[RAND.nextInt(values.length)];
          tv.setText(nextValue);
  </code></pre>
  
  <p>I have the background colors stored in the strings.xml like so:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;string-array name="colorcode_array"&gt;
      &lt;item&gt;3498db&lt;/item&gt;
      &lt;item&gt;2ecc71&lt;/item&gt;
      &lt;item&gt;9b59b6&lt;/item&gt;
      &lt;item&gt;f1c40f&lt;/item&gt;
      &lt;item&gt;1abc9c&lt;/item&gt;
      &lt;item&gt;2980b9&lt;/item&gt;
      &lt;item&gt;8e44ad&lt;/item&gt;
      &lt;item&gt;e41c1c&lt;/item&gt;
      &lt;item&gt;2ecca9&lt;/item&gt;
      &lt;item&gt;752ecc&lt;/item&gt;
      &lt;item&gt;4f2ecc&lt;/item&gt;
      &lt;item&gt;2eccc3&lt;/item&gt;
      &lt;item&gt;2ecc53&lt;/item&gt;
      &lt;item&gt;2ecc2e&lt;/item&gt;
      &lt;item&gt;5bcc2e&lt;/item&gt;
      &lt;item&gt;9ecc2e&lt;/item&gt;
      &lt;item&gt;cca12e&lt;/item&gt;
      &lt;item&gt;cc712e&lt;/item&gt;
      &lt;item&gt;f1c209&lt;/item&gt;
      &lt;item&gt;86f109&lt;/item&gt;
      &lt;item&gt;f11616&lt;/item&gt;
      &lt;item&gt;9c1818&lt;/item&gt;
  &lt;/string-array&gt;
  </code></pre>
  
  <p></p>
  
  <p>Now how can I make the background color change on random when the button "Next" is clicked? Matthew </p>
  
  <p>EDIT:</p>
  
  <pre><code>    @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_my_activity2);
      final RelativeLayout layout = (RelativeLayout) findViewById(R.id.my_relative_layout);
      Button n = (Button) findViewById(R.id.button);
      Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
      n.setTypeface(typeface);
      final TextView tv = (TextView) findViewById(R.id.textView);
      Typeface face = Typeface.createFromAsset(getAssets(),
              "OSP-DIN.ttf");
      tv.setTypeface(face);
  
      final String[] values = getResources().getStringArray(R.array.things_array);
      final String[] value = getResources().getStringArray(R.array.colorcode_array);
  n.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Random RAND=new Random();
          String nextValue = values[RAND.nextInt(values.length)];
          String newValue = value[index++];
          tv.setText(nextValue);
          layout.setBackgroundColor(Color.parseColor(newValue));
      }
  });
  </code></pre>
  
  <p>}</p>