title: How to change a view from a different activity.
body: <p>Right now my goal is to have the Colors activity allow users to hit a button to change the color they are drawing with. How do I get the instance of the canvas in my activity_main.xml to my Colors activity so I can change the color that is drawn on it?</p>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &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"
      android:background="#09B8C1"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="com.app.MainActivity"&gt;
  
      &lt;com.app.color.DrawingView
          android:layout_width="match_parent"
          android:layout_height="200dp"
          android:id="@+id/drawing"
          android:layout_marginLeft="5dp"
          android:layout_marginRight="5dp"
          android:background="#FFFFFFFF"
          android:layout_above="@+id/settings"
          android:layout_alignParentTop="true" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:onClick="changeColor"
          android:text="@string/Settings"
          android:id="@+id/settings"
          android:layout_alignParentBottom="true"
          android:layout_alignParentRight="true"
          android:layout_alignParentEnd="true" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Clear"
          android:id="@+id/button5"
          android:layout_alignParentBottom="true"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:onClick="clearScreen" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>MainActivity.java</p>
  
  <pre><code>package com.app.color;
  
  import android.content.Intent;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  
  public class MainActivity extends AppCompatActivity {
  
      DrawingView dv = new DrawingView();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      public void changeColor(View view)
      {
          Intent intent = new Intent(MainActivity.this, Colors.class);
          startActivity(intent);
      }
  
      public void clearScreen(View view)
      {
  
      }
  
  }
  </code></pre>
  
  <p>Colors.java </p>
  
  <pre><code>package com.app.color;
  
  import android.content.Intent;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  
  public class Colors extends AppCompatActivity {
  
      DrawingView dv = new DrawingView(,);
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_colors);
      }
  
      public void toWhite(View view)
      {
          dv.setColor("#ffffff");
          Intent intent = new Intent(Colors.this, MainActivity.class);
          startActivity(intent);
      }
  
      public void toYellow(View view)
      {
          dv.setColor("#ffff00");
          Intent intent = new Intent(Colors.this, MainActivity.class);
          startActivity(intent);
      }
  
      public void toBlue(View view)
      {
          dv.setColor("#0000ff");
          Intent intent = new Intent(Colors.this, MainActivity.class);
          startActivity(intent);
      }
  
      public void toRed(View view)
      {
          dv.setColor("#ff0000");
          Intent intent = new Intent(Colors.this, MainActivity.class);
          startActivity(intent);
      }
  
      public void toGreen(View view)
      {
          dv.setColor("#00ff00");
          Intent intent = new Intent(Colors.this, MainActivity.class);
          startActivity(intent);
      }
  
      public void toBlack(View view)
      {
          dv.setColor("#00000");
          Intent intent = new Intent(Colors.this, MainActivity.class);
          startActivity(intent);
      }
  }
  </code></pre>
  
  <p>DrawingView.java</p>
  
  <pre><code>package com.app.color;
  
  import android.content.Context;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.graphics.Path;
  import android.util.AttributeSet;
  import android.view.MotionEvent;
  import android.view.View;
  
  public class DrawingView extends View {
      private Paint paint = new Paint();
      private Path path = new Path();
  
      public DrawingView(Context context, AttributeSet attrs) {
          super(context, attrs);
  
          paint.setAntiAlias(true);
          paint.setStrokeWidth(5f);
          paint.setColor(Color.BLACK);
          paint.setStyle(Paint.Style.STROKE);
          paint.setStrokeJoin(Paint.Join.ROUND);
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          canvas.drawPath(path, paint);
      }
  
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          // Get the coordinates of the touch event.
          float eventX = event.getX();
          float eventY = event.getY();
  
          switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                  // Set a new starting point
                  path.moveTo(eventX, eventY);
                  return true;
              case MotionEvent.ACTION_MOVE:
                  // Connect the points
                  path.lineTo(eventX, eventY);
                  break;
              default:
                  return false;
          }
  
          // Makes our view repaint and call onDraw
          invalidate();
          return true;
      }
  
      public void setColor(String newColor){
          invalidate();
          int paintColor = Color.parseColor(newColor);
          paint.setColor(paintColor);
      }
  }
  </code></pre>
  
  <p>activity_colors.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &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"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="com.app.Colors"&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:text="Choose Color"
          android:id="@+id/textView"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/button"
          android:layout_below="@+id/textView"
          android:onClick="toWhite"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_alignParentRight="true"
          android:layout_alignParentEnd="true"
          android:background="#ffffff"
          android:text="@string/white" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/button2"
          android:layout_below="@+id/button"
          android:onClick="toBlue"
          android:layout_alignRight="@+id/button"
          android:layout_alignEnd="@+id/button"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:background="#153fc7"
          android:text="@string/blue" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/red"
          android:id="@+id/button3"
          android:layout_below="@+id/button2"
          android:onClick="toRed"
          android:layout_alignRight="@+id/button2"
          android:layout_alignEnd="@+id/button2"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:background="#f01010" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/yellow"
          android:id="@+id/button4"
          android:background="#f7e80f"
          android:layout_below="@+id/button3"
          android:onClick="toYellow"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_alignRight="@+id/button3"
          android:layout_alignEnd="@+id/button3" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/Green"
          android:id="@+id/button6"
          android:layout_below="@+id/button4"
          android:onClick="toGreen"
          android:layout_alignRight="@+id/button4"
          android:layout_alignEnd="@+id/button4"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:background="#00ff00" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/Black"
          android:id="@+id/button7"
          android:layout_below="@+id/button6"
          android:background="#000000"
          android:onClick="toBlack"
          android:textColor="#ffffff"
          android:layout_alignParentRight="true"
          android:layout_alignParentEnd="true"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>