How can I make my app to show Context menu on Long item Click? I have written this code and it's working. How can I make it so it shows when I click on some button?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:textColor="#04B404" />
Code:
TextView tv=(TextView)findViewById(R.id.tv);
tv.setOnCreateContextMenuListener(this);
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuinfo) {
super.onCreateContextMenu(menu, view, menuinfo);
menu.setHeaderTitle("Set as");
menu.add(menu.FIRST, Menu.NONE, 0, "Set as Wallpaper");
menu.add(menu.FIRST+1, Menu.NONE, 0, "Download");
menu.add(menu.FIRST+2, Menu.NONE, 0, "Info);
}
Try this Code:
public class MainActivity extends ListActivity {
private String[] items;
private List<String> list;
private ArrayAdapter<String> adapter;
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
items = new String[] {"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"};
list = new ArrayList<String>();
Collections.addAll(list, items);
adapter = new ArrayAdapter<String>(this, R.layout.row,
R.id.r_text, list);
setListAdapter(adapter);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater m = getMenuInflater();
m.inflate(R.menu.our_context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.delete_item:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
position = (int) info.id;
list.remove(position);
this.adapter.notifyDataSetChanged();
return true;
}
return super.onContextItemSelected(item);
}
}
Activity.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:textIsSelectable="true"
android:id="@+id/r_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textColor="#ffffff"
android:textSize="17sp"/>
Now create a menu folder in res and put this file in it.
Context Menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/delete_item"
android:title="@string/delete_string"/>
</menu>
You can use registerForContextMenu(View v) method which takes the View on which you want to register you ContextMenu.
So if your Button is called myButton,
registerForContextMenu(myButton);
So if you wish to have it on long click just add onLongClickListener and register your button using the above.
Heres an example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With