Get Menu title : Menu « UI « Android






Get Menu title

  
package app.test;

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

public class Test extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
      super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater(); //from activity
        inflater.inflate(R.menu.my_menu, menu);
      return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
      appendMenuItemText(item);
      if (item.getItemId() == R.id.menu_clear)
      {
        this.emptyText();
        return true;
      }
      return true;
    }
    
    private TextView getTextView()
    {
        return (TextView)this.findViewById(R.id.text1);
    }
    public void appendText(String abc)
    {
        TextView tv = getTextView(); 
        tv.setText(tv.getText() + "\n" + abc);
    }
    
    private void appendMenuItemText(MenuItem menuItem)
    {
       String title = menuItem.getTitle().toString();
       TextView tv = getTextView(); 
       tv.setText(tv.getText() + "\n" + title);
    }
    private void emptyText()
    {
          TextView tv = getTextView();
          tv.setText("");
    }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
  android:id="@+id/text1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Your debug will appear here"
    />
</LinearLayout>
//my_menu.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This group uses the default category. -->
    <group android:id="@+id/menuGroup_Main">
        <item android:id="@+id/menu_clear"
       android:title="clear" />
       
        <item android:id="@+id/menu_testlib_1"
            android:title="Lib Test Menu1" />
            
        <item android:id="@+id/menu_testlib_2"
            android:title="Lib Test Menu2" />
    </group>
</menu>

   
    
  








Related examples in the same category

1.Create Option menu
2.Menu and messages
3.Context menu
4.Custom menu
5.Using Icon in Menu
6.Option Menu selection event
7.Menu Inflation
8.Context Menu event
9.Create Menu within your code
10.Activity Menu
11.Define menu in xml file
12.Adding submenu
13.Using Icon in menu and submenu
14.onOptionsItemSelected/onPrepareOptionsMenu/onCreateOptionsMenu Event
15.Basics of the Action Bar and how it interoperates with the standard options menu.
16.Demonstration of displaying a context menu from a fragment.
17.Demonstrates how fragments can participate in the options menu.
18.Demonstrates inflating menus from XML.
19.Usage of SearchView in an ActionBar as a menu item.
20.This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL