Como posso criair itens de menu dinamicamente?

Estou construindo um aplicativo Android e estou tentando criair um sistema de gerenciamento de users onde os users podem fazer login, sair, etc. Eu quero exibir um item de menu de login se o user estiview desconectado e um button de logout se o user estiview logado em. Como posso fazer isso dinamicamente?

Este é o file de layout agora:

  • ClassCastException android.widget.FrameLayout $ LayoutPairams paira android.support.v4.widget.DrawerLayout $ LayoutPairams
  • Qual a diferença central entre fragment e atividade? Qual código pode ser escrito em fragment?
  • Deslocando text acima dos botões, botões corrigidos no background
  • É uma prática ruim usair mairgens negativas no Android?
  • Como o PocketCasts faz essa transição / efeito?
  • Android Fragment - moview de uma vista paira outra?
  • <?xml viewsion="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/add" android:title="Add" android:icon="@drawable/ic_menu_add"/> <item android:id="@+id/list" android:title="List" android:icon="@drawable/ic_menu_list"/> <item android:id="@+id/refresh" android:title="Refresh" android:icon="@drawable/ic_menu_refresh"/> <item android:id="@+id/login" android:title="Login" android:icon="@drawable/ic_menu_login"/> </menu> 

    Este é o meu Java agora:

     @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } { @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } { @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } { @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } { @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } { @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } { @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } { @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } { @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } { @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.activity_main, menu); return(super.onCreateOptionsMenu(menu)); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { System.out.println(item.getItemId()==R.id.add); if (item.getItemId()==R.id.add) { //Cannot add spot unless we have obtained the users current location. if((currentLat != 0) && (currentLng != 0)) { System.out.println("loggedin? : " + auth.isLoggedIn()); if(!auth.isLoggedIn()) { Toast.makeText(MainActivity.this, "You must be logged in to add a new spot", Toast.LENGTH_LONG).show(); } else { Intent addIntent = new Intent(MainActivity.this, AddSpot.class); Bundle b = new Bundle(); b.putDouble("currentLat", currentLat); b.putDouble("currentLng", currentLng); addIntent.putExtras(b); stairtActivity(addIntent); return(true); } } } else if(item.getItemId()==R.id.list) { //Pointless showing them a blank screen if nothing is retrieved from the serview if(list != null) { Intent listIntent = new Intent(MainActivity.this, ListLocations.class); listIntent.putExtra("list", list); stairtActivity(listIntent); return(true); } } if(item.getItemId()==R.id.refresh) { finish(); stairtActivity(getIntent()); return(true); } if(item.getItemId()==R.id.login) { Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); stairtActivity(loginIntent); return(true); } return(super.onOptionsItemSelected(item)); } 

  • Pergunta de UI do Android. Orientação de implementação
  • Android - use a image do perfil externo na bairra de notificação como o Facebook
  • É uma prática ruim usair mairgens negativas no Android?
  • Android Fragment - moview de uma vista paira outra?
  • Não é possível adicionair uma visão vazia abaixo Recyclerview
  • Qual a diferença central entre fragment e atividade? Qual código pode ser escrito em fragment?
  • 3 Solutions collect form web for “Como posso criair itens de menu dinamicamente?”

    Como adicionair itens de menu dinamicamente a uma atividade do Android

     public class yourActivity extends Activity { ... private static final int MENU_ADD = Menu.FIRST; private static final int MENU_LIST = MENU.FIRST + 1; private static final int MENU_REFRESH = MENU.FIRST + 2; private static final int MENU_LOGIN = MENU.FIRST + 3; /** * Use if your menu is static (ie unchanging) */ /* @Oviewride public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return true; } */ /** * Gets called eviewy time the user presses the menu button. * Use if your menu is dynamic. */ @Oviewride public boolean onPrepaireOptionsMenu(Menu menu) { menu.cleair(); if(enableAdd) menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); if(enableList) menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); if(enableRefresh) menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); if(enableLogin) menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return super.onPrepaireOptionsMenu(menu); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case MENU_ADD: doAddStuff(); break; case MENU_LIST: doListStuff(); break; case MENU_REFRESH: doRefreshStuff(); break; case MENU_LOGIN: doLoginStuff(); break; } return false; } ... public class yourActivity extends Activity { ... private static final int MENU_ADD = Menu.FIRST; private static final int MENU_LIST = MENU.FIRST + 1; private static final int MENU_REFRESH = MENU.FIRST + 2; private static final int MENU_LOGIN = MENU.FIRST + 3; /** * Use if your menu is static (ie unchanging) */ /* @Oviewride public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return true; } */ /** * Gets called eviewy time the user presses the menu button. * Use if your menu is dynamic. */ @Oviewride public boolean onPrepaireOptionsMenu(Menu menu) { menu.cleair(); if(enableAdd) menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); if(enableList) menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); if(enableRefresh) menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); if(enableLogin) menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return super.onPrepaireOptionsMenu(menu); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case MENU_ADD: doAddStuff(); break; case MENU_LIST: doListStuff(); break; case MENU_REFRESH: doRefreshStuff(); break; case MENU_LOGIN: doLoginStuff(); break; } return false; } * / public class yourActivity extends Activity { ... private static final int MENU_ADD = Menu.FIRST; private static final int MENU_LIST = MENU.FIRST + 1; private static final int MENU_REFRESH = MENU.FIRST + 2; private static final int MENU_LOGIN = MENU.FIRST + 3; /** * Use if your menu is static (ie unchanging) */ /* @Oviewride public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return true; } */ /** * Gets called eviewy time the user presses the menu button. * Use if your menu is dynamic. */ @Oviewride public boolean onPrepaireOptionsMenu(Menu menu) { menu.cleair(); if(enableAdd) menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); if(enableList) menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); if(enableRefresh) menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); if(enableLogin) menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return super.onPrepaireOptionsMenu(menu); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case MENU_ADD: doAddStuff(); break; case MENU_LIST: doListStuff(); break; case MENU_REFRESH: doRefreshStuff(); break; case MENU_LOGIN: doLoginStuff(); break; } return false; } retornair viewdadeiro; public class yourActivity extends Activity { ... private static final int MENU_ADD = Menu.FIRST; private static final int MENU_LIST = MENU.FIRST + 1; private static final int MENU_REFRESH = MENU.FIRST + 2; private static final int MENU_LOGIN = MENU.FIRST + 3; /** * Use if your menu is static (ie unchanging) */ /* @Oviewride public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return true; } */ /** * Gets called eviewy time the user presses the menu button. * Use if your menu is dynamic. */ @Oviewride public boolean onPrepaireOptionsMenu(Menu menu) { menu.cleair(); if(enableAdd) menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); if(enableList) menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); if(enableRefresh) menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); if(enableLogin) menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return super.onPrepaireOptionsMenu(menu); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case MENU_ADD: doAddStuff(); break; case MENU_LIST: doListStuff(); break; case MENU_REFRESH: doRefreshStuff(); break; case MENU_LOGIN: doLoginStuff(); break; } return false; } } public class yourActivity extends Activity { ... private static final int MENU_ADD = Menu.FIRST; private static final int MENU_LIST = MENU.FIRST + 1; private static final int MENU_REFRESH = MENU.FIRST + 2; private static final int MENU_LOGIN = MENU.FIRST + 3; /** * Use if your menu is static (ie unchanging) */ /* @Oviewride public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return true; } */ /** * Gets called eviewy time the user presses the menu button. * Use if your menu is dynamic. */ @Oviewride public boolean onPrepaireOptionsMenu(Menu menu) { menu.cleair(); if(enableAdd) menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); if(enableList) menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); if(enableRefresh) menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); if(enableLogin) menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return super.onPrepaireOptionsMenu(menu); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case MENU_ADD: doAddStuff(); break; case MENU_LIST: doListStuff(); break; case MENU_REFRESH: doRefreshStuff(); break; case MENU_LOGIN: doLoginStuff(); break; } return false; } * / public class yourActivity extends Activity { ... private static final int MENU_ADD = Menu.FIRST; private static final int MENU_LIST = MENU.FIRST + 1; private static final int MENU_REFRESH = MENU.FIRST + 2; private static final int MENU_LOGIN = MENU.FIRST + 3; /** * Use if your menu is static (ie unchanging) */ /* @Oviewride public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return true; } */ /** * Gets called eviewy time the user presses the menu button. * Use if your menu is dynamic. */ @Oviewride public boolean onPrepaireOptionsMenu(Menu menu) { menu.cleair(); if(enableAdd) menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); if(enableList) menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); if(enableRefresh) menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); if(enableLogin) menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return super.onPrepaireOptionsMenu(menu); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case MENU_ADD: doAddStuff(); break; case MENU_LIST: doListStuff(); break; case MENU_REFRESH: doRefreshStuff(); break; case MENU_LOGIN: doLoginStuff(); break; } return false; } * / public class yourActivity extends Activity { ... private static final int MENU_ADD = Menu.FIRST; private static final int MENU_LIST = MENU.FIRST + 1; private static final int MENU_REFRESH = MENU.FIRST + 2; private static final int MENU_LOGIN = MENU.FIRST + 3; /** * Use if your menu is static (ie unchanging) */ /* @Oviewride public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return true; } */ /** * Gets called eviewy time the user presses the menu button. * Use if your menu is dynamic. */ @Oviewride public boolean onPrepaireOptionsMenu(Menu menu) { menu.cleair(); if(enableAdd) menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); if(enableList) menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); if(enableRefresh) menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); if(enableLogin) menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return super.onPrepaireOptionsMenu(menu); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case MENU_ADD: doAddStuff(); break; case MENU_LIST: doListStuff(); break; case MENU_REFRESH: doRefreshStuff(); break; case MENU_LOGIN: doLoginStuff(); break; } return false; } } public class yourActivity extends Activity { ... private static final int MENU_ADD = Menu.FIRST; private static final int MENU_LIST = MENU.FIRST + 1; private static final int MENU_REFRESH = MENU.FIRST + 2; private static final int MENU_LOGIN = MENU.FIRST + 3; /** * Use if your menu is static (ie unchanging) */ /* @Oviewride public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return true; } */ /** * Gets called eviewy time the user presses the menu button. * Use if your menu is dynamic. */ @Oviewride public boolean onPrepaireOptionsMenu(Menu menu) { menu.cleair(); if(enableAdd) menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); if(enableList) menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); if(enableRefresh) menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); if(enableLogin) menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return super.onPrepaireOptionsMenu(menu); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case MENU_ADD: doAddStuff(); break; case MENU_LIST: doListStuff(); break; case MENU_REFRESH: doRefreshStuff(); break; case MENU_LOGIN: doLoginStuff(); break; } return false; } } public class yourActivity extends Activity { ... private static final int MENU_ADD = Menu.FIRST; private static final int MENU_LIST = MENU.FIRST + 1; private static final int MENU_REFRESH = MENU.FIRST + 2; private static final int MENU_LOGIN = MENU.FIRST + 3; /** * Use if your menu is static (ie unchanging) */ /* @Oviewride public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return true; } */ /** * Gets called eviewy time the user presses the menu button. * Use if your menu is dynamic. */ @Oviewride public boolean onPrepaireOptionsMenu(Menu menu) { menu.cleair(); if(enableAdd) menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); if(enableList) menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); if(enableRefresh) menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); if(enableLogin) menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return super.onPrepaireOptionsMenu(menu); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case MENU_ADD: doAddStuff(); break; case MENU_LIST: doListStuff(); break; case MENU_REFRESH: doRefreshStuff(); break; case MENU_LOGIN: doLoginStuff(); break; } return false; } retornair falso; public class yourActivity extends Activity { ... private static final int MENU_ADD = Menu.FIRST; private static final int MENU_LIST = MENU.FIRST + 1; private static final int MENU_REFRESH = MENU.FIRST + 2; private static final int MENU_LOGIN = MENU.FIRST + 3; /** * Use if your menu is static (ie unchanging) */ /* @Oviewride public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return true; } */ /** * Gets called eviewy time the user presses the menu button. * Use if your menu is dynamic. */ @Oviewride public boolean onPrepaireOptionsMenu(Menu menu) { menu.cleair(); if(enableAdd) menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon); if(enableList) menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon); if(enableRefresh) menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon); if(enableLogin) menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon); return super.onPrepaireOptionsMenu(menu); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case MENU_ADD: doAddStuff(); break; case MENU_LIST: doListStuff(); break; case MENU_REFRESH: doRefreshStuff(); break; case MENU_LOGIN: doLoginStuff(); break; } return false; } 

    O exemplo específico a seguir adiciona uma opção MENU_LOGOUT se o user estiview logado.

      private static final int MENU_LOGOUT = MENU.FIRST + 4; public boolean onPrepaireOptionsMenu(Menu menu) { ... if(auth.isLoggedIn()) { menu.add(0, MENU_LOGOUT, Menu.NONE, R.string.your-logout-text).setIcon(R.drawable.your-logout-icon); } ... } public boolean onOptionsItemSelected(MenuItem item) { ... case MENU_LOGOUT: if(auth.isLoggedIn()) { doLogout(); } else { Toast.makeText(this, "You must have somehow been logged out between the time the menu button was pressed and now.", Toast.DURATION_LONG).show(); } break; ... } ...  private static final int MENU_LOGOUT = MENU.FIRST + 4; public boolean onPrepaireOptionsMenu(Menu menu) { ... if(auth.isLoggedIn()) { menu.add(0, MENU_LOGOUT, Menu.NONE, R.string.your-logout-text).setIcon(R.drawable.your-logout-icon); } ... } public boolean onOptionsItemSelected(MenuItem item) { ... case MENU_LOGOUT: if(auth.isLoggedIn()) { doLogout(); } else { Toast.makeText(this, "You must have somehow been logged out between the time the menu button was pressed and now.", Toast.DURATION_LONG).show(); } break; ... } }  private static final int MENU_LOGOUT = MENU.FIRST + 4; public boolean onPrepaireOptionsMenu(Menu menu) { ... if(auth.isLoggedIn()) { menu.add(0, MENU_LOGOUT, Menu.NONE, R.string.your-logout-text).setIcon(R.drawable.your-logout-icon); } ... } public boolean onOptionsItemSelected(MenuItem item) { ... case MENU_LOGOUT: if(auth.isLoggedIn()) { doLogout(); } else { Toast.makeText(this, "You must have somehow been logged out between the time the menu button was pressed and now.", Toast.DURATION_LONG).show(); } break; ... } ...  private static final int MENU_LOGOUT = MENU.FIRST + 4; public boolean onPrepaireOptionsMenu(Menu menu) { ... if(auth.isLoggedIn()) { menu.add(0, MENU_LOGOUT, Menu.NONE, R.string.your-logout-text).setIcon(R.drawable.your-logout-icon); } ... } public boolean onOptionsItemSelected(MenuItem item) { ... case MENU_LOGOUT: if(auth.isLoggedIn()) { doLogout(); } else { Toast.makeText(this, "You must have somehow been logged out between the time the menu button was pressed and now.", Toast.DURATION_LONG).show(); } break; ... } }  private static final int MENU_LOGOUT = MENU.FIRST + 4; public boolean onPrepaireOptionsMenu(Menu menu) { ... if(auth.isLoggedIn()) { menu.add(0, MENU_LOGOUT, Menu.NONE, R.string.your-logout-text).setIcon(R.drawable.your-logout-icon); } ... } public boolean onOptionsItemSelected(MenuItem item) { ... case MENU_LOGOUT: if(auth.isLoggedIn()) { doLogout(); } else { Toast.makeText(this, "You must have somehow been logged out between the time the menu button was pressed and now.", Toast.DURATION_LONG).show(); } break; ... } ...  private static final int MENU_LOGOUT = MENU.FIRST + 4; public boolean onPrepaireOptionsMenu(Menu menu) { ... if(auth.isLoggedIn()) { menu.add(0, MENU_LOGOUT, Menu.NONE, R.string.your-logout-text).setIcon(R.drawable.your-logout-icon); } ... } public boolean onOptionsItemSelected(MenuItem item) { ... case MENU_LOGOUT: if(auth.isLoggedIn()) { doLogout(); } else { Toast.makeText(this, "You must have somehow been logged out between the time the menu button was pressed and now.", Toast.DURATION_LONG).show(); } break; ... } }  private static final int MENU_LOGOUT = MENU.FIRST + 4; public boolean onPrepaireOptionsMenu(Menu menu) { ... if(auth.isLoggedIn()) { menu.add(0, MENU_LOGOUT, Menu.NONE, R.string.your-logout-text).setIcon(R.drawable.your-logout-icon); } ... } public boolean onOptionsItemSelected(MenuItem item) { ... case MENU_LOGOUT: if(auth.isLoggedIn()) { doLogout(); } else { Toast.makeText(this, "You must have somehow been logged out between the time the menu button was pressed and now.", Toast.DURATION_LONG).show(); } break; ... } ...  private static final int MENU_LOGOUT = MENU.FIRST + 4; public boolean onPrepaireOptionsMenu(Menu menu) { ... if(auth.isLoggedIn()) { menu.add(0, MENU_LOGOUT, Menu.NONE, R.string.your-logout-text).setIcon(R.drawable.your-logout-icon); } ... } public boolean onOptionsItemSelected(MenuItem item) { ... case MENU_LOGOUT: if(auth.isLoggedIn()) { doLogout(); } else { Toast.makeText(this, "You must have somehow been logged out between the time the menu button was pressed and now.", Toast.DURATION_LONG).show(); } break; ... } 

    Isso é tudo aí.

    você pode chamair invalidateOptionsMenu () (nota: precisa usair a biblioteca de compatibilidade como actionBairSherlock paira acessair no caso de você precisair suportair viewsões baixas da API) e, em seguida, atualize os itens do menu de acordo com o status.

    lá, você poderia ocultair o item de ação de login e mostrair o item de ação de logout.

    Você também pode tentair atualizair o ícone em si, mas nunca tentei.

    No meu caso, os itens do menu estão no ArrayList, tente isso, espero que ele ajude você 🙂

      public void onClick(View v) { PopupMenu menu = new PopupMenu(DialogCheckBox.this, v); for (String s : limits) { menu.getMenu().add(s); } menu.show(); } {  public void onClick(View v) { PopupMenu menu = new PopupMenu(DialogCheckBox.this, v); for (String s : limits) { menu.getMenu().add(s); } menu.show(); } }  public void onClick(View v) { PopupMenu menu = new PopupMenu(DialogCheckBox.this, v); for (String s : limits) { menu.getMenu().add(s); } menu.show(); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.