Why would the code below fail?

I have a blue relativeLayout in my main.xml. On click I'm adding a
green button.

When running on my Motorola Xoom I click the screen and I see the
green button show up, but the background changes from blue to black.
If I click again my blue background shows. Click again and I see
black...

What am I missing?

Thanks for any help.


        package com.android.mikeviewtester;

        import android.app.Activity;
        import android.graphics.Color;
        import android.os.Bundle;
        import android.view.MotionEvent;
        import android.view.View;
        import android.widget.Button;
        import android.widget.RelativeLayout;

        public class ViewTesterActivity extends Activity {

                @Override
                public void onCreate(Bundle savedInstanceState) {
                        super.onCreate( savedInstanceState );

                        // set the relative layout as our view
                        setContentView( R.layout.main );

                        RelativeLayout currentView = (RelativeLayout)
findViewById( R.id.MyRelativeLayout );

                        // set a listener
                        currentView.setOnTouchListener( 
(android.view.View.OnTouchListener)
mOnTouchListener );
                }

                private android.view.View.OnTouchListener mOnTouchListener = new
android.view.View.OnTouchListener() {
                        public boolean onTouch(View v, MotionEvent event) {

                                if ( v != null )
                                        v.onTouchEvent( event );

                                if ( event.getAction() == MotionEvent.ACTION_UP 
) {

                                        android.widget.RelativeLayout vw =
(android.widget.RelativeLayout) findViewById( R.id.MyRelativeLayout );

                                        // create and add a new cyan button
                                        Button btn = new Button( 
ViewTesterActivity.this );
                                        btn.setBackgroundColor( Color.GREEN );
                                        vw.addView( btn, 100, 100 );
                                        vw.invalidate();
                                        btn.invalidate();
                                }

                                return true;
                        }
                };
        }



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android";
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/MyRelativeLayout"
    android:background="#0000FF">
</RelativeLayout>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to