Context Menu in Android

Create a Floating Context Menu in Android

MainCLASS

package com.example.malik.test;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {

ListView listView;
String[] android_versions;
ArrayAdapter<String> adapter;

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

listView = (ListView) findViewById(R.id.list_view);
registerForContextMenu(listView);
android_versions = getResources().getStringArray(R.array.android_versions);
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.raw_layout, R.id.row_item, android_versions);
listView.setAdapter(adapter);

}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.my_contexual_menu, menu);

}
}

MainXML

<?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: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”
tools:context=”com.example.malik.test.MainActivity”
android:id=”@+id/relative_layout”
>

<ListView
android:id=”@+id/list_view”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”></ListView>

</RelativeLayout>

 

my_contexual_menu XML

<?xml version=”1.0″ encoding=”utf-8″?>
<menu xmlns:android=”http://schemas.android.com/apk/res/android”>

<item android:title=”Delete” android:id=”@+id/id_delete”></item>

<item android:title=”Share” android:id=”@+id/id_share”></item>

<item android:title=”Help” android:id=”@+id/id_help”></item>

</menu>

 

raw_layout XML

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical” android:layout_width=”match_parent”
android:layout_height=”40dp”
android:background=”#000000″>

<TextView
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:id=”@+id/row_item”
android:text=”Hello World”
android:textColor=”#ffffff”
android:gravity=”center”
android:textAppearance=”?android:textAppearanceLarge”

/>

</LinearLayout>

 

String XML

<resources>
<string name=”app_name”>Test</string>
<string name=”action_settings”>Settings</string>
<string-array name=”android_versions”>
<item>Version 1</item>
<item>Version 2</item>
<item>Version 3</item>
<item>Version 4</item>
<item>Version 5</item>
<item>Version 6</item>
<item>Version 7</item>
<item>Version 8</item>
<item>Version 9</item>
<item>Version 10</item>
</string-array>

</resources>

Output :

pic

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.