Setting ContextMenu on SetOnLongClickListener - android

How can I set my app to show Context Menu on Long Click Listener?
I want it when I click to play sound, and on long click to show menu with some options and buttons, on 1 same button.
So I want o normal click to play sound, on long click to show menu with some buttons which I can use to set as ringtone and other stuff.
mp=MediaPlayer.create(this, R.raw.hekler);
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp2.isPlaying()){
mp2.pause();
mp2.seekTo(0);
}
else{
mp.start();
}
}
});
btn1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
return true;
}
});

To show a context menu on long click, you should call registerForContextMenu(View) for view that is clicked.
You should also override onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo)
You not need setOnLongClickListener. If you need it for some other reason, it should return false.
In your code:
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
registerForContextMenu(btn1);
btn1.setOnClickListener(new View.OnClickListener() {
-------------
-------------
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_context_menu, menu);
}
To handle the context menu:
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.contextItem1:
//Do what you want
return true;
case R.id.contextItem2:
//Do what you want
return true;
default:
return super.onContextItemSelected(item);
}
}

Related

Android: MenĂ¹ popup with long click?

I need to implement a menĂ¹ that appears with a long click around a button, so the user can choose with option he wants to take simply sliding toward a certain direction. Is there any way to do that? I have just a setOnLongClickListener with a onLongClick method for now.
btn01.setOnLongClickListener(new View.OnLongClickListener(){
public boolean onLongClick (View view) {
Toast.makeText(getApplicationContext(),"Button 01 long clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
In your Activity :
btn01.setOnLongClickListener(new View.OnLongClickListener(){
public boolean onLongClick (View view) {
registerForContextMenu(btn01);
openContextMenu(btn01);
return true;
}
});
#Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
//Context menu
menu.setHeaderTitle("My Context Menu");
menu.add(Menu.NONE, CONTEXT_MENU_VIEW, Menu.NONE, "Add");
menu.add(Menu.NONE, CONTEXT_MENU_EDIT, Menu.NONE, "Edit");
menu.add(Menu.NONE, CONTEXT_MENU_ARCHIVE, Menu.NONE, "Delete");
}
#Override
public boolean onContextItemSelected (MenuItem item){
switch (item.getItemId()) {
case CONTEXT_MENU_VIEW: {
}
break;
case CONTEXT_MENU_EDIT: {
// Edit Action
}
}
}

Focus ListView Items on Button Click

Is it possible to focus the List View Items through any button click?
Like i want that when user click on Floating Action Button then the listview gets focused. I dont want to show checkbox type layout on button clicked. I just want to show the same like in the screenshot on Button click.
What i did is I put the listview onItemLongClick code in the button click blocks but it doesnot work.
fabButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
listViewMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listViewMessages.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
tv.setText(listViewMessages.getCheckedItemCount()+ " Selected");
}
#Override
public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu:
}
});
mode.finish();
return true;
default:
return false;
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.contextual, menu);
fabButton.setVisibility(View.INVISIBLE);
fabButtonn.setVisibility(View.VISIBLE);
fabButtonn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for ( int i=0; i< messageListAdapter.getCount(); i++ ) {
listViewMessages.setItemChecked(i, true);
}
}
});
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
fabButton.setVisibility(View.VISIBLE);
fabButtonn.setVisibility(View.GONE);
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});
With this code if user clcik on button then he/she has too long press items again to focus the list items which is not what i want. I want to focus items right when button click. Any explanation or link provided will be helpful

Share music on messenger

Hi i made aplication(soundboard)
I have the buton when i click(OnClick) is plays music, when i hold button(longClick) is opens ContextMenu. In ContextMenu i have button. Now i need add the action for this button(button in ContextMenu) "share this music on messneger". Can help me someone?
Java code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button contextMenuButton = (Button) findViewById(R.id.smiech);
registerForContextMenu(contextMenuButton);
final MediaPlayer smiech = MediaPlayer.create(this, R.raw.smiech);
Button playsmiech = (Button) this.findViewById(R.id.smiech);
playsmiech.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
smiech.start();
}
});
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.menu_main,menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item_option1:
break;
case R.id.item_option2:
break;
case R.id.item_option3:
break;
}
return super.onContextItemSelected(item);
}

Checking if a button has been clicked

How to load menu depend on button was clicked? Any idea, solution?
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
menu.clear();
if(button.**isclicked**) {
getMenuInflater().inflate(R.menu.menu_main, menu);
} else {
getMenuInflater().inflate(R.menu.test, menu);
}
return true;
}
Put a boolean field in your activity and change whenever you click button . Depends if its true or false show or not your menu
UPDATE
//declare boolean
boolean clicked=false;
//my button clic
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
//change boolean value
clicked=true;
}
});
//then on another method or where you want
if(clicked)
{
openmenu();
}
else
{
closemenu();
}
try this function
button.isPressed();
Try something like this:
get the button's id
Button button = (Button) findViewById(R.id.button_send);
Adding click event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do whatever you want
}
});
THe below also work fine.
if(button.isSelected())

EditText setOnCreateContextMenuListener handle result

I have set setOnCreateContextMenuListener to edittext. onCreateContextMenu method is called user long press on edittext. and it opens context menu with 'done' and 'copy' options.
But my question is how can I handle when user select done option or copy options?
Can I get any event when user click on done button or copy button. so I can get selected text via clip manager?
edit.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
Log.i("TAG", "onCreateContextMenu");//it is printing while context menu is created.
}
});
Thanks.
onContextItemSelected is the event you should use. but it has no View argument to access selected item. Here is a trick to access to selected View.
public class MainActivity extends Activity
{
protected View selectedView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)findViewById(R.id.lv1);
registerForContextMenu(lv);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
this.selectedView = v;
super.onCreateContextMenu(menu, v, menuInfo);
}
#Override
public boolean onContextItemSelected(MenuItem item)
{
switch(item.getId())
{
case R.id.Context_Edit:
// access to view with this.selectedView
break;
case R.id.Context_Delete:
// access to view with this.selectedView
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
}
You can get your both button event like following example
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)findViewById(R.id.lv1);
registerForContextMenu(lv);
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.lv1)
{
menu.setHeaderTitle("Select Action");
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 0, "Delete");
}
}
#Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if(item.getTitle() == "Edit")
{
// do something
}
if(item.getTitle() == "Delete")
{
// do something
}
return true;
}
Hope it will help you.
You must to override the onLongClick of the editText and setting one CAB. This is example:
private ActionMode.Callback action = new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == android.R.id.copy){
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
Log.i("CAB",String.valueOf(clipboard.getPrimaryClip().getItemAt(0).getText()));
}
return true;
}
};
private EditText edtText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtText = (EditText) findViewById(R.id.edtText1);
registerForContextMenu(edtText);
edtText.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
((EditText)v).setTextIsSelectable(true);
((EditText)v).setCustomSelectionActionModeCallback(action);
return false;
}
});
}

Categories

Resources