Opening a floating menu (context menu) in Android?

I created a new menu called drmenu.xml. It works correctly when I click the menu button, but I need to open the context menu when the user clicks the button. In the code below, the button just shows a toast.

this is my xml layout:

 <LinearLayout
        android:id="@+id/MenuCall"
        android:layout_width="90dip"
        android:layout_height="match_parent"
        android:gravity="right|center_vertical" >
        <ImageView
            android:id="@+id/MenuCall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/imageiew6" />
    </LinearLayout>

      

and this is my java code:

    final LinearLayout callback_var = (LinearLayout) findViewById(R.id.MenuCall);
    registerForContextMenu(callback_var);
    callback_var.setOnClickListener(new android.view.View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "this is repeated",      Toast.LENGTH_LONG).show();
            openContextMenu(callback_var);
        }

      

+4


source to share


2 answers


If you want to create a context menu, you must call a method registerForContextMenu()

passing it a view that should be associated with the context menu.

For example, suppose to bind a context menu to a button:

Button button = (Button) findViewById(R.id.my_button);
registerForContextMenu(button);

      

which can be called in onCreate () of your activity. Then, inside the same activity, you need to override the method onCreateContextMenu()

.

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.your_context_menu, menu);
}

      

Then you need to implement onContextItemSelected()

to trigger the correct action when a button is clicked inside the context menu:



@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.first_action:
            // your first action code
            return true;
        case R.id.second_action:
            // your second action code
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

      

Now press and hold the button on the context menu that you defined in the file your_context_menu.xml

.

Please note that opening the context menu with a long press follows the Android UI standard, however if you would like your context menu to be displayed with a simple click you can see the answer here

Note: As stated here

The ID does not have to be unique across the entire tree, but it must be unique within the part of the tree you are looking for (which can often be the entire tree, so it is best to be completely unique when possible).

+6


source


as Joseph82 said for the context menu, we can use this path. and I find to use the options menu, I can use the hit code: in the Oncreate method, write this:



    final LinearLayout callback_var = (LinearLayout) findViewById(R.id.MenuCall);
    callback_var.setOnClickListener(new android.view.View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            openOptionsMenu();
        }
    });


 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    MenuInflater mnu = getMenuInflater();
    mnu.inflate(R.menu.darunamamenu, menu);
    return true;
}


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId())
        {
        case R.id.firstpage:

            break;
        case R.id.exit:
            finish();
            System.exit(0);
            break;
        case R.id.setting:
            Intent Settingact = new Intent(G.currentActivity, SettingActivity.class);
            G.currentActivity.startActivity(Settingact);
            G.currentActivity.finish();
            break;
        case R.id.regalarm:
            Intent RegAlarm = new Intent(G.currentActivity, alarmsetter.class);
            G.currentActivity.startActivity(RegAlarm);
            G.currentActivity.finish();
            break;
        case R.id.inform:
            Intent inform = new Intent(G.currentActivity, ActivityInformation.class);
            G.currentActivity.startActivity(inform);
            G.currentActivity.finish();
            break;
    }
    return super.onOptionsItemSelected(item);
}

      

0


source







All Articles