info@thistimebd.com

Thursday 21st of November 07:04:20pm

share menu android app studio source code

MainActivity.java

================


package com.sam.web.sharemenu;


import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuInflater;

import android.view.MenuItem;

import android.widget.Toast;


import javax.security.auth.Subject;


public class MainActivity extends AppCompatActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }


    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater menuInflater=getMenuInflater();

        menuInflater.inflate(R.menu.menu_layout,menu);

        return super.onCreateOptionsMenu(menu);

    }


    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId()==R.id.shareId){


            Intent intent=new Intent(Intent.ACTION_SEND);

            intent.setType("text/plain");

            String subject="c programming app";

            String body="having app did learn programming c. com.sam.web.sharemenu";


            intent.putExtra(Intent.EXTRA_SUBJECT, subject);

            intent.putExtra(Intent.EXTRA_TEXT, body);

            startActivity(Intent.createChooser(intent," Share with"));

        }

        return super.onOptionsItemSelected(item);

    }

}

================================================================================================

activity_main.xml

==================


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

<LinearLayout 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"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:gravity="center"

    tools:context="com.sam.web.sharemenu.MainActivity">


    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Hellow"

      />


</LinearLayout>


==========================================================================================

menu_layout.xml

=================


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

<menu xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:android="http://schemas.android.com/apk/res/android">

<item

    android:id="@+id/shareId"

    android:title="@string/app_name"

    android:icon="@android:drawable/ic_menu_share"

    app:showAsAction="always" />

</menu>