0

How to send data from activity class to custom view class I have written following program? I am using this program for selecting the color and then shape using OnTouchListener. Please, could you give me more detail how I can complete this task. Thank you in advance
Activity class

        package com.easyway2win;

        import android.app.Activity;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.View;
        import android.widget.Button;



        public class MainActivity extends Activity {

            private Button butnPink;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                butnPink = (Button)findViewById(R.id.pinkColor);

                super.onCreate(savedInstanceState);
                /*      MyViewActivity mv = new MyViewActivity(this);*/
                setContentView(R.layout.activity_main);


            }

            public void clickMe(View view){
                int intColor = 0;
                String hexColor = null;
                switch(view.getId()){

                case R.id.pinkColor:
                    intColor =  getResources().getColor(R.color.pink);
                    //  hexColor = String.format("#%06X", (0xFFFFFF & intColor));
                    //  Log.d("Hi", "I am pink color code " + hexColor);
                    break;
                case R.id.blueColor:
                    intColor =  getResources().getColor(R.color.blue);
                    //  hexColor = String.format("#%06X", (0xFFFFFF & intColor));
                    // Log.d("Hi", "I am Blue color " + hexColor);
                    break;
                case R.id.squaretool:
                    Log.i("Hi","I am square");
                }

                hexColor = String.format("#%06X", (0xFFFFFF & intColor));
                Log.d("Hi", "I am color code " + hexColor );

            }// end [ clickMe method ]

            PaintView paintView = new PaintView();

        }

Custom View Class

    package com.easyway2win;

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.view.View;

    public class PaintView extends View {
          private Paint paint;


          public PaintView(Context context) {
                super(context);
            //context.getResources().getColor(Color.parseColor()) // I don't know how to do it coding

    }


        public PaintView(Context context, AttributeSet attrs ){
            super(context, attrs);
         }

        public PaintView(Context context, AttributeSet attrs, int defStyle/*Context context, Rect rectangle, Paint paint*/) {
            super(context,attrs,defStyle);
        //  this.rectangle = rectangle;
        //  this.paint = paint;
        }

       public void setColour(String colour){
           // need code here       
       }
        @Override
        protected void onDraw(Canvas canvas) {
        /*  canvas.drawRect(rectangle,paint);*/

        }




    }
Jaymesh
  • 11
  • 4

1 Answers1

0

There are multiple ways to achieve your goal. But creating a class with global variables to hold your color and call it when necessary is a good approach for your scenario. Check this question for more info on how to create and access global variables from a class or an activity.

Community
  • 1
  • 1