Share button

Asked

Viewed 4,044 times

1

And hey guys all right? I hope so with the whole Stack community.

Well, I have a question that’s been killing me for a long time, and I can’t fix it. First of all I want to remember that I’m a beginner in Android Programming.

I want to put on my App a button or some share menu, so people can share the content of a webview.

Below follows my codes:

Android Manifest.xmml

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

<android:uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
    <activity
        android:name=".splash_screen"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:noHistory="true"
        >
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|screenSize"
        android:label="@string/title_activity_splash_screen"
        android:screenOrientation="portrait" />
    <activity android:name="radardf.radardf.error_webview"></activity>
</application>

Mainactivity.java

package radardf.radardf;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.ShareActionProvider;

import radardf.radardf.R;

public class MainActivity extends Activity {
//Faz a verificacao da conexao com a internet
//Fim da Verifica��o de conexao com a internet
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

//Botão de Compartilhamento


//Fim do compartilhamento

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WebView wv = (WebView) findViewById(R.id.webView);
        wv.setWebViewClient(new WebViewClient());
        final WebSettings ws= wv.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(false);
        //news implementation
        ws.setSaveFormData(true);
        wv.loadUrl("http://urldosite.com.br");
        wv.getSettings().setUseWideViewPort(true);
        wv.getSettings().setLoadWithOverviewMode(true);
        wv.setWebChromeClient(new WebChromeClient());


        //Barra de Progress StackOverflow
       /* ProgressDialog progress = new ProgressDialog();
        progress.setMessage("Carregando");
        progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progress.setIndeterminate(true);
        progress.show();*/


        //Barra de Progresso / Carregando
       final ProgressBar Pbar;
        Pbar = (ProgressBar) findViewById(R.id.progressBar);


        wv.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                if (progress < 100 && Pbar.getVisibility() == ProgressBar.GONE) {
                    Pbar.setVisibility(ProgressBar.VISIBLE);
                }
                Pbar.setProgress(progress);
                if (progress == 100) {
                    Pbar.setVisibility(ProgressBar.GONE);

                }
            }
        });
        //Fim da Barra de Progresso / Carregando

        //Verifica se a internet está ativa no aparelho
       /* ConnectivityManager cManager = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
        NetworkInfo ninfo = cManager.getActiveNetworkInfo();
        if(ninfo!=null && ninfo.isConnected()){
            Toast.makeText(this,"Conectado na internet", Toast.LENGTH_LONG).show();
        }else{
                //Caso não tenha internet, Recarrega a SplashScreen
                    Intent i = new Intent(MainActivity.this, splash_screen.class);
                    startActivity(i);
               //Caso não tenha internet, Recarrega a SplashScreen

            Toast.makeText(this,"Sua Internet Precisa estar Ativa. Estamos Tentando conectar...", Toast.LENGTH_LONG).show();
        }
*/

        /* Caso a pagina da web não funciona*/
        wv.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Intent i = new Intent(MainActivity.this, radardf.radardf.error_webview.class);
                startActivity(i);

            }
        });
         /* Fim:: Caso a pagina da web não funciona*/


        }

//Fecha a Aplicacao Quando pressionar o botao voltar
@Override
public void onBackPressed(){
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
   //Fecha a Aplicacao Quando pressionar o botao voltar




    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }














}

2 answers

4

To share any item just call the Intent.ACTION_SEND, for example:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
String texto = "Olá sou um texto compartilhado"
sendIntent.putExtra(Intent.EXTRA_TEXT, texto);
sendIntent.setType("text/plain");
startActivity(sendIntent);

Done this, will open the default share window of android, this varies from version to version, and can share with all registered Apps.

1


You need to use the function Shareactionproviders

Example:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
            android:id="@+id/menu_item_share"
            android:showAsAction="ifRoom"
            android:title="Share"
            android:actionProviderClass=
                "android.widget.ShareActionProvider" />
    ...
</menu>

And then in Java:

private ShareActionProvider mShareActionProvider;
...

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate menu resource file.
    getMenuInflater().inflate(R.menu.share_menu, menu);

    // Locate MenuItem with ShareActionProvider
    MenuItem item = menu.findItem(R.id.menu_item_share);

    // Fetch and store ShareActionProvider
    mShareActionProvider = (ShareActionProvider) item.getActionProvider();

    // Return true to display menu
    return true;
}

// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(shareIntent);
    }
}

And the result will be:

inserir a descrição da imagem aqui

Follow the link from Documentation of Android that explains the details of how it should be done.

  • Thanks Leonardo, The solution you sent me ended up giving 99% right, but it helped a lot. The share button appeared, but it does nothing :(, it only stays there and when I click it does not show the options

Browser other questions tagged

You are not signed in. Login or sign up in order to post.