How to create simple android share button

How to create simple android Share button

Today we are going to learn how to create a simple android share button.

 Lessons to learn:

  • Adding Toolbar
  • Using WebView 
  • Creating android menu
  • Using onOptionsItemSelected method to listen to selected MenuItems.

Let get started
Create a new project and name it "EasyShareExample"
Package name :net.techoverload.www.easyshareexample
Add some drawables i.e Notification,settings and share 


activity_main.xml code



    

    




MainActivity.java code

   
package net.techoverload.www.easyshareexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;


public class MainActivity extends AppCompatActivity {
    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar=findViewById(R.id.my_toolbar);
        setSupportActionBar(toolbar);

        webView=findViewById(R.id.webView);
        WebSettings webSettings=webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl("http://www.mafisizone.com");

    }

    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.top_nav,menu);

        return  true;
    }

    public  boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.top_nav_share:

            {
                String tittle=webView.getTitle();
                String url=webView.getUrl();
//sharing implementation here
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, tittle);
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, url);
                startActivity(Intent.createChooser(sharingIntent, "Share via"));

                return true;
            }

        }

        return  true;
    }



}


Menu.xml code



    
    
    




Output


Download Source Code


COMMENTS

Name

android android Apps android emulators android.app antivirus arrays blogger bluestack Break Statement in Java constructors download downloader drivers emulators fragment java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql node.js php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: How to create simple android share button
How to create simple android share button
How to create simple android Share button
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9FE5czmH0Eqbdq67aA1lYendYHX7zisix2rrdJgCyANM7FEc1GeH_r46vOKJCszVS5F7BET75FkwtLgQsL7BpEll-jmXSLhyphenhyphenFeTSoa5Iqu2vU8EhStzIbZtHWhvKTIsiOPw6rQDROEdpQ/s320/Screenshot_2018-03-27-19-21-50.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9FE5czmH0Eqbdq67aA1lYendYHX7zisix2rrdJgCyANM7FEc1GeH_r46vOKJCszVS5F7BET75FkwtLgQsL7BpEll-jmXSLhyphenhyphenFeTSoa5Iqu2vU8EhStzIbZtHWhvKTIsiOPw6rQDROEdpQ/s72-c/Screenshot_2018-03-27-19-21-50.png
Java Codes
https://download-all-stuff.blogspot.com/2018/03/how-to-create-simple-android-share.html
https://download-all-stuff.blogspot.com/
http://download-all-stuff.blogspot.com/
http://download-all-stuff.blogspot.com/2018/03/how-to-create-simple-android-share.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy