Menu in Android Tutorial

Popup Menu Example

         

         user_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:id="@+id/first"
android:title="First"
app:showAsAction="never" />
< item
android:id="@+id/second"
android:title="Second"
app:showAsAction="never" />
< item
android:id="@+id/third"
android:title="Third"
app:showAsAction="never" />
< /menu>

           activity_menu_demo.xml
< ?xml version="1.0" encoding="utf-8"?>
< 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">
< TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:id="@+id/textdata"
android:text="Hello Friends"/>

< /LinearLayout>

package example.com.serverapp;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

public class MenuDemo extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_demo);
tv = findViewById(R.id.textdata);

tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu pm=new PopupMenu(MenuDemo.this,tv);
pm.getMenuInflater().inflate(R.menu.user_menu,pm.getMenu());
pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
int menuitem_id = menuItem.getItemId();
switch (menuitem_id){
case R.id.first:
Toast.makeText(MenuDemo.this, "First", Toast.LENGTH_SHORT).show();
break;
case R.id.second:
Toast.makeText(MenuDemo.this, "Second", Toast.LENGTH_SHORT).show();
break;
case R.id.third:
Toast.makeText(MenuDemo.this, "Third", Toast.LENGTH_SHORT).show();
break;
}

return false;
}
});
pm.show();
}
});
}

}
_______________________________________________________________________

           Context Menu Example

Layout same as above and Menu also.

package example.com.serverapp;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

public class MenuDemo extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_demo);
tv = findViewById(R.id.textdata);
registerForContextMenu(tv);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.user_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int menuitem_id = item.getItemId();
switch (menuitem_id){
case R.id.first:
Toast.makeText(this, "First", Toast.LENGTH_SHORT).show();
break;
case R.id.second:
Toast.makeText(this, "Second", Toast.LENGTH_SHORT).show();
break;
case R.id.third:
Toast.makeText(this, "Third", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}

         __________________________________________________________________

         Option Menu Example 

Layout same as above and Menu also.

package example.com.serverapp;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

public class MenuDemo extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_demo);
tv = findViewById(R.id.textdata);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.user_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int menuitem_id = item.getItemId();
switch (menuitem_id) {
case R.id.first:
Toast.makeText(this, "First", Toast.LENGTH_SHORT).show();
break;
case R.id.second:
Toast.makeText(this, "Second", Toast.LENGTH_SHORT).show();
break;
case R.id.third:
Toast.makeText(this, "Third", Toast.LENGTH_SHORT).show();
break;
}
return true;
}

}

          For Video click me.

No comments: