OptionMenuActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_pass_data_activitity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cfdb28"
tools:context="com.bhaumik.programmingstudy.OptionMenuActivitity">
</RelativeLayout>
Add Menu Resource in Resource Folder Add Android Directory and Create Menu File Xml in Menu Folder
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search"
android:title="Search">< /item>
<item
android:id="@+id/menu_share"
android:title="Share">< /item>
<item android:id="@+id/menu_about"
android:title="About">< /item>
</menu>
OptionMenuActivity.java
package com.bhaumik.programmingstudy;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class OptionMenuActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_option_menu);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.option_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.menu_search:
Toast.makeText(getApplicationContext(),"Search Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.menu_share:
Toast.makeText(getApplicationContext(),"Share Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.menu_about:
Toast.makeText(getApplicationContext(),"About Selected",Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
0 comments:
Post a Comment