info@thistimebd.com

Saturday 23rd of November 03:34:40pm

Option Menu Android Source Code

MainActivity.java

==================

package com.sam.web.optionmenu;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuInflater;

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) {

        MenuInflater menuInflater=getMenuInflater();

        menuInflater.inflate(R.menu.menu_layout,menu);

        return super.onCreateOptionsMenu(menu);

    }


    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId()==R.id.settingId){


            Toast.makeText(MainActivity.this,"this setting is selected",Toast.LENGTH_SHORT).show();

            return true;

        } if(item.getItemId()==R.id.shareId){


            Toast.makeText(MainActivity.this,"this share is selected",Toast.LENGTH_SHORT).show();

            return true;

        } if(item.getItemId()==R.id.feedbackId){


            Toast.makeText(MainActivity.this,"this feedback is selected",Toast.LENGTH_SHORT).show();

            return true;

        } if(item.getItemId()==R.id.aboutusId){


            Toast.makeText(MainActivity.this,"this About Us is selected",Toast.LENGTH_SHORT).show();

            return true;

        }

        return super.onOptionsItemSelected(item);

    }

}

======================================================================================


activity_main.xml

=================

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

<LinearLayout 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"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:gravity="center"

    tools:context="com.sam.web.optionmenu.MainActivity">


    <TextView

        android:textSize="20sp"

        android:textStyle="bold"

        android:textAlignment="center"

        android:text="Hellow world"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />




</LinearLayout>

================================================================================================


menu_layout.xml

==================

drawable-->new--->vector icon-->image select-->search settings icon select then ok button 


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

<menu xmlns:android="http://schemas.android.com/apk/res/android"

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


<item

    android:id="@+id/settingId"

    android:title="Setting"

    app:showAsAction="always"

    android:icon="@drawable/ic_settings_black_24dp"

    />


    <item

    android:id="@+id/shareId"

    android:title="Share"

    app:showAsAction="always"

    android:icon="@drawable/ic_share_black_24dp"

    />


    <item

    android:id="@+id/feedbackId"

    android:title="Feedback"

    app:showAsAction="never"

    />


    <item

    android:id="@+id/aboutusId"

    android:title="About Us"

    app:showAsAction="never"

    />


</menu>