I have this onOptionsItemSelected for a search menu button in mainactivity:
public class MainActivity extends BaseActivity {
...
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_search:
search();
return true;
}
return true;
}
and this in baseactivity:
public class BaseActivity extends AppCompatActivity implements View.OnClickListener{
...
#Override
public boolean onOptionsItemSelected(MenuItem item){
//int id = item.getItemId();
if(mToggle.onOptionsItemSelected(item)){
return true;
}
nDrawerLayout.closeDrawers(); //close menu after click
return super.onOptionsItemSelected(item);
}
but with the two I can't make none of them work. (click not working both)
any ideas how to solve it?
You have to call super.onOptionsItemSelected(item) in your main activity, if you did not consume click. Then onOptionItemSelected will be called in BaseActivity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
search();
return true;
}
return super.onOptionItemSelected(item);
}
Related
I want to navigate to another Activity when click on the option menu item 'settings' from the menu bar. Nothing actual happens.I have checked similar issues posted here, but i can understand why this is not working for option menu.
see the code below:
Can't go to a new activity from selected option from option menu
<item
android:id="#+id/mySettings"
android:title="#string/action_settings" />
<item
android:id="#+id/logout"
android:title="log out" />
code:
public class Dashboard extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.app_bar_menu,menu);
return super.onCreateOptionsMenu(menu);
}
public void openConfigure(){
Intent intent = new Intent(this,Configure.class);
this.startActivity(intent);
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.mySettings:
openConfigure();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
Use onOptionsItemSelected instead of onContextItemSelected cause you are using OptionMenu not ContextMenu.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.mySettings:
openConfigure();
break;
}
return super.onOptionsItemSelected(item);
}
to select an options menu item, you have to override onOptionItemSelected() :
try below code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.mySettings:
openConfigure();
break;
}
return true;
}
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:)
I've the base activity where all toolbar initializations and options menu are done, the activities extending the base can not fire onitemclick
In the base i have
public class BaseActivity extends AppCompatActivity {
private MenuItem refresh;
public Toolbar getToolbar() {
return toolbar;
}
public MenuItem getRefresh() {
return refresh;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
refresh = menu.findItem(R.id.action_refresh);
refresh.setActionView(R.layout.menu_item_view);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return false;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
switch (mState) {
case Const.STATE_IDLE:
refresh.setVisible(true);
break;
case STATE_WORKING:
refresh.setVisible(false);
break;
default:
refresh.setVisible(true);
break;
}
return super.onPrepareOptionsMenu(menu);
}
}
In one of the activites I handle it like
public class CommentsActivity extends BaseToolbarActivity
{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId() == R.id.action_refresh){
setState(Const.STATE_WORKING);
showMsg(contentRoot,"oops");
return true;
}
return super.onOptionsItemSelected(item);
}
}
but the click items won't fire
After a while I came to realize that since I was setting a custom layout for my items (useful for animations) an option menu with a custom view that is either set in the xml or dynamically using
item.setActionView(R.layout.menu_lay);
Just like my problem above the menu item can never be invoked by the normal onOptionsItemSelected listener so the way to make it work is to implement onClickListener on the item's custom View so in my case the way to make it invoke is by
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getRefresh.getActionView().setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//refresh some data
}
});
return true;
}
I have two activities.One of them has Fragment.
From this fragment I can go to another activity, but by clicking the button "home" it goes to previous activity instead of going to the fragment of the previous activity.
Image
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_closet);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Refere below code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You need to override below method :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== android.R.id.home) {
finish();
return true;
}
return false;
}
getSupportActionBar().setNavigationOnClickListener(new OnClickListener {
public void onClick(View v){
onBackPressed()
}
})
Since you want same behavior as of back button you should use onBackPressed();
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I'd like to have only one menu for all my activities. I don't want to repeat my menu code (below) in all my activities.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.referent, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
System.out.println("set");
return true;
case R.id.action_alert:
System.out.println("alert");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I read some topics, but I found just one solution witch is to extends a parent class who declare the menu. I can't use this solution because all my activities are not extending Activity, I have also have FragmentActivity and ListActivity.
Is there a solution to have the same menu on each activity writing a minimum of code on each activity?
Depending on what the menu handling code needs access to from the current activity, you could create a class whose only responsability is to handle the selected menu items. Possibly even with just a static method that receives the MenuItem.
For example, modify activities such that the onOptionsItemSelected is:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean handled = MenuHandler.onOptionsItemSelected(item);
if (!handled) {
handled = super.onOptionsItemSelected(item);
}
return handled;
}
and create the MenuHandler class:
public class MenuHandler {
public static boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId()) {
case 1: //R.id.action_settings:
System.out.println("set");
return true;
case 2: //R.id.action_alert:
System.out.println("alert");
return true;
default:
return false; //allow default processing
}
}
}
all what you need is to extend from a main class
public abstract class main extends activity(){
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// put your common menu code
super.onOptionsItemSelected(item);
}
}
public class HelloActivity extends main{
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
}
}