2

well the problem is that i am using in my activity a kind of a SurfaceView, and i want to add buttons in it.

my questions are:

1) how do i create an instance of a button without calling findViewById(...)? (cause i dont have layout because of the surfaceView)...

2) and how do i draw this button on a canvas?

or maybe you are suggesting doing something else?

all i care is that there will be button on my screen and i can implement something like OnClickListener(...)....

thanks to all in advance !!!

Leon
  • 55
  • 1
  • 8

2 Answers2

12

add as a Button to Activity with setOnClickListener without xml:

@Override  
        protected void onCreate(Bundle savedInstanceState) {  
            // TODO Auto-generated method stub  
            super.onCreate(savedInstanceState);  

            Button button= new Button (this);  
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(  
            FrameLayout.LayoutParams.WRAP_CONTENT,  
            FrameLayout.LayoutParams.WRAP_CONTENT);   
            params.topMargin = 0;  
            params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;  

            button.setText("dynamic Button");  
            addContentView(button, params);  
            // setContentView(tv);  
            button.setOnClickListener(new Button.OnClickListener(){  
            @Override  
            public void onClick(View v) {  

            }  

        });  
  }  
Lore
  • 1,286
  • 1
  • 22
  • 57
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • tnx alot i am new in the android world and it really helps to understand some consepts better so thank you! but i will use fdreger answer because it suits my code more – Leon Jul 09 '12 at 20:49
  • This is better the answer – Diego Dec 26 '19 at 04:33
2

If you draw your button on the canvas (which is possible), it will not be clickable. What you really want is:

  • wrap your SurfaceView into a frame layout - if you also add other views to the same layout, they will appear above the SurfaceView;
  • add a relative layout to the frame layout mentioned above (so you can position the button and possibly other views - if you only have the button, you might get away with just setting the margins).

Like so:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FrameLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/restartButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:onClick="whatever"
            android:text="look, I float above the SurfaceView!" />

    </RelativeLayout>

</FrameLayout>
fdreger
  • 12,264
  • 1
  • 36
  • 42
  • i didn't know i can add a surfaceView in xml layout that will be very usefull for me thank you alot!! – Leon Jul 09 '12 at 20:57