Button that when clicking and holding it has a sharing function

1

I have several buttons that when clicking, emits a sound, I want to make clicking and holding the button appear sharing options, for facebook whatsapp etc how do I do this?, follow my button code

            ImageButton sehloiro = (ImageButton) findViewById(R.id.sehloiro);
            sehloiro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            MediaPlayer mp = MediaPlayer.create(MainActivity.this, 
            R.raw.sehloiromp3);
            mp.start();

        }
    });
    
asked by anonymous 05.04.2017 / 19:38

1 answer

0

To put an event while holding the button, you only have to use OnLongClickListener as quoted @Luc in the comment. See how it can be done

sehloiro.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        showDialogShared(); // esse é o método para chamar o dialogo
        return true;
   }
);

So you can create a AlertDialog to show the sharing option:

public void showDialogShared() {
    AlertDialog.Builder builderSingle = new AlertDialog.Builder(this);

    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.select_dialog_item);
    arrayAdapter.add("Compartilhar");

    builderSingle.setAdapter(
            arrayAdapter,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String strName = arrayAdapter.getItem(which);
                    if (strName.equals("Compartilhar")) {
                        // aqui você coloca o código de compartilhamento
                    }
                }
            });
    builderSingle.show();
}
    
05.04.2017 / 20:40