Tuesday, February 28, 2017

showing dialog after activity finished

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@6346ff9 is not valid; is your activity running?


if(((Activity) context).isFinishing()) {
    return;
}

Tuesday, February 14, 2017

Java Interface callback example

***** Interface class
public class ExtServiceCallback {
    static ExtServiceCallback.Callback sCallbacks = null;

    public void runServiceCallback(String path, String method, String query, HashMap<String, String> headers, String msgBody) {
        sCallbacks.downToExtService(path, method, query, headers, msgBody);
    }

    public void setCallback(ExtServiceCallback.Callback callback) {
        sCallbacks = callback;
    }

    public interface Callback {
        public void downToExtService(String path, String method, String query, HashMap<String, String> headers, String msgBody);
    }
}


***** App usage

protected void onCreate(Bundle savedInstanceState) {
    ServerCallback callback  = new ServerCallback();
}

public class ServerCallback extends ExtServiceCallback implements ExtServiceCallback.Callback {
    private static final String TAG = "ServerCallback";

    public ServerCallback() {
        Log.e(TAG, "ServerCallback");
        setCallback(this);
    }

    @Override    public void downToExtService(String path, String method, String query, HashMap<String, String> headers, String msgBody) {
        Log.e(TAG, "path: " + path + ", " +
                "method: " + method + ", " +
                "query: " + query + ", " +
                "msgBody: " + msgBody);
    }
}

Android Studio - add new module

File -> New -> Import Module...

src/app/main/build.gradle
dependencies {
    compile project (':apolloRegistry')
}

Thursday, February 9, 2017

customized list view and helper APIs

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
    private static final String TAG = "ListAdapter";
    Context mContext;
    Activity mActivity;

    public CustomListViewAdapter(Context context, Activity activity, int resourceId,
                                 List<RowItem> items) {
        super(context, resourceId, items);
        this.mContext = context;
        this.mActivity = activity;
    }

    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
        TextView txtTitle;
        TextView txtDesc;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        RowItem rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) mContext
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.video_list_layout, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
            holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.imageView.setImageResource(rowItem.getImageId());
        holder.txtTitle.setText(rowItem.getTitle());
        holder.txtDesc.setText(rowItem.getDesc());

        return convertView;
    }

    RowItem getRowItem(int index) {
        ListView selectList = (ListView) mActivity.findViewById(R.id.video_list_view);
        ListAdapter lAdapter = selectList.getAdapter();
        RowItem item = (RowItem) lAdapter.getItem(index);
        Log.i(TAG, "item.link:" + item.getLink());

        return item;
    }

    int getItemIndex(String title) {
        int index;
        ListView selectList = (ListView) mActivity.findViewById(R.id.video_list_view);
        ListAdapter adapter = selectList.getAdapter();

        for (index = 0; index < adapter.getCount(); index++) {
            RowItem item = (RowItem) adapter.getItem(index);
            if (item.getTitle().matches(title)) {
                break;
            }
        }

        if (index == adapter.getCount())
            return -1;

        return index;
    }

    public void setImageByIndex(TypedArray images, int index, int image_index) {
        ListView listView = (ListView) mActivity.findViewById(R.id.video_list_view);
        ListAdapter adapter = listView.getAdapter();

        int wantedPosition = index; // Whatever position you're looking for
        int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount(); // This is the same as child #0
        int wantedChild = wantedPosition - firstPosition;

        if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
            Log.w(TAG, "Unable to get view for desired position, because it's not being displayed on screen.");
            return;
        }

        View wantedView = listView.getChildAt(wantedChild);
        ViewHolder holder = (ViewHolder) wantedView.getTag();
        holder.imageView.setImageResource(images.getResourceId(image_index, 0));
    }
}