Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday 25 May 2016

Android Marshmallow Direct Share

Hello Dear

actvity_main.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/colorPrimary"
        android:elevation="4dp"
        android:minHeight="?android:attr/actionBarSize"
        android:popupTheme="@android:style/ThemeOverlay.Material.Light"
        android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="@string/explanation"
        android:textAppearance="@android:style/TextAppearance.Material.Body1" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray" />

    <EditText
        android:id="@+id/body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:hint="@string/text_to_share"
        android:text="@string/hello" />

    <Button
        android:id="@+id/share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:text="@string/share" />

</LinearLayout>


MainActivity.java


public class MainActivity extends Activity {

    private EditText mEditBody;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setActionBar((Toolbar) findViewById(R.id.toolbar));
        mEditBody = (EditText) findViewById(R.id.body);
        findViewById(R.id.share).setOnClickListener(mOnClickListener);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.share:
                    share();
                    break;
            }
        }
    };

    /**
     * Emits a sample share {
@link Intent}.
     */
   
private void share() {
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, mEditBody.getText().toString());
       startActivity(Intent.createChooser(sharingIntent, getString(R.string.send_intent_title)));
   
}

}

AndroidMainefest.xml


<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.directshare"
    android:versionCode="1"
    android:versionName="1.0">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/DirectShareTheme">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".SendMessageActivity"
            android:label="@string/app_name"
            android:theme="@style/DirectShareDialogTheme">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
            <meta-data
                android:name="android.service.chooser.chooser_target_service"
                android:value=".SampleChooserTargetService" />
        </activity>

        <activity
            android:name=".SelectContactActivity"
            android:label="@string/app_name"
            android:theme="@style/DirectShareDialogTheme" />

        <service
            android:name=".SampleChooserTargetService"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
            <intent-filter>
                <action android:name="android.service.chooser.ChooserTargetService" />
            </intent-filter>
        </service>

    </application>

</manifest>

Thank you

Full Source Code MarshmallowDirectShare


Live Sample








No comments:

Post a Comment