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