Creating a menu on Android

I am new to Android app development. I want to develop a simple Android application that contains a menu. Is there any source code on the web. Tell someone tell me how I should chase

Thanks in advance Tushar

+4
source share
3 answers

All you need to know is in the Android Dev Guide .

What this means - and I just copy the relevant parts from the Android Dev manual - creates an XML menu resource, for example. this one and save it as game_menu.xml :

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/new_game" android:icon="@drawable/ic_new_game" android:title="@string/new_game" /> <item android:id="@+id/help" android:icon="@drawable/ic_help" android:title="@string/help" /> </menu> 

And then inflating it in its activities:

 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } 

When an item is clicked, you can do several things:

 @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.new_game: newGame(); return true; case R.id.help: showHelp(); return true; default: return super.onOptionsItemSelected(item); } } 
+10
source

XML CODE:

 <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_new" android:title="New" /> <item android:id="@+id/menu_about" android:title="About" /> <item android:id="@+id/menu_help" android:title="Help" /> </menu> 

Main code:

 package com.menuexample; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toast; public class MenuSample extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.layout.menus, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_about: Toast.makeText(MenuSample.this, "You Clicked About", 3000).show(); return true; case R.id.menu_help: Toast.makeText(MenuSample.this, "You Clicked Help", 3000).show(); return true; case R.id.menu_new: Toast.makeText(MenuSample.this, "You Clicked New", 3000).show(); return true; default: return super.onOptionsItemSelected(item); } } } 
+2
source

In previous answers, the traditional menu used in android was considered. They are another option that you can use if you are looking for an alternative.

https://github.com/AnshulBansal/Android-Pulley-Menu

The Pulley menu is an alternative to the traditional menu, which allows the user to intuitively select any option for activity. The menu opens by dragging the screen down, and in this gesture the user can also select any of the parameters.

+1
source

Source: https://habr.com/ru/post/1342107/


All Articles