Search code examples
androidandroid-sharing

Android: SharingActionView on swipe from bottom


I would like to share data with other Apps, but I don't want the standard popup which appears bottom tool bar; I want this kind of view: enter image description here

I followed the official document: https://developer.android.com/training/sharing/shareaction.html

So is there a native component to do that in Android SDK? Or is there any library to do that?

thank for your help guys!


Solution

  • You can do it like this :

    First create a menu item :

    <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="com.example.rashish.myapplication.MainActivity">
        <item
            android:id="@+id/action_share"
            android:orderInCategory="100"
            android:title="@string/action_settings"
            android:icon="@android:drawable/ic_menu_share"
            app:showAsAction="always" />
    </menu>
    

    Then create a method to show the Intent chooser dialog, like this :

    private void share() {
        String textToShare = "hello";
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        // set the type here i.e, text/image/ etc.
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
        sharingIntent.putExtra(Intent.EXTRA_TEXT, textToShare);
        startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }
    

    Then in the onOptionsItemSelected method, call this function when the menu item is clicked :

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. 
        int id = item.getItemId();
    
        if (id == R.id.action_share) {
            share();
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    Also make sure you've overriden onCreateOptionsMenu in your Activity like this :

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    

    Here's the output :

    screenshot from an android device