Partage
  • Partager sur Facebook
  • Partager sur Twitter

Bouton de partage Android Studio

Sujet résolu
25 août 2016 à 3:07:15

Bonjour,
Suite au cours sur la création d'applications Android, je voudrais savoir comment faire pour ajouter une option de partage a mon bouton " action_share ". Mon bouton action_share est celui-ci, situé dans la barre du haut de mon MainActivity.java.


Car dans le cours, il est montré comment le créer, mais aucun détails sur "comment partager avec"... J'ai cherché et me suis renseigné sur internet, mais étant donné que la rédaction du code dépend de ce que chaque utilisateur a inscrit auparavant dans ses fichiers, bah je suis perdu et impossible d'y arriver... Et je suis également débutant en la matière...
Pourrais-je avoir de l'aide ?
Merci en avance :)

EDIT : mon code MainActivity :

Button buttonshare = (Button) findViewById(R.id.action_share);
buttonshare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("matcnoo", "Button share ok !");
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Here is the share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});

main_menu.xml:

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

<item android:id="@+id/action_share"
android:icon="@android:drawable/ic_menu_share"
android:title="@string/toolbar_share_button"
app:showAsAction="always"></item>
<item android:id="@+id/action_add"
android:icon="@android:drawable/ic_menu_add"
android:title="@string/toolbar_add_button"
app:showAsAction="ifRoom"></item>
<item android:id="@+id/action_delete"
android:icon="@android:drawable/ic_menu_delete"
android:title="@string/toolbar_delete_button"
app:showAsAction="never"></item>

</menu>

En inscrivant tout ca, l'appli plante au démarrage, j'ai oublié quelque chose dans le manifest ?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.matcnoo.dice" >

<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="Dice"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DiceActivity" /><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>

</manifest>

-
Edité par MatCo1 25 août 2016 à 23:12:24

  • Partager sur Facebook
  • Partager sur Twitter
26 août 2016 à 10:03:54

Salut,

Que cherches-tu à faire exactement ? Tu souhaites partager avec une application spécifique ?

  • Partager sur Facebook
  • Partager sur Twitter
26 août 2016 à 11:50:07

rolandl a écrit:

Salut,

Que cherches-tu à faire exactement ? Tu souhaites partager avec une application spécifique ?


Bonjour,

Non pas forcément, a partir de l'icone de partage android dans l'action bar, j'aurais aimé partager à n'importe quelle application possible en y inscrivant un texte par défaut comme le lien de téléchargement de l'application (quand elle sera dans le store) mais en attendant, j'aurais aimé mettre au moins un texte au hasard juste histoire de tester.

-
Edité par MatCo1 26 août 2016 à 11:51:17

  • Partager sur Facebook
  • Partager sur Twitter
26 août 2016 à 12:08:33

Et quel est le problème avec ton code actuel ?

  • Partager sur Facebook
  • Partager sur Twitter
26 août 2016 à 15:03:16

Le problème est que je n'arrive tout simplement pas à demander à ce bouton de partager, je ne sais pas comment faire, j'ai regardé pleins de méthode de partout sur internet, mais aucune ne fonctionne...

Si tu regarde les codes que j'ai inscrit dans mon premier message, l'application plante au démarrage

j'ai donc tenté autre chose, l'application démarre mais le bouton de partage ne fonctionne toujours pas...

Voici ce que j'ai mis a la place dans main_menu.xml :

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

<item
android:id="@+id/menu_item_share"
app:showAsAction="always"
android:icon="@android:drawable/ic_menu_share"
android:title="@string/toolbar_share_button"
bwq:actionProviderClass="android.support.v7.widget.ShareActionProvider" />

<item android:id="@+id/action_add"
android:icon="@android:drawable/ic_menu_add"
android:title="@string/toolbar_add_button"
app:showAsAction="ifRoom"></item>

<item android:id="@+id/action_delete"
android:icon="@android:drawable/ic_menu_delete"
android:title="@string/toolbar_delete_button"
app:showAsAction="never"></item>

</menu>

MainActivity.java :

private ShareActionProvider mShareActionProvider;

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

// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);

// Return true to display menu
return true;
}


// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
  • Partager sur Facebook
  • Partager sur Twitter
29 août 2016 à 9:27:58

Le mieux est d'utiliser ta première méthode qui utilisait une Intent. Ensuite pour le partage, il convient simplement d'ajouter ton entrée dans le menu (via le code ou via le XML) et de récupérer le clic sur le bouton dans la méthode onOptionItemSelected par exemple.

  • Partager sur Facebook
  • Partager sur Twitter
30 août 2016 à 11:21:56

Merci rolandl !

J'ai enfin pu régler mon problème ! Pour ceux qui ont le même problème que moi et qui nous lisent :

Voici mon code action_bar.xml :

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

<item android:id="@+id/action_share"

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

android:title="@string/toolbar_share_button"

app:showAsAction="always"></item>

<item android:id="@+id/action_redirect_url"

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

android:title="WebSite"

app:showAsAction="always"></item>


<item android:id="@+id/action_mail"

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

android:title="@string/toolbar_mail_button"

app:showAsAction="ifRoom"></item>

</menu>

code MainActivity.java :

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.action_redirect_url:

Intent viewIntent =
new Intent("android.intent.action.VIEW",
Uri.parse("http://securitenum.heb3.org/"));
startActivity(viewIntent);

return true;

case R.id.action_share:
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");

// Add data to the intent, the receiving app will decide
// what to do with it.
share.putExtra(Intent.EXTRA_SUBJECT, "APP");
share.putExtra(Intent.EXTRA_TEXT, "Go visit his website ! http://securitenum.heb3.org/");

startActivity(Intent.createChooser(share, getString(R.string.share_subject)));
return true;

case R.id.action_mail:

String[] TO = {"XXXXXXXXX@gmail.com"};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");


emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "[App] XXXXXXXX");
emailIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.mail_text) );

try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}

return true;
}

return true;


}

-
Edité par MatCo1 30 août 2016 à 11:25:54

  • Partager sur Facebook
  • Partager sur Twitter