Quiero agregar el botón "Compartir" a mi aplicación de Android.
Como eso

Agregué el botón "Compartir", pero el botón no está activo. Hago clic, pero no pasa nada.
Mi código en MainActivity.java:
private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share_menu, menu);
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.share_menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share_menu).getActionProvider();
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return true;
}
{
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
Quiero compartir texto en mi primera pestaña (first_tab.xml) o segunda pestaña (second_tab.xml).
Código en la pestaña (xml) (si es necesario):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity$DummySectionFragment" >
<TextView
android:id="@+id/section_label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/text"
android:textColor="@color/text_color" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/sprite" />

Respuestas:
Agregue un
Buttony al hacer clic enButtonagregar este código:Enlaces útiles:
Para compartir básico
Para personalización
fuente
shareícono en mi barra de acciónCree un botón con un id compartido y agregue el siguiente fragmento de código.
El fragmento de código anterior abrirá el selector de compartir en la acción de clic del botón de compartir. Sin embargo, tenga en cuenta ... Es posible que el fragmento de código compartido no produzca muy buenos resultados con el emulador. Para obtener resultados reales, ejecute el fragmento de código en el dispositivo Android para obtener resultados reales.
fuente
en kotlin:
fuente