0

When I try to create my GameView, I get an error "function call expected".

in my Main activity:

GameView gv = GameView(this, null);

public class GameView extends View {
        private Drawable mCustomImage;

        public GameView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mCustomImage = context.getResources().getDrawable(R.drawable.bg);
        }

        protected void onDraw(Canvas canvas) {
            Rect imageBounds = canvas.getClipBounds();  // Adjust this for where you want it

            mCustomImage.setBounds(imageBounds);
            mCustomImage.draw(canvas);
        }
    }
}

How do I create this dynamically?

I'm trying to add a view to my activity. The GameView will then control the position of its subviews programmatically.

1
  • 2
    GameView gv = new GameView(this, null); - You forgot new. And that has to be inside a class method, if it's not already. Commented Nov 1, 2017 at 3:14

3 Answers 3

1

This will make scene

I made some new changes in your GameView class. Below is copy of new GameView class.

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

public class GameView extends View {

private Context mContext;

private Drawable mCustomImage;


public GameView(Context context) {
    super(context);
    mContext = context;
    mCustomImage = ContextCompat.getDrawable(context, R.mipmap.ic_launcher);
}

public GameView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    mCustomImage = ContextCompat.getDrawable(context, R.mipmap.ic_launcher);
}

public GameView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mContext = context;
    mCustomImage = ContextCompat.getDrawable(context, R.mipmap.ic_launcher);
}

public GameView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    mContext = context;
    mCustomImage = ContextCompat.getDrawable(context, R.mipmap.ic_launcher);
}

@Override
protected void onDraw(Canvas canvas) {
    Rect imageBounds = canvas.getClipBounds();
    mCustomImage.setBounds(imageBounds);
    mCustomImage.draw(canvas);
}
}

Adding it through xml inside activity.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <GameView
            android:layout_width="50dp"
            android:layout_height="50dp" />

    </LinearLayout>

</ScrollView>

Output

Image_One

Adding it through code in activity

public class CustomActivity extends AppCompatActivity {

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


    initialiseView();

}

private void initialiseView() {

    LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);

    GameView mGameView = new GameView(this);

    mLinearLayout.addView(mGameView);

}
}

Output

Image_Two

EDIT

If you want to set parameters while adding it pragmatically then initialiseView method will be like

private void initialiseView() {

    LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);

    GameView mGameView = new GameView(this);

    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(50, 50);

    mGameView.setLayoutParams(layoutParams);

    mLinearLayout.addView(mGameView);

}

Output :

Image_Three

Sign up to request clarification or add additional context in comments.

3 Comments

In the onCreate, if this is my MainActivity then it should be ` setContentView(R.layout.activity_main);` ? You mention "draw line" but I didn't see that anywhere else
I try the layout, get error "Element GameView is not allowed here"
Hello Abhishek, so your activity was named "ActivityDrawLine"? I removed that line from XML and now I can draw!
0

GameView is a class, not a method, so you must call new to use them,

try this:

GameView gv = new GameView(this, null);

public class GameView extends View {
        private Drawable mCustomImage;

        public GameView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mCustomImage = context.getResources().getDrawable(R.drawable.bg);
        }
         ....

Comments

0

Use new GameView(this, null); inside onCreate of your activity to create a new instance of your view.

Then use the method setContentView(View) to use it in your activity.

public class MainActivity extends AppCompatActivity{

    private GameView gameView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gameView = new GameView(this, null);
        setContentView(gameView);
        //other code
    }
    //other methods
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.