Button that when clicking and holding it has a sharing function

Asked

Viewed 346 times

1

I have several buttons that when clicking , emits a sound , I want to make the click and hold the button appear the sharing options , for facebook Whatsapp etc how do I do it ? , follows 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();

        }
    });
  • 3

    https://developer.android.com/reference/android/view/View.OnLongClickListener.html

  • related http://answall.com/questions/14761/click-e-segurr-o-button-listview

  • It worked out thanks! Ack

  • @Paiva abs Brow! Precise, tmj.

1 answer

0


To place an event while holding the button, just use the OnLongClickListener as you 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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.