C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Android Context Menu ExampleAndroid context menu appears when user press long click on the element. It is also known as floating menu. It affects the selected content while doing action on it. It doesn't support item shortcuts and icons. Android Context Menu ExampleLet's see the simple example of context menu in android. activity_main.xmlDrag one listview from the pallete, now the xml file will look like this: File: activity_main.xml
main_menu.xmlCreate a separate menu_main.xml file in menu directory for menu items. Activity classLet's write the code to display the context menu on press of the listview. File: MainActivity.java
package example.TheDeveloperBlog.com.contextmenu; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.ContextMenu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ListView listView; String contacts[]={"Ajay","Sachin","Sumit","Tarun","Yogesh"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView)findViewById(R.id.listView); ArrayAdapter Output:Output after long press on the listview. Output after clicking on the context menu.
Next TopicAndroid Popup Menu Example
|