Android App Development With AndroidCource: Context Menu
Showing posts with label Context Menu. Show all posts
Showing posts with label Context Menu. Show all posts

Friday, 15 September 2017

Android Context Menu

Context Menu


Context menu appears when user press long click on the element.

A context menu is a floating menu.

It provides actions that affect the selected content or context frame.

It doesn't support item shortcuts and icons.

Monday, 21 August 2017

Context Menu Example in Android

ContextMenuActivity.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:id="@+id/activity_context_menu"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:orientation="vertical"
              tools:context="com.bhaumik.programmingstudy.ContextMenuActivity">

    <ListView
            android:layout_marginTop="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/list_view"></ListView>

</LinearLayout>
ContextMenuActivity.java
package com.bhaumik.programmingstudy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ContextMenuActivity extends AppCompatActivity {

    ListView listView;
    ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_context_menu);

        listView = (ListView) findViewById(R.id.list_view);

        adapter = ArrayAdapter.createFromResource(this,R.array.str_language,android.R.layout.simple_list_item_1);
        listView.setAdapter(adapter);

        registerForContextMenu(listView);

    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Select The Action");
        menu.add(0,v.getId(),0,"Remove Item");
        menu.add(0,v.getId(),0,"Hide Item");
        menu.add(0,v.getId(),0,"Show Item");
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {

        if (item.getTitle() == "Remove Item")
        {
            Toast.makeText(this,"Remove Item Clicked",Toast.LENGTH_SHORT).show();
        }
        else if (item.getTitle() == "Hide Item")
        {
            Toast.makeText(this,"Hide Item Clicked",Toast.LENGTH_SHORT).show();
        }
        else if (item.getTitle() == "Show Item")
        {
            Toast.makeText(this,"Show Item Clicked",Toast.LENGTH_SHORT).show();
        }
        return true;
    }

}

Ads