Search code examples
javaandroidonclicklistenerpopupmenuoptionmenu

How to use onClickListener and onMenuSelectListener in mainactivity?


Hello im trying to use onclicklistener along with onmenuselectlistener in my mainactivity class and the program runs but selecting the popup menu opions dont work they wont set the text to want it want anyone have any idea's? I know i did not implement onMenuSelectListener and maybe thats my problem if it is is there any other way to makes this work?

Here is my code:

public class MainActivity extends Activity implements OnClickListener {
// init variables
Handler uiHandler;
EditText cl;
TextView info;
Button enter;
Button line;
Button arc;
DrawingUtils callDU = new DrawingUtils();
DrawingTools callDT = new DrawingTools();
EditTools callET = new EditTools();
Conversion callConversion = new Conversion();
GLSurfaceView mGLSurface;
String Tag = "Debug";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGLSurface = new GLSurfaceView(this);
    mGLSurface.setRenderer(new BasicRenderer());
    setContentView(R.layout.canvas);
    FrameLayout v = (FrameLayout) findViewById(R.id.canvas);
    v.addView(mGLSurface);

    // init views and buttons
    info = (TextView) findViewById(R.id.info);
    enter = (Button) findViewById(R.id.enter);
    line = (Button) findViewById(R.id.line);
    arc = (Button) findViewById(R.id.arc);
    cl = (EditText) findViewById(R.id.cl);

    /*
     * Handler for Main Thread uiHandler = new Handler() { public void
     * handleMessage(Message msg) { switch (msg.what) {
     * 
     * } Bundle bundle = msg.getData(); String string1 =
     * bundle.getString("P1Key"); String string2 =
     * bundle.getString("P2Key"); info.setText(string1);
     * info.setText(string2); } };
     */

}

@Override
public void onClick(View v) {
    switch (v.getId()) {

    case R.id.enter:

        break;
    case R.id.line:

        break;
    case R.id.arc:

        break;

    }

};

public void CreatePopupMenu(View v) {
    PopupMenu mypopupmenu = new PopupMenu(this, v);
    MenuInflater inflater = mypopupmenu.getMenuInflater();
    inflater.inflate(R.menu.filemenu, mypopupmenu.getMenu());
    mypopupmenu.show();
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.newCanas:
        info.setText("New");
        Log.d(Tag, "New was clicked");
        break;
    case R.id.open:
        break;
    case R.id.save:
        break;
    }
    return super.onMenuItemSelected(featureId, item);

}

}


Solution

  • It doesn't look like you're attaching onClickListeners to anything. What that means is you're saying "whenever the onClick event is fired with me as the target, perform this action". But then you're never making yourself a target. Try adding the following code to your onCreate.

    arc.setOnClickListener(this);
    line.setOnClickListener(this);
    enter.setOnClickListener(this);
    

    The same thing happens with the PopupMenu it appears. Try adding mypopupmenu.addOnMenuItemSelectListener(this) right after you inflate the layout for your menu.