Shavarsh Movsesyan
Shavarsh Movsesyan

Reputation: 21

How to pass an activity instance to a custom view?

let me start by telling you that I am new to android development. I have a MainActivity class, xml layout for it and a custom view class used for drawing. My problem is that I need to pass the instance of the MainActivity class to the custom view class. I tried with Bundle and found out it can't transfer instances. I can access an individual variable (_grid in this case) and operate with it in the view class by making it static, but in this case this isn't an option. I have read about and tried with getContext() but with no result. I found out that I also can't override the custom view constructors. So what do I need to do in order to pass the MainActivity instance to the custom view class?

MainActivity class:

    package com.example.myfirstapp;

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

    public class MainActivity extends Activity 
    {
        boolean isPlaying = false;
        static int[][] _grid = new int[10][13];


        //Fill grid with zeros (just in case)
        public void initialiseGrid()
        {
            for(int i = 0; i < 10; i++)
            {
                for(int j = 0; j < 13; j++)
                {
                    _grid[i][j] = 0;
                }
            }
        }

       //Note addition
       public void addToGrid(int x, int y)
       {
           _grid[x][y] = 1;     
       }

       //Note removal
       public void removeFromGrid(int x, int y)
       {
           _grid[x][y] = 0;
       }

           @Override
           protected void onCreate(Bundle savedInstanceState) 
           {
                   super.onCreate(savedInstanceState);
                   setContentView(R.layout.activity_main);
                   initialiseGrid();
           }

           //Play button functionality
       public void playButton (View v) 
       {        
           //button logic
       }

       //Stop button functionality
       public void stopButton (View v)
       {
           //button logic

       }    
    }    

It's layout xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >

<com.example.myfirstapp.showImage
    android:id="@+id/my_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" /> 

<Button
   ... />

<Button
   ... />

</RelativeLayout>

And the custom view class:

package com.example.myfirstapp;

//imports

class showImage extends View 
{       
    Vector<Pair<Integer, Integer>> _notePositions = new Vector<Pair<Integer, Integer>>();
    Canvas _canvas;
    Paint _paint = new Paint();
    MainActivity _main;

    Bitmap _score;
    Bitmap _note = BitmapFactory.decodeResource(getResources(), R.drawable.note);
    Bitmap _tempNote = BitmapFactory.decodeResource(getResources(), R.drawable.note);

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

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

    @Override
    public void onDraw (Canvas canvas) 
    {       
        //irrelevant code
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) 
    {
        //irrelevant code
    }   
}

Upvotes: 1

Views: 1544

Answers (2)

Shavarsh Movsesyan
Shavarsh Movsesyan

Reputation: 21

Alright I managed to make a what I will call a work around since I am using a static function.

In my MainActivity class I have:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialiseGrid();
    showImage.getIstance(this); //this is the new line
}

And in my showImage class I have:

static MainActivity _main;

.
.
.
public static void getIstance(MainActivity mainActivity) 
{
    _main = mainActivity;       
}

In my case I won't have any other instances so this works perfectly.

Upvotes: 1

A.R.
A.R.

Reputation: 2641

    You can pass the context of your MainActivity class to your Custom view class.

//  In Main Activity 

    public class MainActivity extends Activity
    {
      onCreate(Bundle b)
      {

        // Create Instance of your view class & pass context of your MainActivity class
          ShowImage showImage=new ShowImage(MainActivity.this);


      }

// In Custom View class

   public class ShowImage extends View
   {
      Context context;
     // Create constructor ShowImage(with argument Context)

      public ShowImage(Context context)
     {
       this.context=context;
     }

   }

Upvotes: 0

Related Questions