Search code examples
androidandroid-canvasandroid-custom-view

Access object of one custom view to another Customview, created on Activty class


My actual problem: I have an array in CustomView1 class. And I want to access it in CustomView2 class. And when it will fill, I have to call view2.invalidate().

This is my activity class:

public class TestApp extends Activity {

        CustomView1 view1;

        CustomView2 view2;

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

            view1 = (CustomView1) findViewbyId(R.id.CustomViewID1);
            view2 = (CustomView2) findViewbyID(R.id.CustomViewID2);

        }
    }

This is my first CustomView class . Here I want to call view2.invalidate().

public class CustomView1 extends View {

    byte[] bytearray = new byte[200];

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

    @override
    onDraw() {
        view2.invalidate();
    }

}

And this is my second CustomView class. Here I want to access the same object of CustomView1(i.e view1 object ) class that I have created in Activity class.

public class CustomView2 extends View {

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

Is it possible to access like this? Any other idea how to do that?


Solution

  • For this to work you need to have a reference of those views inside your custom views classes. Something like this in CustomView1 (same logic is applied for CustomView2):

    public class CustomView1 extends View
    {
    
        byte[] bytearray  = new byte[200];
        private CustomView2 view2;
    
        public CustomView1(Context context, AttributeSet attrs) 
        {
           super(context, attrs);  
        }
    
        public void setCustomView2(CustomView2 view2) 
        {
           this.view2 = view2;
        }
    
        @override 
        onDraw()
        {
           if(view2 != null) //If view2 is not already set, we do not want a null pointer exception to occur
           {
              view2.invalidate();
           }
        }
    
    }
    

    To use it, you need something like this:

    public class TestApp extends Activity  
    { 
    
       CustomView1  view1 ;
       CustomView2  view2 ;
    
    
       @Override
       public void onCreate(Bundle savedInstanceState)
       {
           super.onCreate(savedInstanceState); 
           setContentView(R.layout.main); 
    
           view1   = (CustomView1) findViewbyId(R.id.CustomViewID1);
           view2   = (CustomView2) findViewbyID(R.id.CustomViewID2);
    
           view1.setCustomView2(view2);
           view2.setCustomView1(view1);
    
       }  
    }
    

    EDIT: You can create another custom constructor for your CustomView1 class in addition to the one you already have. Something like this:

    public CustomView1(Context context) 
    {
         super(context);  
    }
    

    This way you avoid passing a null AttributeSet to your view and you call your custom view like this: CustomView1 view1 = new CustomView1(context).

    The public CustomView1(Context context, AttributeSet attrs) constructor is used to handle the situation that you declare you custom view through a layout xml file and not programmatically. You will need to set the layout properties of your view programmatically that way. Check this for help in doing so.