Explanation of Java syntax - getMenuInflater ()

Just download android studio and I am using the great ranch android programming guide to learn the ropes.

When you start android studio, this code is already in the main activity file:

 @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_quiz, menu);**
    return true;
}

      

I don't understand the line getMenuInflater

. In my short experience with Java, only the object comes before the method when using a period to separate two like in dog.bark()

. Here it looks like the line means a call to the inflation method, which is defined in the getMenuInflater method. However, I checked the source code for getMenuInflater()

and there is no bloat method in its body.

Can anyone demystify the syntax in this line for me?

+3


source to share


4 answers


String getMenuInflater().inflate(R.menu.menu_quiz, menu);

is short form:



MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_quiz, menu)

      

+2


source


You are extending the activity, and android studio adds that the menu for the layout is configured for you, below is the actual implementation of the method in the activity class:



 public MenuInflater getMenuInflater() {
            // Make sure that action views can get an appropriate theme.
            if (mMenuInflater == null) {
                initWindowDecorActionBar();
                if (mActionBar != null) {
                    mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
                } else {
                    mMenuInflater = new MenuInflater(this);
                }
            }
            return mMenuInflater;
        }

      

+2


source


I'm not really sure what you are confused with, but I believe you think that you should always have a method call after the object name. This is mostly true (except for static methods), so you can think of a call like

this.getMenuInflater()

      

It is a keyword in Java referring to the current object calling the method

+1


source


This sort of menu will be automatically created in the Res folder named menu.xml,

<menu xmlns:android="http://schemas.android.com/apk/res/android" >


<item android:id="@+id/slideshowbutton"
      android:icon="@drawable/settings"
      android:title="@string/settings"/>

<item android:id="@+id/editbutton"
      android:icon="@drawable/adduser"
      android:title="@string/adduser"/>

<item android:id="@+id/cropbutton"
      android:icon="@drawable/message"
      android:title="@string/message"/>

<item android:id="@+id/detailsbutton"
      android:icon="@drawable/logout"
      android:title="@string/Logout"/>

      

you can add menu list as you want

The menu will look like enter image description here

0


source







All Articles