Change rectangle color by a button
<p>I'm trying to make simple app that displays rectangle and has ability to change its color by button.</p>

<p>Rectangle class is:</p>

<pre><code>public class DrawView extends View{
Paint paint = new Paint();


public DrawView(Context context) {
    super(context);
}
public DrawView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public void onDraw(Canvas canvas) {

    paint.setColor(Color.YELLOW);
    canvas.drawRect(300, 550, 150, 400, paint );

}

public void setColorRed()
{
   paint.setColor(Color.RED);
   invalidate();
}
</code></pre>

<p>My app is Tab Layout app. This class is displayed in 3rd Tab this way:</p>

<p>main.xml </p>

<pre><code>&lt;TabHost
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" &gt;

    &lt;LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" &gt;

        &lt;TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" &gt;
        &lt;/TabWidget&gt;

        &lt;FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" &gt;

            &lt;LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" &gt;

            &lt;/LinearLayout&gt;

            &lt;LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" &gt;

            &lt;/LinearLayout&gt;

            &lt;LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"  &gt;

&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" &gt;

    &lt;Button
    android:id="@+id/bRedColor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Red" /&gt;


   &lt;com.thms.systemy3.DrawView
        android:id="@+id/yourID"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"&gt;
    &lt;/com.thms.systemy3.DrawView&gt;
                &lt;/FrameLayout&gt;


            &lt;/LinearLayout&gt;
        &lt;/FrameLayout&gt;
    &lt;/LinearLayout&gt;
&lt;/TabHost&gt;
</code></pre>

<p></p>

<p>I was trying to get access to setColorRed() from my MainClass.java class by </p>

<pre><code>DrawView drawview;
</code></pre>

<p>and then use</p>

<pre><code>drawview.setColorRed()
</code></pre>

<p>MainClass.java:</p>

<pre><code>public class MainClass extends Activity implements OnClickListener{

TabHost th;
DrawView drawview;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    th = (TabHost) findViewById(R.id.tabhost);



    //tab3
    Button bColorRed = (Button) findViewById(R.id.bRedColor);

    th.setup();

    TabSpec specs = th.newTabSpec("tag1");
    specs.setContent(R.id.tab1);
    specs.setIndicator("TAB1");
    th.addTab(specs);

    specs = th.newTabSpec("tag2");
    specs.setContent(R.id.tab2);
    specs.setIndicator("TAB2");
    th.addTab(specs);

    specs = th.newTabSpec("tag3");
    specs.setContent(R.id.tab3);
    specs.setIndicator("TAB3");
    th.addTab(specs);


}



@Override
public void onClick(View v) {
    switch (v.getId()){
    case R.id.bRedColor:
        drawview.setColorRed();
        break;
    }
</code></pre>

<p>What am I doing wrong? Could anyone correct this code or give me a proper example of setting up simple app that draws rectangle and is able to change color by a button?</p>

<p>Thank you for reply. </p>
