Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Customize the Share Intent Onclick Event in Android

How to customize the Android Share Intent for Facebook App. When I am using the share Intent, I am getting the following dialog.

Share Intent Dialog

But I am using Facebook sdk for post the image and text. And how to customize, when we click on Facebook icon in the above dialog it will navigate to my custom facebook dialog...

like image 829
Mr. N.V.Rao Avatar asked Feb 19 '14 10:02

Mr. N.V.Rao


2 Answers

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

        }
    });
like image 192
Venu Avatar answered Nov 20 '22 10:11

Venu


I used Venu's solution to get the "customized share intent" working. I just had little trouble by creating the xml.. So here I want to show other Android beginner how to add custom_share_list_white.xml. Maybe it will help others to get it working, too.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView"
            android:footerDividersEnabled="false"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Large Text"
                android:id="@+id/tv_share_adapter"
                android:gravity="center_vertical"
                android:layout_gravity="center_vertical"/>
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

in ShareAdapter.java:

 public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_share_list_white, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.tv_share_adapter);
            holder.logo = (ImageView) convertView.findViewById(R.id.imageView);
            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;
    }
like image 38
Martin Pfeffer Avatar answered Nov 20 '22 09:11

Martin Pfeffer