Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a menu programmatically in Android?

Now I want to add it a menu on the bottom of the screen. I wrote a lot of about but still didnt get how to do that. My main problem is that I dont have an xml file on my main page. its look liks that:

   public class start  extends ListActivity {
        static final String[] COUNTRIES = new String[] {
        "NEWS1", "NEWS2","RADIO"};
 Intent intent;
 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, COUNTRIES));

      ListView lv = getListView();
      lv.setTextFilterEnabled(true);

      lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
if (((TextView) view).getText().equals("NEWS1")){
 intent = new Intent(start.this,  NewsActivity.class);

how can I add a menu with an action. please give me a example. thanks

like image 765
Vitaly Menchikovsky Avatar asked Sep 06 '11 11:09

Vitaly Menchikovsky


1 Answers

use this code to add menu dynamically

private static final int NEW_MENU_ID=Menu.FIRST+1;

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, NEW_MENU_ID, 0, "New"); 

        return true;
    }
like image 136
ilango j Avatar answered Oct 24 '22 17:10

ilango j