There is a LinearLayout . In the code I create 10 buttons and add them to LinearLayout . I put a context menu on each button, with a long press on the button, a dialogue is created in which there are 2 options: rename and delete the button.
I can't figure out how to get the id of the button I clicked. That the text changed on it and that button which I selected was removed.
2 sample code. In one I fill in LinearLayout , and in the other I want to get the id of the button I clicked on. I tried through linearLayout.getChildAt , but only the element whose index I specify, and not the one I linearLayout.getChildAt , is deleted and renamed there.
Filling:
linearLayout = (LinearLayout) findViewById(R.id.layout); for (int i = 0; i < 10; i++) { Button button = new Button(this); button.setText("new button " + i); button.setId(i); registerForContextMenu(button); linearLayout.addView(button); } Treatment:
@Override public boolean onContextItemSelected(final MenuItem item) { final View customView = getLayoutInflater().inflate(R.layout.custom_alert_dialog, null); switch (item.getItemId()) { case R.id.menuRename: final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Rename") .setView(customView) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { EditText editText = (EditText) customView.findViewById(R.id.editText); String nameButton = editText.getText().toString(); Button button = (Button) linearLayout.getChildAt(2); button.setText(nameButton); } }) .create() .show(); break; case R.id.menuDelete: linearLayout.removeView(linearLayout.getChildAt(2)); break; } return true; }