The idea is to temporarily hide an option menu for a specific fragment. If the user is signed in, the system should hide the menu.
In all other fragments the menu should be implemented and enabled.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_main_home:
if (MainActivity.isSignedIn() == true){
(signedIn == false)
forwardToWelcomeFragment();
} else {
Toast.makeText(this, "You are not logged in.",
Toast.LENGTH_SHORT).show();
forwardToLoginFragment();
}
return true;
case R.id.menu_main_settings:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_main_info:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_main_signout:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
setSignedIn(false);
forwardToLoginFragment();
return true;
default:
return super.onOptionsItemSelected(item);
}
Related
I created a navigation bar in android and I am unable to select an item. I want to display a toast when a specific item is selected but it doesn't enter into the if condition of checking id of the item clicked.
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (abdt.onOptionsItemSelected(item)) {
if (item.getItemId() == R.id.addCat) {
Toast.makeText(
getApplicationContext(),
"Category Name Field is Empty!",
Toast.LENGTH_LONG
).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
In Navigation Drawer to implement item selection, you need to implement the interface of NavigationView.OnNavigationItemSelectedListener and
navigationView.setNavigationItemSelectedListener(this);
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.addCat:
Toast.makeText(getApplicationContext(), "Category Name Field is Empty!", Toast.LENGTH_LONG).show();
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return false;
}
So in my MainActivity, I can change the background of the layout by clicking on the overflow in my actionbar and selecting a background.
But how can I change the layout across all other activities?
This is the code i have now
private int selectedBackgroundId = R.id.defaultTheme;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.defaultTheme:
mainLayout.setBackgroundResource(R.drawable.defaultbackground);
if (selectedBackgroundId == R.id.defaultTheme){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.defaultTheme;
return true;
case R.id.background1:
mainLayout.setBackgroundResource(R.drawable.redpinkgradientbackground);
if (selectedBackgroundId == R.id.background1){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.background1;
return true;
Second question i have
How can I save the layout that has been set when app is destroyed?
So when I reopen the app, it's still the layout I clicked in the overflow menu.
I have problem in creating pop-up menu on menu key event. I don’t understand how to pass required parameter to popmenu constructor. If any know what is the problem in my code then please suggest.
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU)
{
View v=getCurrentFocus();
PopupMenu popupMenu = new PopupMenu(this,v);
popupMenu.inflate(R.menu.poupup_menu);
popupMenu.setOnMenuItemClickListener(
new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_red:
Toast.makeText(context,"red",2000).show();
break;
case R.id.menu_blue:
Toast.makeText(context,"red",2000).show();
break;
case R.id.menu_green:
Toast.makeText(context,"red",2000).show();
break;
}
return true;
}
});
popupMenu.show();
// ...
return true;
} else {
return super.onKeyUp(keyCode, event);
}
}
}
thanks guys for answering...I got perferct solution to handle android menus..Override onCreateoption menu method,in that get menu instance and inflate our customize menu..and handle menu click event.
/* Initiating Menu XML file (menu.xml) */
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_preferences:
Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onPopup(View view)
{
final PopupMenu menu=new PopupMenu(this,view);
menu.getMenuInflater().inflate(R.menu.menu1,menu.getMenu());
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
Toast toast=Toast.makeText(MainActivity.this,
item.getTitle()+"Selected",Toast.LENGTH_SHORT);
//Intent intent2 = new Intent(MainActivity.this, YourSpotActivity.class);
//startActivity(intent2);
//startActivity(new Intent(MainActivity.this,YourSpotActivity.class));
toast.show();
return true;
}
});
menu.show();
}
When i click any one of the list item then it will start another activity.
How can i do that by modify an above code. explain me please.
I have use four car model in the menu. when i choose any one of that car then it will go to particular activity.
You need to use switch as below
switch (item.getItemId()) {
case R.id.menuitem1:
Toast.makeText(getApplicationContext(), "StartActiviy 1", Toast.LENGTH_SHORT).show();
// start activity 1
return true;
case R.id.menuitem2:
Toast.makeText(getApplicationContext(), "StartActiviy 2", Toast.LENGTH_SHORT).show();
// start activity 2
return true;
default:
//default intent
return true;
}
http://developer.android.com/reference/android/widget/PopupMenu.html
You can use switch statement as below inside onMenuItemClick:
switch (item.getItemId()) {
case R.id.menuitem1:
//calling intent ( activity1 )
case R.id.menuitem2:
//calling intent ( activity 2)
default:
//default intent
}
I'm using ActionBar in my application, and I want when the user clicks a button, the item in the ActionBar should change the text.
This is my code for onOptionsItemSelected():
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
.show();
finish();
break;
case R.id.lg:
Toast.makeText(getBaseContext(), "ma", Toast.LENGTH_SHORT).show();
break;
case R.id.French:
Toast.makeText(getBaseContext(), "zaki", Toast.LENGTH_SHORT).show();
break;
case R.id.nerlandais:
Toast.makeText(getBaseContext(), "brahim", Toast.LENGTH_SHORT)
.show();
}
return true;
}
What must I do to change the item title from another item?
Example: When I click in French item I want to change nerlandais item title.
If you want to change an MenuItem's title clicking to another item you can do something like this :
private String mMenuItemTitle;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.nerlandais);
item.setText(mMenuItemTitle);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.French:
Toast.makeText(getBaseContext(), "zaki", Toast.LENGTH_SHORT).show();
mMenuItemTitle = "My New Title";
supportInvalidateOptionsMenu();
break;
}
return true;
}
Try item.setTitle("new title")
Or, if from another item then the Menu has findItem