How to Customize Share Intent in Android

How to customize share intent in Android?

No you can't. Intent's are supposed to work this way. If you have to force a particular app to open, use explicit intents if the target apps support those. Without knowing the package names or the component names of the target apps, or the type or mime type of data, you can't force a particular app to work on generalized intents.

Customise Sharing Intent Dialog Is That Possible?

Yes, it is possible to create custom sharing dialog. You just need to get the IntentActivity list and customize as per your requirement. For Sample you can do as below.

Step 1: Prepare Intent

String urlToShare = "https://play.google.com/store/apps/details?id=com.yourapp.packagename";
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
// intent.putExtra(Intent.EXTRA_SUBJECT, "If any extra"); // NB: has no effect!
intent.putExtra(Intent.EXTRA_TEXT, "Let me recommend you this application \n\n" + urlToShare);

Step 2: Get activity list and set in ListAdapter.

final List<ResolveInfo> activities = getPackageManager().queryIntentActivities(intent, 0);

List<DialogItem> appNames = new ArrayList<DialogItem>();

for (ResolveInfo info : activities) {
appNames.add(new DialogItem(info.loadLabel(getPackageManager()).toString(),
info.loadIcon(getPackageManager())));
}
final List<DialogItem> newItem = appNames;
ListAdapter adapter = new ArrayAdapter<DialogItem>(activity,
android.R.layout.select_dialog_item, android.R.id.text1, newItem) {
public View getView(int position, View convertView, ViewGroup parent) {
//Use super class to create the View
View v = super.getView(position, convertView, parent);
TextView tv = v.findViewById(android.R.id.text1);
tv.setText(newItem.get(position).app);
tv.setTextSize(15.0f);
//Put the image on the TextView
tv.setCompoundDrawablesWithIntrinsicBounds(newItem.get(position).icon, null, null, null);

//Add margin between image and text (support various screen densities)
int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
tv.setCompoundDrawablePadding(dp5);

return v;
}
};

Note:- Make sure that DialogItem is your model class and you have to create in you app.

public class DialogItem {
public String app = "";
public Drawable icon;

public DialogItem(String name, Drawable drawable) {
app = name;
icon = drawable;
}
}

Step 3: Set that adapter in your AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Custom Sharing Dialog");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
ResolveInfo info = activities.get(item);

if (info.activityInfo.packageName.equals("com.facebook.katana")) {
Toast.makeText(activity, "Facebook Selected ", Toast.LENGTH_LONG).show();
} else {

// start the selected activity
Log.i(TAG, "Hi..hello. Intent is selected");
intent.setPackage(info.activityInfo.packageName);
startActivity(intent);
}
}
});

AlertDialog alert = builder.create();
alert.show();

Output :-

Sample Image

Its custom sharable dialog using AlertDialog but you can do all setting(UI,Selection,Theme,etc) as per your requirement by using custom layout and creating Dialog class

How to customize share intent?

Replace:

shareIntent.putExtra(Intent.EXTRA_TEXT, mUri);

with:

shareIntent.putExtra(Intent.EXTRA_TEXT, "To get more facts download the APPNAME from the android store PLAYSTORELINK");

How to Customize the Share Intent Onclick Event in Android

By using the below code u can get the list of social media networking apps list which are installed in your mobile.

Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
List activities = ShareList.this.getPackageManager().queryIntentActivities(sendIntent, 0);

Send this list to Adapter class:

ListView lv=(ListView)findViewById(R.id.listView1);
final ShareAdapter adapter=new ShareAdapter(ShareList.this,activities.toArray());
lv.setAdapter(adapter);

Here is the Adapter class code:

public class ShareAdapter extends BaseAdapter {
Object[] items;
private LayoutInflater mInflater;
Context context;

public ShareAdapter(Context context, Object[] items) {
this.mInflater = LayoutInflater.from(context);
this.items = items;
this.context = context;
}

public int getCount() {
return items.length;
}

public Object getItem(int position) {
return items[position];
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.singleitem, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.textView1);
holder.logo = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.name
.setText(((ResolveInfo) items[position]).activityInfo.applicationInfo
.loadLabel(context.getPackageManager()).toString());

holder.logo
.setImageDrawable(((ResolveInfo) items[position]).activityInfo.applicationInfo
.loadIcon(context.getPackageManager()));

return convertView;
}

static class ViewHolder {

TextView name;
ImageView logo;
}}

Handling the specific social media network in the listview using the below code:

lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ResolveInfo info = (ResolveInfo) adapter.getItem(arg2);
if(info.activityInfo.packageName.contains("facebook")) {
new PostToFacebookDialog(context, body).show();
//here u can write your own code to handle the particular social media networking apps.
Toast.makeText(getApplicationContext(), "FaceBook", Toast.LENGTH_LONG).show();
} else {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "body");
((Activity)ShareList.this).startActivity(intent);
}

}
});

Custom Share to view in Android

Yes, you can.

The first step is to build the sharing intent:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");

And then, instead of creating a chooser intent, you ask the package manager for the activities that can service the sendIntent:

List<ResolveInfo> infos = getPackageManager().queryIntentActivities(sendIntent, 0);

This will give you the list of activities (apps) that the chooser would also show.

You can get the app's icon and label from ResolveInfo and show them in a list to the user:

ResolveInfo info; 
Drawable icon = info.loadIcon(getPackageManager());
String label = info.loadLabel(getPackageManager());

Once the user has selected a ResolveInfo you can enrich your sendIntent with the selected activity to handle your intent and then start the activity:

sendIntent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
startActivity(sendIntent);

We asked the system which activities can handle sendIntent and then let the user pick one of them. So, by definition, that activity can handle sendIntent. Setting the class name of the activity on the sendIntent sends the intent directly to that activity.

Custom share intent activity

Get the list of installed application:

final PackageManager pm = getPackageManager();

List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo packageInfo : packages) {

Log.d(TAG, "Installed package :" + packageInfo.packageName);
Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));

}// the getLaunchIntentForPackage returns an intent that you can use with startActivity()
}

and to launch that package:

protected void launchApp(String packageName) {
Intent mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if (mIntent != null) {
try {
startActivity(mIntent);
} catch (ActivityNotFoundException err) {
Toast t = Toast.makeText(getApplicationContext(),
"Application not found", Toast.LENGTH_SHORT);
t.show();
}
}
}

EDIT

This method queries the package manager for installed packages that can respond to an intent with the specified action:

public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}

and to share your text using a specific application (which can handle ACTION_SEND):

ResolveInfo info = list.get(index);
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
((Activity)context).startActivity(intent);


Related Topics



Leave a reply



Submit