Hide or Show a Menu Item if Webview Class Activated? - android

I am trying to disable programmatically a menu item if WebView class is not activated.
I tried this code:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
MenuItem item = menu.findItem(R.id.itemRefresh);
if (WebViewFragment.viewContentWebView.isActivated()){
item.setVisible(true);
item.setEnabled(true);
} else
{
item.setVisible(true);
item.setEnabled(false);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.itemRefresh:
WebViewFragment.viewContentWebView.reload();
return true;
case R.id.itemAdd:
//oke im gonna write here
return true;
case R.id.itemHelp:
// ok ok i will
return true;
default:
return true;
}
}
In Eclipse I don't get any error msg but on real device App blocks itself.
I think I'm doing something wrong in IF-ELSE statement part.
Thank you.

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.

Handling Click Event for menu Used for Floating Action Button Options

I used a library recently that Provides a Floating Action Button and changing to a Something like Navigation bar by clicking on it.
but i cant set click Event for my navigation bar Items.
after Pressing on items i don't see any reaction!
Anyone can help me?
fabOptions = (FabOptions) findViewById(R.id.fabcontainer);
fabOptions.setButtonsMenu(R.menu.menu1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu1, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back:
newBack();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void newBack() {
Toast.makeText(this, "back", Toast.LENGTH_SHORT).show();
}
}
First of all you should have to use break whenever you use switch statement otherwise the program will go to default status. so use break; the code below.
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getId(); // Retrieve the id of the selected menu item
switch(id) {
case R.id.back:
newBack();
return true;
break;
case ...
}
return super.onOptionsItemSelected(item);
}

Android menu crashs application

I have the following in my code and I want to switch to this new activity when I select it from the menu, but the app just keeps closing:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.mi_baas:
startActivity(new Intent("com.my.project.BAAS"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Please help, 2 days so far.....
You have to pass context and class which is to be opened.
Your code should be like this.
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.mi_baas:
startActivity(new Intent(getContext(),BAAS.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Hope it helps:)

How to implement a menuitem click listener inside a fragment in android

I have MenuItems in the ActionBar and I am using Fragments inside ViewPager. Now I would like to handle onMenuItemClickListener event inside my fragment. It works fine inside Main Activity. But not inside Fragments. And also it doesn't fetch any error.
Here is the methods that I tried. Both works fine inside Activity.
First method:
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.grid_view);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.v("test","dfsfdsfasd");
return true;
}
});
return true;
}
Second Method:
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.grid_view:
{
Log.v("Log:","grid_view item pressed");
return true;
}
case R.id.list_view:
{
Log.v("Log:","list_view item pressed");
return true;
}
default:
return true;
}
}
Any help on how to achieve this will be appreciated.
Solved by using onPrepareOptionsMenu method.

Enable/Disable Options Menu button with .setEnabled()?

Im trying to do something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//multiListener = false;
menu.add(0,START_DELETE,0, "Delete selected..").setEnabled(multiListener);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.multiselect:
if(multiselect == 0) { multiselect = 1;
multiListener = true;
Log.d("DH", "index="+multiListener);
}
else if(multiselect == 1) { multiselect = 0; multiListener = false;
Log.d("DH", "index="+multiListener);
}
fillData();
return true;
case START_DELETE:
Toast.makeText(Notepadv3.this, "Pressed delete", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Basically, if multiListener = true; make "Delete selected.." pressable otherwise gray it out...
This simple , thing.. doesn't want to work out with me,
for somehow... the button is always greyed out, although Log says that, it changes to true...
Anyone, know something?
You should call setEnabled() again to change item's state. Its state doesn't bind to a variable. This method should be called in onPrepareOptionsMenu().

Categories

Resources