views:

22

answers:

1

Hello all,

is it possible to start a context menu on the onCreate method? I know its probably bad design ethics but I have my resons!! I've tried the:

registerForContextMenu(this.getCurrentFocus());

But its not working.. So does anyone have any better ideas?

Many thanks in advance!

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout layout = new LinearLayout(this);
    layout.setLayoutParams(new
    LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    //Button button = new Button(this);
    //button.setLayoutParams(new
    //LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    //button.setText("my button");

    TextView text = new TextView(this);
    text.setLayoutParams(new
    LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    layout.addView(text);
    setContentView(layout);

    registerForContextMenu(text);
    openContextMenu(layout);
A: 

You need to do registerForContextMenu() for some widget, then use openContextMenu(). However, I agree with your conclusion that this is a bad design.

CommonsWare
Mark, I've added the code im trying to implement in the question above, however the context menu still refuses to open. The codes works perfectly if I attach the contextmenu to a button click event! Cheers
Ally
@Ally: the parameter to `registerForContextMenu()` needs to be the same as the parameter to `openContextMenu()`.
CommonsWare
hmmm i've tried registerForContextMenu(layout) openContextMenu(layout) and registerForContextMenu(text) openContextMenu(text) to no avail its just crashing my app... LogCat tells me Activity idle timeout! hmmm
Ally
@Ally: Have you considered perhaps doing something else, instead of opening a context menu in `onCreate()`? It is a sub-par UI and it apparently is not possible.
CommonsWare