Sigmar Gabriel
Sigmar Gabriel

Reputation: 23

Android Studio - Long Click On A Button

I have an Button and when I click it it plays a Sound. When I hold the Button for a second, I want that a menu will open where I can select where the sound should be shared (whatsapp...).

Does anybody know how this works?

Upvotes: 1

Views: 2819

Answers (3)

Usman Ali
Usman Ali

Reputation: 433

First you implement a Long pressed click listener like this

       button.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
    String audioFilePath=Environment.getExternalStorageDirectory().getPath()+"/pathoffile.ogg";
    Uri uri= Uri.parse(audioFilePath);
    Intent share= new Intent(Intent.ACTION_SEND);
    share.setType("audio/*");
    share.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(share, "Share The Audio")); 
            return true;
        }
    });

When this function is run It will open all the sharing applications in your mobile will open like whatsapp, bluetooth,shareit, zapya etc etc

Upvotes: 0

Sahil Garg
Sahil Garg

Reputation: 262

You can use following listener and put your code inside it.

button.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            return false;
        }
    });

Upvotes: 2

Febi M Felix
Febi M Felix

Reputation: 2839

Try this code

        String audioFilePath    = Environment.getExternalStorageDirectory().getPath() + "/yourfolder/youraudiofile.ogg";
        Uri uri                 = Uri.parse(audioFilePath);
        Intent share            = new Intent(Intent.ACTION_SEND);
        share.setType("audio/*");
        share.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(share, "Share Audio File")); 

Upvotes: 2

Related Questions