Reputation: 405
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);
}
Upvotes: 4
Views: 7090
Reputation:
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>
Upvotes: 4
Reputation: 6792
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.
Upvotes: 1
Reputation: 4652
call registerForContextMenu(tv);
and pass it the view you want to attach the context menu to.
Upvotes: 0
Reputation: 4001
XML Code
<Button
android:id="@+id/btnFb"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="5dip"
android:layout_weight="1"
android:text="Button" />
Java Code
btnFb.setOnClickListener(FbListenser);
private OnClickListener FbListenser = new OnClickListener() {
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(activity.getApplicationContext(), v);
List<String> lstFb = new List<String>();
for (int i = 0; i < lstFb.size(); i++) {
popupMenu.getMenu().add(lstFb.get(i));
}
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
String Value = item.toString();
}
});
popupMenu.show();
}
};
Upvotes: 0
Reputation: 5145
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:visibility="gone"
android:textColor="#04B404" />
Now take one button and this all which u did wd text view just do same and on click of that button just do this like
Button dialogButtonCancel = (Button) findViewById(R.id.dialogButtonCancel);
dialogButtonCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
textview.setVisibility(View.VISIBLE);
}
});
Upvotes: 0