quantumpotato
quantumpotato

Reputation: 9767

Create a custom view programatically in Android

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.

Upvotes: 0

Views: 108

Answers (3)

Abhishek
Abhishek

Reputation: 3398

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

Upvotes: 1

Nabin Bhandari
Nabin Bhandari

Reputation: 16379

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
}

Upvotes: 0

HassanUsman
HassanUsman

Reputation: 1973

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);
        }
         ....

Upvotes: 0

Related Questions