Android set Visible, Gone, button click - android

At the click of a button, I set the visibility of some Layout. Now when the button is clicked again I want to put all Layouts invisible. How can I do?
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.menu_graf, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.grf:
lista.setVisibility(View.GONE);
chartContainer.setVisibility(View.VISIBLE);
lista_c.setVisibility(View.VISIBLE);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

If the view is visible at first, you could use a boolean variable to know when to hide and when to show your view. For example (Pseudocode):
visible = true;
onclickEvent{
if visible == true{
visible = false
view.setVisibility(View.Gone)
}
else {
visible = true
view.setVisibility(View.VISIBLE)
}
}

Related

Setting visibility of a view when a menu item is selected and when we are done with the menu item

I have implemented a menu item to search a list view. I need to make a view invisible when the menu item is selected. This is easily done with this code in my fragment:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_search:
addButton.setVisibility(View.INVISIBLE);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I can't figure out how to set the visibility back when I am done with the search (I am using SearchView). I tried to use onOptionsMenuClosed (Menu menu) but that is not being called for some reason.
Thanks in advance
Try using setOnActionExpandListener():
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
menu.findItem(R.id.action_search).setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
addButton.setVisibility(View.VISIBLE);
return false; // change to true if `false` wont work for your case
}
});
return super.onCreateOptionsMenu(menu);
}
onMenuItemActionCollapse() will be called when SearchView is collapsed or closed.

How to update menu item in android

I have an activity with menu items. Every time when user come to this activity, i want to update the value of textView with some Utility value. This is my code.
#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_my2, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu){
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
tv.setText(String.valueOf(Utility.ShoppingCartItemCount()));
return true;
}
This is updating the value only once (when launching activity). But when user moves from this activity and come on this activity again, this is not getting updated even value for Utility.ShoppingCartItemCount() is updated.
How to solve this?
Try invalidateOptionsMenu()
This will be call onCreateOptionsMenu(Menu) again.
edit)
I have some code like below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (currentPage == 8) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_p, menu);
return true;
} else {
return false;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.purchaseRestore:
// TODO: Restore purchase
return true;
case R.id.purchaseTerm:
// TODO: Show Term by WebView
return true;
default:
return super.onOptionsItemSelected(item);
}
}
When my user click other button, put invalidateOptionsMenu() after currentPage = 8 in Button's OnClickListener. In result, User can show Option Menu.
edit))
invalidateOptionsMenu() will force reload onCreateOptionsMenu and onPrepareOptionsMenu.
If this methods doesn't working for you, try to debug Utility.ShoppingCartItemCount().

How to click disabled option menu item in Toolbar android?

I have two option items in a Toolbar. When one item is clicked, that item will be enabled. Then another item must be disabled. But, once the item was disabled I can't fire click event anymore on that item. Is there anyway that I can click on the disable item?
Thank you
I did like this but its not working anymore
MenuItem tureMenuItem;
MenuItem dingMenuItem
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.lore_fragment, menu);
tureMenuItem = menu.findItem(R.id.menu_ture);
dingMenuItem = menu.findItem(R.id.menu_ding);
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(false);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_ding:
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(false);
break;
case R.id.menu_ture:
tureMenuItem.setEnabled(false);
dingMenuItem.setEnabled(true);
break;
}
return super.onOptionsItemSelected(item);
}
Using setEnabled() will work. manage with your conditions.
you must be doing some action on those menu items right. You can enable the other item at the end of action.since you have not posted I have only option. Using Handler.PostDelayed.
boolean isMenuEnabled;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_settings) {
this.isMenuEnabled = true;
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(false);
handler.postDelayed(runnable,1000);
return true;
}
else if(item.getItemId()== R.id.action_settings1) {
this.isMenuEnabled = true;
tureMenuItem.setEnabled(false);
dingMenuItem.setEnabled(true);
handler.postDelayed(runnable,1000);
return true;
}
return super.onOptionsItemSelected(item);
}
remove the callbacks
#Override
protected void onDestroy() {
handler.removeCallbacks(runnable);
super.onDestroy();
}
runnable with delay
Runnable runnable = new Runnable() {
#Override
public void run() {
if(isMenuEnabled){
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(true);
isMenuEnabled=false;
}
}
};
You need to check below example code to disable and enable menu item vice-versa.
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
if(menu.getItem(0))
{
menu.getItem(0).setEnable(false);
menu.getItem(1).setEnable(true);
}
else if(menu.getItem(1))
{
menu.getItem(1).setEnable(false);
menu.getItem(0).setEnable(true);
}
return super.onPrepareOptionsMenu(menu);
}

