Menu List (Option Menu,Context Menu,PopUp Menu) In Android.
Step :-1 Create New Project
Step : -2 Create New menu.xml file
MainActivity.java
package com.example.sunny.menus;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
private ActionMode actionMode=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void PopUpMenu(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.menu);
popup.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.setting:
Toast.makeText(MainActivity.this,"Clicked Setting",Toast.LENGTH_SHORT).show();
mode.finish();
return true;
case R.id.logout:
Toast.makeText(MainActivity.this,"Clicked Logout",Toast.LENGTH_SHORT).show();
mode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
actionMode = null;
}
};
public void ActionMode(View view){
TextView click=findViewById(R.id.conttext_menu);
click.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View view) {
if (actionMode != null) {
return false;
}
actionMode = MainActivity.this.startActionMode(actionModeCallback);
view.setSelected(true);
return true;
}
});
menu.xml }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.search_item:
// do your code
return true;
case R.id.upload_item:
// do your code
activity_main.xml return true;
case R.id.copy_item:
// do your code
return true;
case R.id.print_item:
// do your code
return true;
case R.id.share_item:
// do your code
return true;
case R.id.bookmark_item:
// do your code
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.search_item:
// do your code
return true;
case R.id.upload_item:
// do your code
activity_main.xml return true;
case R.id.copy_item:
// do your code
return true;
case R.id.print_item:
// do your code
return true;
case R.id.share_item:
// do your code
return true;
case R.id.bookmark_item:
// do your code
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/menu2"
tools:context=".MainActivity">
<Button
android:id="@+id/conttext_menu"
android:layout_width="150dp"
android:layout_height="83dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="17dp"
android:layout_marginLeft="17dp"
android:layout_marginTop="233dp"
android:onClick="ActionMode"
android:background="#0080ff"
android:text="Context Menu"
android:textSize="20dp" />
<Button
android:id="@+id/popup_menu"
android:layout_width="150dp"
android:layout_height="83dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="288dp"
android:onClick="PopUpMenu"
android:background="#0080ff"
android:text="Pop Up Menu"
android:textSize="20dp" />
</RelativeLayout>
menu.xml
<?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:title="Setting" android:id="@+id/setting">
</item>
<item android:id="@+id/logout" android:title="Logout" app:showAsAction="never"></item>
<item android:id="@+id/search_item"
android:title="Search" />
<item android:id="@+id/upload_item"
android:title="Upload" />
<item android:id="@+id/copy_item"
android:title="Copy" />
<item android:id="@+id/print_item"
android:title="Print" />
<item android:id="@+id/share_item"
android:title="Share" />
<item android:id="@+id/bookmark_item"
android:title="BookMark" />
</menu>
OUTPUT
Android Menu Program in Menu
Android Menu Program on Option Menu
After Click Option Menu Show......
Android Menu Program on Option MenuAfter Click Context Menu Show.........
Android Menu Program on PopUp Menu
No comments:
Post a Comment
Please do not any spam link in Comment Box