0

If I want to add a button in this class so that I can call the onclicklistener, how should I do it?i have also provided the activity class onto which i am adding this view.

activity:

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;

import android.content.Context;



public class NewGame extends Activity {

View view;
Context context;
RelativeLayout layout;
GameView gameview;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    gameview=new GameView(this);
    setContentView(gameview);
    //layout = (RelativeLayout) findViewById(R.id.relative_layout);



    //layout.addView(gameview);

}


}

view: public class GameView extends View { Path circle; Paint cPaint; Paint tPaint; String z; int i = 65, strt, arc, leftx, topy, rightx, bottomy, maxx, maxy; boolean flag1, flag2, flag3; double n1, n2; int n, n3 = 180,n4,n5 = 90; float f1 = 180, f2 = 90; Button b1; Random r = new Random(); RectF oval;

    public GameView(Context context) {
        super(context);

        leftx = 0;
        topy = 60;
        rightx = 150;
        bottomy = 120;

        z = String.valueOf(Character.toChars(i));

        cPaint = new Paint();
        cPaint.setColor(Color.RED);

        strt = 45;
        arc = 315;

        n1 = Math.random() * 600;
        Log.d("random", z);

        if (flag2 == false)
            new DrawThread(this);

        // cPaint.setStrokeWidth(2);
        tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        tPaint.setColor(Color.BLACK);
        float scale = getResources().getDisplayMetrics().scaledDensity;
        tPaint.setTextSize(20 * scale);
    }

    public void onSizeChanged(int w,int h,int oldh,int oldw) {
        maxx = oldw;
        maxy = oldh;
    }

    //@Override
    protected void onDraw(Canvas canvas) {
        // Drawing commands go here
        oval = new RectF(leftx,topy,rightx,bottomy);
        canvas.drawArc(oval, strt, arc, true, cPaint);
        while (i < 90)  {
            canvas.drawText(String.valueOf(Character.toChars(i)),f1,f2, tPaint);
            break;
        }
    }
}

2 Answers 2

1

You can do it like this:

Button bt = new Button(this);
bt.setText("A Button");
bt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
linerLayout.addView(bt);

And then you can do this

bt.setOnClickListener(new View.OnClickListener() {

//TO DO
}

I hope that this can help you.

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

Comments

0

first of all in order to allow to your custom view to have an addView(View v) method it must extend ViewGroup instead of View; then you can use this code

b1=new Button(context);
b1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT ));
b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
        this.addView(b1);

30 Comments

after doing what you mentioned above i am now getting a blank screen.what do you think can be the problem ?
try to set an id for the button b1.setId(1234)
still getting a blank screen
try adding text with b1.setText("your text") and using LayoutParams.FILL_PARENT instead of LayoutParams.WRAP_CONTENT. what if you don't put in your custom view the code for the button? how are you adding your custom view to the layout? in xml or by code?
sir i have also provided the activity class above.
|

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.