How to Customize the Share Intent Onclick Event in Android
Asked Answered
O

2

5

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...

Oppen answered 19/2, 2014 at 10:7 Comment(2)
Try this link #6827907Jaclyn
Is it possible to handle the onclick event for the share intent@HariharanTamilanOppen
T
11

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

        }
    });
Tasteful answered 21/2, 2014 at 5:18 Comment(2)
Great reply. But can you tell me what exactly is R.id.listView1?Tamper
Thank you so much. It was a little tricky but with you advice I made it working. Tomorrow I will show my solution, as an additional "how to" - I'm just a beginner, but maybe this stuff will become clearer if I explain my code to others. Thanks a lot!Asterisk
A
1

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;
    }
Asterisk answered 1/12, 2014 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.