Skip to content

Instantly share code, notes, and snippets.

@nathanjsweet
Created January 27, 2015 19:57
Show Gist options
  • Save nathanjsweet/fc71131f2d8ce96d9804 to your computer and use it in GitHub Desktop.
Save nathanjsweet/fc71131f2d8ce96d9804 to your computer and use it in GitHub Desktop.
Don't want to use the ShareActionProvider? Use the ShareActionAdapter instead
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@android:color/transparent" />
<item android:state_pressed="true" android:drawable="@color/INSERHERE" /> <!-- try: #22FFFFFF -->
</selector>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:paddingTop="12.0dp"
android:paddingBottom="12.0dp"
android:paddingLeft="16.0dp"
android:paddingRight="16.0dp"
android:minWidth="196.0dp"
android:background="@drawable/menu_item_selector"
android:layout_gravity="center_vertical"
android:clickable="true"
style="@android:style/Widget.Holo.Light.PopupMenu">
<ImageView
android:layout_width="24.0dp"
android:layout_height="wrap_content"
android:id="@+id/icon"
android:adjustViewBounds="true"
android:layout_marginRight="8.0dp"/>
<TextView
style="@android:style/Widget.Holo.Light.PopupMenu"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"/>
</LinearLayout>
public class MyActivity extends Activity {
private ShareActionAdapter m_share_action_adapter = null;
private ListPopupWindow m_popup_social = null;
private Button m_share_button = null
@Override
public void OnCreate(Bundle savedInstance){
setContentView(/*some_view that includes a button*/);
m_share_button = findViewById(R.id.my_share_btn);
m_share_button = m_actionbar.findViewById(R.id.share_btn);
m_share_button.setVisibility(View.VISIBLE);
m_share_action_adapter = new ShareActionAdapter(context);
m_share_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setNewPopup();
}
});
m_share_action_adapter.setOnMenuListener(new ShareActionAdapter.OnMenuListner() {
@Override
public void onSeeAllSelected() {
m_popup_social.dismiss();
m_share_action_adapter.setSeeAll(true);
setNewPopup();
}
@Override
public void onItemSelected() {
m_popup_social.dismiss();
}
});
}
private void setNewPopup(){
m_popup_social = new ListPopupWindow(getContext(), null, -1, R.style.LocalSaver_PopupMenuStyle);
m_popup_social.setAnchorView(m_share_button);
m_popup_social.setAdapter(m_share_action_adapter);
//R.dimen.popupwidth is 580.dp
int maxWidth = Math.max(getResources().getDisplayMetrics().widthPixels / 2, getResources().getDimensionPixelSize(R.dimen.popupwidth));
m_popup_social.setWidth(maxWidth);
m_popup_social.setContentWidth(Math.min(maxWidth, m_share_action_adapter.measureContentWidth()));
m_popup_social.setModal(true);
m_popup_social.show();
m_popup_social.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
m_share_action_adapter.setSeeAll(false);
}
});
}
}
package com.localsaver.details;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View.MeasureSpec;
import java.util.ArrayList;
public class ShareActionAdapter extends BaseAdapter implements View.OnClickListener{
private ArrayList<ResolveInfo> m_list;
private Intent m_intent = null;
private Context m_context;
private boolean m_seall = false;
private OnMenuListner m_menu_litener = null;
private OnShareTargetSelected m_onshare_listener = null;
private static int S_MAX_ELS = 4;
public ShareActionAdapter (Context context){
this(context, false);
}
public ShareActionAdapter (Context context, boolean seeAll){
m_seall = seeAll;
m_context = context;
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
PackageManager pm = context.getPackageManager();
m_list = new ArrayList<>(pm.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER));
}
public void setSeeAll(boolean seeAll){
m_seall = seeAll;
}
public void setShareIntent(Intent intent){
m_intent = intent;
}
public void setOnMenuListener(OnMenuListner menuListener){
m_menu_litener = menuListener;
}
public void setOnShareTargetSelected(OnShareTargetSelected shareTargetListener){
m_onshare_listener = shareTargetListener;
}
@Override
public int getCount() {
return m_seall ? m_list.size() : Math.min(S_MAX_ELS, m_list.size()) + 1;
}
@Override
public ResolveInfo getItem(int position) {
return m_list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = convertView != null ? convertView : LayoutInflater.from(m_context).inflate(R.layout.menuitem, null);
LinearLayout ll = (LinearLayout)convertView;
if(!m_seall && position + 1 == getCount()){
ll.findViewById(R.id.icon).setVisibility(View.GONE);
((TextView) ll.findViewById(R.id.title)).setText("See All");
ll.setTag(-1);
}
else {
ResolveInfo ri = getItem(position);
((TextView) ll.findViewById(R.id.title)).setText(ri.loadLabel(m_context.getPackageManager()).toString());
((ImageView) ll.findViewById(R.id.icon)).setImageDrawable(ri.loadIcon(m_context.getPackageManager()));
ll.setTag(position);
}
ll.setOnClickListener(this);
return ll;
}
public int measureContentWidth() {
// Menus don't tend to be long, so this is more sane than it looks.
int maxWidth = 0;
View itemView = null;
int itemType = 0;
final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int count = this.getCount();
for (int i = 0; i < count; i++) {
final int positionType = this.getItemViewType(i);
if (positionType != itemType) {
itemType = positionType;
itemView = null;
}
itemView = this.getView(i, itemView, null);
itemView.measure(widthMeasureSpec, heightMeasureSpec);
final int itemWidth = itemView.getMeasuredWidth();
if (itemWidth > maxWidth) {
maxWidth = itemWidth;
}
}
return maxWidth;
}
@Override
public void onClick(View v) {
int tag = (int)v.getTag();
if(tag == -1 && m_menu_litener != null) {
m_menu_litener.onSeeAllSelected();
}
else{
m_menu_litener.onItemSelected();
ResolveInfo ri = getItem(tag);
if(ri != null && m_intent != null){
ActivityInfo activity = ri.activityInfo;
ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
if(m_onshare_listener != null)
m_onshare_listener.onShareTargetSelected(activity.name, m_intent);
m_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
m_intent.setComponent(name);
m_context.startActivity(m_intent);
}
}
}
public static interface OnMenuListner{
void onSeeAllSelected();
void onItemSelected();
}
public static interface OnShareTargetSelected {
void onShareTargetSelected(String name, Intent intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment