When you click on the buttons in the Genymotion emulator, instead of opening the context menu, there is no reaction. There is no error in the logs. Checked on a standard emulator, the same error. What could be the problem?
Main Activity:
package com.example.hasanshans.segundo; import android.graphics.Color; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.ContextMenu; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { Button tvColor, tvSize; final int MENU_COLOR_GREEN = 1; final int MENU_COLOR_BLUE = 2; final int MENU_COLOR_RED = 3; final int MENU_SIZE_22 = 4; final int MENU_SIZE_26 = 5; final int MENU_SIZE_30 = 6; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvColor = (Button) findViewById(R.id.tvColor); tvSize = (Button) findViewById(R.id.tvSize); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); tvColor.setOnCreateContextMenuListener(this); registerForContextMenu(tvSize); setSupportActionBar(toolbar); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { switch (v.getId()){ case R.id.tvColor: menu.add(0,MENU_COLOR_BLUE, 0, "BLUE"); menu.add(0,MENU_COLOR_RED, 0, "RED"); menu.add(0,MENU_COLOR_GREEN, 0, "GREEN"); break; case R.id.tvSize: menu.add(0,MENU_SIZE_22, 0, "22"); menu.add(0, MENU_SIZE_26, 0, "26"); menu.add(0,MENU_SIZE_30, 0, "30"); break; } } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_COLOR_RED: tvColor.setTextColor(Color.RED); tvColor.setText("Text color = red"); break; case MENU_COLOR_GREEN: tvColor.setTextColor(Color.GREEN); tvColor.setText("Text color = green"); break; case MENU_COLOR_BLUE: tvColor.setTextColor(Color.BLUE); tvColor.setText("Text color = blue"); break; // пункты меню для size case MENU_SIZE_22: tvSize.setTextSize(22); tvSize.setText("Text size = 22"); break; case MENU_SIZE_26: tvSize.setTextSize(26); tvSize.setText("Text size = 26"); break; case MENU_SIZE_30: tvSize.setTextSize(30); tvSize.setText("Text size = 30"); break; } return super.onContextItemSelected(item); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } xml:
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/tvColor" android:layout_below="@+id/toolbar" android:layout_centerHorizontal="true" android:layout_marginTop="66dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/tvSize" android:layout_below="@+id/tvColor" android:layout_centerHorizontal="true" android:layout_marginTop="63dp" /> </RelativeLayout>