Pages

Saturday, May 21, 2016

Menu’s in Android

Menus are a common user interface component in many types of applications. To provide a familiar and consistent user experience, you should use the Menu APIs to present user actions and other options in your activities.
Beginning with Android 3.0 (API level 11), Android-powered devices are no longer required to
provide a dedicated Menu button. With this change, Android apps should migrate away from a
dependence on the traditional 6-item menu panel and instead provide an action bar to present
common user actions.
Although the design and user experience for some menu items have changed, the semantics to define a set of actions and options is still based on the Menu APIs.
Menu’s are 3 types:
  1. Optoins Menu
  2. Context Menu
  3. PopUp Menu
Options Menu:
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 “Search,” “Compose email,” and “Settings.”
Context Menu:
A context menu is a floating menu that appears when the user performs a long-click on an
element. It provides actions that affect the selected content or context frame.
PopUp Menu:
A popup menu displays a list of items in a vertical list that’s anchored to the view that
invoked the menu. It’s good for providing an overflow of actions that relate to specific content
or to provide options for a second part of a command. Actions in a popup menu should not
directly affect the corresponding content—that’s what contextual actions are for. Rather, the
popup menu is for extended actions that relate to regions of content in your activity.

Create Manu Folder under res
items.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/item1"

        android:title="Option1">
    </item>
    <item
        android:id="@+id/item2"

        android:title="Option2">
    </item>
    <item
        android:id="@+id/item3"

        android:title="Option3">
    </item>
    <item
        android:id="@+id/item4"

        android:title="Option4">
    </item>

</menu>
contextitems.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/item5"
        android:title="Option5">
    </item>
    <item
        android:id="@+id/item6"
        android:title="Option6">
    </item>
    <item
        android:id="@+id/item7"
        android:title="Option7">
    </item>
    <item
        android:id="@+id/item8"
        android:title="Option8">
    </item>

</menu>
activity_main.xml
<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=".MainActivity" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="68dp"
        android:text="Context Menu" />

</RelativeLayout>
ManiActivity.java
package com.blogspot.kantapp.menusinandroid;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

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

        Button contextButton = (Button) findViewById(R.id.button);

        registerForContextMenu(contextButton);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub

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

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {
            case R.id.item1:
                Toast.makeText(getApplicationContext(), "option1 selected", Toast.LENGTH_SHORT).show();

                break;

            case R.id.item2:
                Toast.makeText(getApplicationContext(), "option2 selected", Toast.LENGTH_SHORT).show();

                break;

            case R.id.item3:
                Toast.makeText(getApplicationContext(), "option3 selected", Toast.LENGTH_SHORT).show();

                break;

            case R.id.item4:
                Toast.makeText(getApplicationContext(), "option4 selected", Toast.LENGTH_SHORT).show();

                break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        getMenuInflater().inflate(R.menu.contextitems, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
            case R.id.item5:
                Toast.makeText(getApplicationContext(), "option5 selected", Toast.LENGTH_SHORT).show();

                break;

            case R.id.item6:
                Toast.makeText(getApplicationContext(), "option6 selected",       Toast.LENGTH_SHORT).show();

                break;

            case R.id.item7:
                Toast.makeText(getApplicationContext(), "option7 selected", Toast.LENGTH_SHORT).show();

                break;

            case R.id.item8:
                Toast.makeText(getApplicationContext(), "option8 selected", Toast.LENGTH_SHORT).show();

                break;
        }
        return super.onContextItemSelected(item);
    }

}

No comments:

Post a Comment