I am going nuts over this.

I did not find any working solution (tried a few from stackoverflow)

Scenario (this is an actual screenshot what is already done):

![enter image description here](https://i.stack.imgur.com/D7135.png)

I have a Activity that has a View as his Attribute.

This view adds another view via View.addView(myView).

I now want to add a Button to myView (to be specific: after MotionEvent.ACTION\_UP the button should appear in the right lower corner (this will start the robot to drive the track))

Here is a shortcut of my code:

```
public class ModeRouting extends View {
public ModeRouting(Context context) {
    super(context);
Button asuroStartButton = new Button(context) //does not work
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int actionevent = event.getAction();
    if (actionevent == MotionEvent.ACTION_UP
            || actionevent == MotionEvent.ACTION_CANCEL) {
asuroStartButton.visible=true;
view.add(asuroStartButton);
    }
    return true;
}
}

```

and my Activity:

```
//in constructor
contentView = (FrameLayout) findViewById(R.id.content);
onClickListenerFacade(routingMode, route);

//this removes all views from stack and places the new one on the view
private void onClickListenerFacade(View v, final View target) {
    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            contentView.removeAllViews();
            contentView.setBackgroundColor(0xff000000);
            contentView.addView(target);
            modeSelectorAnimation();
        }
    });
}

```

I tried to create a button in my mainactivity.xml and instantiate in my mainactivity.

I am missing some point in here but i am not sure which.

Since my view is purely dynamic (no layout.xml) i dont think i should use a layout.xml (maybe thats my mind-blockage) but instead set the button attributes dynamically too.

Any hint is appreciated!