13 Context Menu



Algorithm:
Step 1) Create a new Android Studio Project

Step 2)Goto Project Structure and open activity_main.xml file

Step 3) Create menu Android resource Directory and inside menu folder create menu resource file.

Step 4) open menu resource file and create a list of items you want to display in the context menu as options.
Step 5) Implement the following methods

@Override

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
//code

}



@Override

public boolean onContextItemSelected(MenuItem item){

}


Step 6) Run the application from the menu bar select Run and Click on Run App option.


activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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" tools:context="com.example.contextmenu.MainActivity">

<ListView android:layout_width="368dp" android:layout_height="495dp" android:id="@+id/listView" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<item android:id="@+id/call" android:title="Call" />
<item android:id="@+id/sms" android:title="SMS" />
</menu>














MainActivity.java
package com.example.contextmenu;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.ContextMenu; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast;


public class MainActivity extends AppCompatActivity { ListView listView;
String contacts[]={"venkatesh", "Nagarjun", "AmeerMalik", "Jhansi", "Roshan"};
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView)findViewById(R.id.listView); ArrayAdapter<String> adapter=new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contacts); listView.setAdapter(adapter);
// Register the ListView for Context menu registerForContextMenu(listView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_main, menu); menu.setHeaderTitle("Select The Action");
}
@Override
public boolean onContextItemSelected(MenuItem item){ if(item.getItemId()==R.id.call){
Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();
}
else if(item.getItemId()==R.id.sms){
Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();
}else{
return false;
}
return true;
}
OUTPUT:




Comments

Popular posts from this blog

20 Simple Content Provider Example

14 Popup Menu

17 Simple Notification Program