Menu in fragments

I have this scenario:
I have a activity, lets call itAcitivty1 with
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return false;
}
I open a fragment from Activity1 lets call it Fragment1 with:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.layout, container, false);
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (menu != null){
menu.clear();
}
if (!boolean) {
inflater.inflate(R.menu.menu1, menu);
} else {
inflater.inflate(R.menu.menu2, menu);
}
}
This fragment will be called again from activity as a new instance.
Based on the boolean in onCreateOptionsMenu() I'm deciding what menu should be loaded in the fragment so, during the second instance if I click on a menu item, I see the objects of first instance fragment.
I have no clue, why is this happening?
How is the workflow for displaying menu options...
if (menu != null){
menu.clear();
}
That piece of code might be the root cause.
You're telling the system to clear the menu if it's null. Well, FYI, the menu will never be null in the first place; it is supplied by the system. It might have no items inside, but it'll never be null.
One way to check if a menu already contains an item (or more) is to call hasVisibleItems().
From the documentations:
public abstract boolean hasVisibleItems()
Returns True if there is one or more item visible, else false.
Therefore, this is how you should do it:
if (menu.hasVisibleItems()){
menu.clear();
}
In a case like this, you need to put the menu in activity and update the menu dynamically in onPrepareOptionsMenu()
You need to inflate menu in onCreateOptionsMenu(..) of your Activity1 and need to make return true to display menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Your code here
getMenuInflater().inflate(R.menu.main1, menu);//My menu
return true;
}
After that you get menu in your fragment also.
Edits:
If you use single menu file and show / hide MenuItem of your menu. it will solve your problem.
Add all menu in single file.by default all menu items are visible false using android:visible="false"
See example code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
MenuItem item1 = menu.getItem(0);
MenuItem item2 = menu.getItem(1);
MenuItem item3 = menu.getItem(2);
MenuItem item4 = menu.getItem(3);
if(!boolean){
//visible items which you want to show when boolean is false
item1.setVisible(true);
item2.setVisible(true);
}
else
{
//visible items which you want to show when boolean is true
item3.setVisible(true);
item4.setVisible(true);
}
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menuItem1) {
return true;
}
if (id == R.id.menuItem2) {
return true;
}
...
return super.onOptionsItemSelected(item);
}

Set visibility in Menu programmatically android

So, that´s what I wanna know. How can I set the visibility of the menu programatically in Android?? This is how I have my menu:
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId()){
case R.id.menuregistrar:
break;
case R.id.menusalir:
break;
}
return true;
}
But this code is not on the onCreate, so I don´t know how to set one item visible or invisible programmatically (in my case, I want the "menuregistrar" to be invisible once I have registered my application and forever.
Put this method in your Activity
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem register = menu.findItem(R.id.menuregistrar);
if(userRegistered)
{
register.setVisible(false);
}
else
{
register.setVisible(true);
}
return true;
}
in shorter version you could write:
MenuItem register = menu.findItem(R.id.menuregistrar);
register.setVisible(!userRegistered); //userRegistered is boolean, pointing if the user has registered or not.
return true;
I would simplify Adil's solution even further with the following:
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem registrar = menu.findItem(R.id.menuregistrar);
registrar.setVisible(!isRegistered);
return true;
}
Simply do one thing get the id of the item of menu from this line:
Menu menu =navigationView.getMenu();
MenuItem nav_dashboard = menu.findItem(R.id.nav_dashboard);
and than make it visible it accourding to you by this line:
nav_dashboard.setVisible(true/false);
Menu Object has a property to set the visibility of a menu's item using setVisible(boolean)//
Example
private Menu menu_change_language;
...
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
...
menu_change_language = menu;
...
...
return super.onCreateOptionsMenu(menu);
}
use code below for hiding Menu Item:
if(menu_change_language != null){
menu_change_language.findItem(R.id.menu_change_language)
.setVisible(false);
}
Use public boolean onPrepareOptionsMenu (Menu menu) it is called everytime you press the menu button and do your stuff there. or use your oncreateoptionsmenu() in different activities to inflate different menus - this one is called only once.
Cheers
If you want to change the visibility inside the onOptionsItemSelected whenever you click the menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
item.setVisible(true);
return true;
}
OR
for item in the menu that you didn't click on
private Menu globalMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu."menu Xml Name", menu);
globalMenuItem = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
globalMenuItem.findItem(R.id."id of the menu item").setVisible(true);
return true;
}

Categories

Resources