Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9086521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:30:36+00:00 2026-06-16T21:30:36+00:00

I have actionbar menuitems cancel and save. menu.xml <?xml version=1.0 encoding=utf-8?> <menu xmlns:android=http://schemas.android.com/apk/res/android >

  • 0

I have actionbar menuitems cancel and save.

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/saveButton" 
          android:showAsAction="always"          
          android:title="@string/save" 
          android:visible="true">

    </item>
    <item android:id="@+id/cancelButton" 
          android:showAsAction="always"         
          android:title="@string/cancel" 
          android:visible="true">        
    </item>

</menu>

I want to disable save menuitem when activity is started.

My activity code –

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_project);

        EditText projectName = (EditText) findViewById(R.id.editTextProjectName);   
        projectName.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                configureSaveButton(s);             
            }           
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            @Override
            public void afterTextChanged(Editable s) {}
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.addprojectmenu, menu);      
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem item = (MenuItem) findViewById(R.id.saveButton);
        item.setEnabled(false);     
        return super.onPrepareOptionsMenu(menu);
    }

    private void configureSaveButton(CharSequence s){
        String text = null;
        if(s != null){
            text = s.toString();
        }       
        if(text != null && text.trim().length() != 0){

        }else{

        }
    }

So what I am trying to do here is, initially when activity is started save menu item should be disabled and when editext contains some text then it should be enabled.

I am not sure what should be the code in if else in configureSaveButton method.
Also how can i disable save menu item initially.

I get null pointer exception in onPrepareOptionsMenu.

I am using android 4.1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:30:37+00:00Added an answer on June 16, 2026 at 9:30 pm
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.addprojectmenu, menu);      
    
        menu.getItem(0).setEnabled(false); // here pass the index of save menu item
        return super.onPrepareOptionsMenu(menu);
    
    }
    

    Just inflate it on prepare time and disable after inflated menu no need to inflate in oncreateoptionemenu time or you can just use last two line of code after inflating from onCreateOptionMenu.

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    
        menu.getItem(0).setEnabled(false); // here pass the index of save menu item
        return super.onPrepareOptionsMenu(menu);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the dev guide related to the subject http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems So we have in
I have an ActionBar in my Android app (API Level 14). There is a
Is it possible on Android to have MenuItems (either the ones from the options
I have a collapsed search EditView menu within my Actionbar (ActionbarSherlock). When setShowAsAction method
When using the ActionBar in Android, how do you refresh the options menu? I
I have MenuItems in the ActionBar and I am using Fragments inside ViewPager. Now
I have a menu option in my ActionBarSherlock Actionbar that kicks off an AsyncTask.
I have a actionbar with a custom layout, but this show me the icon
I have an actionBar with multiple items, I would like to change the colour
I have this application where I implement the ActionBar Fragment interface. Underlying the interface,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.