gui5711 gui5711 - 1 year ago 52
Java Question

How create a view inside activity?

My goal is to create a simple ball in the mobile screen center so I can move it around the screen as they drag my finger.

For that I wanted to use the Canvas and bitmap (I do not know if it's the best way seen as already said I am new in the Android world).

I created a function in my Activity so that when a button was clicked, a activity containing the script to the canvas design was created (Again not know if it's the best way. In a php script or javascript I would create the ball in the function itself). My code is as follows:

public void StartGame(View v) {
Intent i = new Intent(MainActivity.this, ball.class);

By doing this it should call the following activity:

com.teste package;
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    public class ball extends View {
        Paint pincelPreto private;
        Paint pincelAmerelo private;
        Public ball (Context context) {
            super (context);
        Public ball (Context context, attributeSet attrs) {
            super (context, attrs);
            setBackgroundColor (Color.LTGRAY);
            pincelPreto = new Paint ();
            pincelPreto.setColor (Color.BLACK);
            pincelAmerelo = new Paint ();
            pincelPreto.setColor (Color.YELLOW);
            setFocusable (true);
        protected void onDraw (Canvas canvas) {
            super.onDraw (canvas);
            canvas.drawCircle (200, 200, 200, pincelAmerelo);

And both draw a ball at every location ...
But I get the following error:

Android.content.ActivityNotFoundException: Unable to find explicit activity class {com.teste / com.teste.ball}; have you declared this activity in your AndroidManifest.xml?

What am I doing wrong?

OBS.: I maybe call activity because i dont know the real name of it.

Answer Source

Expanding on Neo's answer above and given your edited question:

The reason you are getting the Android.content.ActivityNotFoundException is because "ball" is a View, not an Activity, and so you cannot start "ball" as an activity.

public void StartGame(View v)  {
    Intent i = new Intent(MainActivity.this, ball.class);

Since you are new to Android, try to visualize an Activity as a component that usually takes over the whole screen, and a View as a smaller piece of that. You want your ball to display within your activity.

In your res/layout folder you should have a layout xml file for your MainActivity. You can add your ball view in here with some xml, similar to the following:

<?xml version="1.0" encoding="utf-8"?>


You may hit other problems once you've gotten that far, but it should be easier to debug then.

I agree with the comment above to learn the basics first before jumping in to a complicated application. Take a look at the Android Developer pages to learn the components and trying simple tutorials. There are a lot of components that make up an Android application and fully understanding them and how they interact with each other will make your life easier :). Good luck and welcome to Android!

Edit to answer question below ...can i do it with java? i searched something about "inflate" a view but i dont know if it is correct?

Yes you can add a view to the Activity's parent layout using addView. Inflate is for instantiating views in your XML into Java objects, so it will not work in your case because you don't have an xml file for your Ball layout. You would need to create a new Ball object and use addView to add it to your layout:

LinearLayout layout = findViewById(;

Ball ball = new Ball(this);
ball.setLayoutParams(new LinearLayout.LayoutParams(


where layout is the parent layout found in your activity's XML. You may need to assign it an ID if you haven't already, so if we use my example above, your LinearLayout tag would look like:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download