onCreateOptionsMenu from java android application — Java Android

5Ssoftwaresafety
5S JAVA AND KOTLIN LEARNING
4 min readOct 15, 2020

This is how you can display an Android Android Options Menu 3 DOT STYLESMain Activity in Java Android Studio:

Setting menus in OnCreateOptionsMenu()

Android Options Menu in JAVA ANDROID

This example demonstrates how to use the oncreateoptionsmenu fragment in android studio. Also you are going to make onOptionsItemSelected(MenuItem item) practice.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Create Empty Project

How to use oncreateoptionsmenu fragment in android?

Step 2 — “Add the following code to res/layout/activity_main.xml.“

onCreateOptionsMenu(Menu menu) menu layout

XML CODE

I worked in simple relative layout method for only give basic example.

*In the below code, we have declared Textview as ”@+id/textView”

RELATIVE LAYOUT FILE CODE IN XML:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yourname.yourandroidstuido.MainActivity"
android:padding="16dp"
android:id="@+id/relLayout"
android:orientation="vertical"
>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:text="Click the three dots on the upper right corner."
android:textStyle="bold"
android:textSize="20sp"
/>

<TextView
android:id="@+id/textViewJavaLearn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="300dp"
android:gravity="center"
android:text="5S JAVA AND KOTLIN LEARNING"
/>


</RelativeLayout>

Step 3: Create Menu Item in XML

Create menu Item in XML file within res/menu.

Click res and create new resource file and select menu fromresource type section
Select menu as resource tyoe and source set and file name as optionsmenulayout.
Step 3: Write below code between menu (profile, settings and signout)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/profile" android:title="Profile" />
<item android:id="@+id/settings" android:title="Settings" />
<item android:id="@+id/signout" android:title="Sign out" />
</menu>>

Step 4: MainActivity.java” java file − Add the following code to src/MainActivity.java

JAVA CODE IN MAIN ACTIVITY:

package com.yourname.yourpackagename;import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {

TextView textView;
RelativeLayout relativeLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = (TextView) findViewById(R.id.textView);
relativeLayout = (RelativeLayout) findViewById(R.id.relLayout);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//inflate the menu resource file in your activity
getMenuInflater().inflate(R.menu.optionsmenulayout, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
//code for item select event handling
switch(item.getItemId()){
case R.id.profile:
textView.setText("Profile");
relativeLayout.setBackgroundColor(Color.CYAN);
break;
case R.id.settings:
textView.setText("Settings");
relativeLayout.setBackgroundColor(Color.MAGENTA);
break;
case R.id.signout:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}

Review Application

The OnCreateOptionsMenu() method is called to give an opportunity to the Activity parameter to define actions for the ActionBar.

The Activity class provides a MenuInflater method, which reads the XML definition file and places the action defined on the ActionBar.

Step 5: Results and How to Work OnCreateOptionsMenu !

Click the three dots on the upper right corner.

After clicked that THREE DOTS , we see that 3 options such as profile, settings and sign out.

After clicked that settings menu, we see that backgruond clour was changed and text view changed as Profile.

After clicked that settings menu, we see that backgruond clour was changed and text view changed as Settings. We see three options and clicked sign out button.

Click Sign Out Options Menu

We click sign out button.

And then we come back to android main page after click sign out our application.

In this lesson, you learn how to onCreateOptionsMenu and onOptionsItemSelected in java.

Home > Android
 Android Options Menu Example using getMenuInflater().inflate, onCreateOptionsMenu and onOptionsItemSelected
Setting menus in OnCreateOptionsMenu()

Android Options Menu Example using getMenuInflater().inflate, onCreateOptionsMenu and onOptionsItemSelected

What is onCreateOptionsMenu(Menu menu)

Options menu and app bar: The options menu is the primary collection of menu items for an activity. It’s where you should place actions that have a global impact on the app, such as “Exit Applications,” “Profile” and “Settings.”

If you have any question write comments I will reply.

Thanks for reading my blog.

Have a nice day.

If you need Google AdMob Alternatives you can check out Start Ad Platfrom.

Regards

5S JAVA AND KOTLIN LEARNING

--

--