Skip to main content

How to add a menu in a programmatic manner in Android

Hello Friends.

Follow Below Step for Create Programmatic Menu in Android.
Here Use onCreateOptionsMenu(Menu menu) Override method.

1) First Create Simple Android Project (menudemo)

2) Now edit string.xml(Path:- res/values/string.xml)

  <?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="hello">Hello World, MenudemoActivity!</string>
    <string name="app_name">Menudemo</string>
    <string name="HomeOption">Home</string>
<string name="Oak">a@k</string>
</resources>

Here above Code for Manualy create String Home and a@k.

3) Edit  MenudemoActivity.java (this is Android Activity)

 import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MenudemoActivity extends Activity {
    /** Called when the activity is first created. */
 int id_menu = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    menu.add(R.string.HomeOption);
    menu.add(R.string.Oak);
    menu.add("Welcome");
    return super.onCreateOptionsMenu(menu);
    }
}


Here in Above Code  onCreateOptionsMenu(Menu menu)  override method use for create menu. then use add method for add string from string.xml or manualy like "welcome"
4) Now Run Android Application.

 See in Emulator after Run project Click on Menu Button and See Below Output.

Comments

Popular posts from this blog

Login Application using Database with dialog demo

Follow Below Steps for make simple login applicaiton in Android using database with Dialog demo. 1)DataBaseDemoActivity.java This file diplay login page and trasfer two edit text data into second activity........  package database.co.in; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class DataBaseDemoActivity extends Activity implements OnClickListener {     /** Called when the activity is first created. */     EditText mtxt_email, mtxt_password;     Button mLogin; @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         mtxt_email = (EditText)findViewById(R.id.editEmail_id);   ...

Audio Recording in Android

Simple State Diagram for Media Recorder... Here Sequence of Method call is as per state diagram of media Recorder .... Otherwise Invalid sate call Error will be Occur........ Steps: 1) Also her two permission add in Android manifest file <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 2) This is java file coding  audiorecording.java package com.example.recording; import java.io.File; import java.io.FileDescriptor; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import ...

Android Activity

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with  setContentView(View) . While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with  windowIsFloating  set) or embedded inside of another activity (using  ActivityGroup ). There are two methods almost all subclasses of Activity will implement: onCreate(Bundle)  is where you initialize your activity. Most importantly, here you will usually call  setContentView(int)  with a layout resource defining your UI, and using  findViewById(int)  to retrieve the widgets in that UI that you need to interact with programmatically. onPause()  is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point...