Wednesday 23 September 2015

Menus 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. This guide shows how to create the three fundamental types of menus or action presentations on all versions of Android:
In Android Application Development There are three types of Menus
1 .Options Menu
2. Context Menu
3. Popup Menu (Deprecated)

Options Menu:-
Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc.
Here, we are going to see two examples of option menus. First, the simple option menus and second, options menus with images.

Here, we are inflating the menu by calling the inflate() method of MenuInflater class. To perform event handling on menu items, you need to override onOptionsItemSelected() method of Activity class
activity_main.xml
----------------
<LinearLayout

    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"

    android:orientation="vertical"

    tools:context=".MainActivity">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="Welcome to Options Menu"

        android:id="@+id/textView"

        android:textSize="20dp"

        android:layout_marginTop="50dp" />

</LinearLayout>
menu_main.xml
------------------
<menu xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

    <item android:id="@+id/action_settings" android:title="@string/action_settings"

        android:orderInCategory="100" app:showAsAction="never" />

    <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="Options4"></item>

</menu>

MainActivity.java
-------------------
package com.android.anil.optionssmenu;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;

    }



    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will

        // automatically handle clicks on the Home/Up button, so long

        // as you specify a parent activity in AndroidManifest.xml.

        switch (item.getItemId()){

            case R.id.item1:

                Toast.makeText(MainActivity.this, "Options 1 is Selected", Toast.LENGTH_SHORT).show();

                break;

            case R.id.item2:

                Toast.makeText(MainActivity.this, "Options 2 is Selected", Toast.LENGTH_SHORT).show();

                break;

            case R.id.item3:

                Toast.makeText(MainActivity.this, "Options 3 is Selected", Toast.LENGTH_SHORT).show();

                break;

            case R.id.item4:

                Toast.makeText(MainActivity.this, "Options 4 is Selected", Toast.LENGTH_SHORT).show();

                break;

            

            

        }



        return super.onOptionsItemSelected(item);

    }

}
Output is:
--------------












No comments:

Post a Comment

Encryption Decryption Example Android Using AES Alogirthm

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