Share music on messenger - android

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);
}

Related

Playing a sound file through a menu item

I am trying to play a sound file through a menu item which exists in a fragment.
Here is the full code of my fragment,
public class OneFragment extends Fragment {
public OneFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_one, container,false);
ImageButton popupCTelevision = (ImageButton)rootview.findViewById(R.id.chineseTelevision);
ImageButton popupCBathe = (ImageButton)rootview.findViewById(R.id.chineseBathe);
ImageButton popupCSick = (ImageButton)rootview.findViewById(R.id.chineseSick);
popupCTelevision.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(getActivity().getApplicationContext(), v);
popupMenu.inflate(R.menu.menu_chinese_television);
popupMenu.show();
}
});
popupCBathe.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(getActivity().getApplicationContext(), v);
popupMenu.inflate(R.menu.menu_chinese_bathe);
popupMenu.show();
}
});
popupCSick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(getActivity().getApplicationContext(), v);
popupMenu.inflate(R.menu.menu_chinese_sick);
popupMenu.show();
}
});
return rootview;
}
public boolean onMenuItemClick(MenuItem item){
switch(item.getItemId()) {
case R.id.c_bathe:
final MediaPlayer CSick = MediaPlayer.create(getActivity(), R.raw.c_bathe);
MenuItem playCSick = (MenuItem) getView().findViewById(R.id.c_sick);
CSick.start();
playCSick.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
CSick.start();
return true;
}
});
default:
return false;
}
}
}
I think the issue is that the method onMenuItemClick is not used at all. How do I implement this method for the code?
How do I do it similarly to pressing a button to play the sound file?
final MediaPlayer englishSoundMP = MediaPlayer.create(this, R.raw.english_bath);
Button playEnglishBath = (Button) this.findViewById(R.id.play_english);
playEnglishBath.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
englishSoundMP.start();
}
});

Context menu single click Android

I'm new to the Android development. I was trying to add a context menu to my app. I understood that by default it requires long click on the button to open the context menu. But I need to make it to appear on the single click. I tried all the other solutions here in stackoverflow but none of them are really helping me.
I have posted my code below. kindly let me know what are the modifications to be done to make it working.
public class ThirdActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
confirmButton = (Button) findViewById(R.id.confirmButton);
registerForContextMenu(confirmButton);
}
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select Menu");
menu.add(0, v.getId(), 0, "Action 1");
}
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Action 1") {
//do something
}
}
just :
public class ThirdActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
confirmButton = (Button) findViewById(R.id.confirmButton);
confirmButton .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
confirmButton .performLongClick();
}
});
registerForContextMenu(confirmButton);
}
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select Menu");
menu.add(0, v.getId(), 0, "Action 1");
}
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Action 1") {
//do something
}
}
You can simply write:
confirmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view.showContextMenu();
}
});

How do I open ContextMenu with a Button

I'd like to show a context_menu when a user click on a Button but I'm encountering some problems. For some reason the menu is not appearing, maybe there's something I'm missing. Can you help me please?
That's my code:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnCreateContextMenuListener(this);
registerForContextMenu(btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.showContextMenu();
}
});
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
int i = item.getItemId();
if (i == R.id.share) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Leggi");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.chooser_title)));
return true;
} else {
return onContextItemSelected(item);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
and this is my context_menu in R.menu:
<menu 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"
tools:context=".MainActivity">
<item android:id="#+id/share"
android:title="Condividi"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="#+id/web"
android:title="Mostra articolo"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="#+id/add"
android:title="Aggiungi nei Preferiti"
android:orderInCategory="100"
app:showAsAction="never" />
Change the app:showAsAction="never" to android:showAsAction="never" perhaps ?
(or just remove it).
You can use openContextMenu instead of showContextMenu.
registerForContextMenu(btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openContextMenu(v);
}
});
you can try this code
code:
public class MainActivity extends Activity {
Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.Button1);
registerForContextMenu(btn);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btn.showContextMenu();
}
});
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0,1,0,"copy");
menu.add(0,2,0,"paste");
super.onCreateContextMenu(menu, v, menuInfo);
}
}

Setting ContextMenu on SetOnLongClickListener

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);
}
}

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