トラッキング コード

1/21/2015

IllegalStateException at ActivityUnitTestCase with ActionBarActivity

I need to do ActivityUnitTestCase which activity is extends ActionBarActivity.
However, I get the following exception.

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)


If call startActivity, Context does not have Theme.AppCompat...
So, We need to call setActivityContext() due to set Context with theme.
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        Context context = getInstrumentation().getTargetContext();
        ContextThemeWrapper contextTheme = new ContextThemeWrapper(context, R.style.AppTheme);
        setActivityContext(contextTheme);
        startActivity(new Intent(), null, null);
    }

1/17/2015

How to add Volley Request with parameter of POST and Headers

If need to add parameter of POST and Headers, you need to read the following source code.
There are getHeaders() and getParams() in Request class which is super class of StringRequest class.
     /**
     * Returns a list of extra HTTP headers to go along with this request. Can
     * throw {@link AuthFailureError} as authentication may be required to
     * provide these values.
     * @throws AuthFailureError In the event of auth failure
     */
    public Map<String, String> getHeaders() throws AuthFailureError {
        return Collections.emptyMap();
    }

     /**
     * Returns a Map of parameters to be used for a POST or PUT request.  Can throw
     * {@link AuthFailureError} as authentication may be required to provide these values.
     *
     * <p>Note that you can directly override {@link #getBody()} for custom data.</p>
     *
     * @throws AuthFailureError in the event of auth failure
     */
    protected Map<String, String> getParams() throws AuthFailureError {
        return null;
    }

We can add parameter when do override getHeaders() and getParams() method.The following source is sample.


    /**
     * Request(StringRequest) with params
     *
     * @param url      request url
     * @param listener listener for Response or Error
     * @param params   value of setting Http Params
     * @param headers  value of setting Http headers
     */
    public void get(String url, final ResponseListener listener, final Map<String, String> params
            , final Map<String, String> headers) {
 
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        listener.onResponse(s);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        listener.onErrorResponse(volleyError);
                    }
                }
        ) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }
 
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return headers;
            }
        };
 
        mRequestQueue.add(request);
    }

7/27/2014

How to handle to auto flip with ViewFlipper

ViewFlipper do not have a handler to auto flipping.
If do handing, we should get Animation Class via VewFlipper#getInAnimation().
We register to Listener.


The follow code is sample.
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_flashing_card, null);
        mVewFlipper = (ViewFlipper) view.findViewById(R.id.viewFlipper1);

        mVewFlipper.setAutoStart(true);
        mVewFlipper.setInAnimation(getActivity(), android.R.anim.slide_in_left);
        mVewFlipper.getInAnimation().setAnimationListener(this);
        mVewFlipper.setFlipInterval(2000);

        return view;
    }

    @Override
    public void onAnimationEnd(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationStart(Animation animation) {
    }

3/30/2014

How to use SwipeRefreshLayout, like a twitter, facebook



Create the Layout

You need to use the android.support.v4.widget.SwipeRefreshLayout.
Content is included SwipeRefreshLayout for clild layout( or view).

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_widget"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <!-- some full screen pullable view that will be the offsetable content -->

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/content"/>
</android.support.v4.widget.SwipeRefreshLayout>


Update for Swipe

You need the following step, due to updating for Swipe.
1. call setOnRefreshListener methiod due to setting Listener which handles the update starting.
2. onRefresh() is handled the update starting.
3. Then your update process finish、call setRefreshing(false).
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_swipe_refresh_widget);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }


    @Override
    public void onRefresh() {
        refresh();
    }

    private void refresh() {
        Message msg = mHander.obtainMessage(0, this);
        mHander.sendMessageDelayed(msg, 1000);
    }

    private static Handler mHander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            SwipeRefreshLayoutActivity actibity = (SwipeRefreshLayoutActivity) msg.obj;
            actibity.mSwipeRefreshLayout.setRefreshing(false);
        }
    };

3/15/2014

How to use Google Play services

Ready to use Google Play services

If you need to use Google Play services API, you should do download of Google Play services on Android SDK Manager.Google Play services was saved in following directory.<AndroidSDK>\sdk\extras\google\google_play_services\libproject\google-play-services_lib

Importing this Android Project to eclipse.

You must write a meta-data in AndroidMaifest.xml using Google Play services API.

    <application
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

Check to exist of Google Play services

You shoud Check to exist of Google Play services in a device.

public class UsingGooglePlayServiceSampleActivity extends FragmentActivity {

    private boolean isGooglePlayServicesAvailable() {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        if (ConnectionResult.SUCCESS == resultCode) {
            return true;
        } else {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    CONNECTION_FAILURE_RESOLUTION_REQUEST);
            if (dialog != null) {
                ErrorDialogFragment errorFragment = new ErrorDialogFragment();
                errorFragment.setDialog(dialog);
                errorFragment.show(getSupportFragmentManager(), "errro_dialog");
            }
        }

        return false;
    }

    public static class ErrorDialogFragment extends DialogFragment {

        private Dialog mDialog;

        public ErrorDialogFragment() {
            super();
            mDialog = null;
        }

        public void setDialog(Dialog dialog) {
            mDialog = dialog;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return mDialog;
        }

        @Override
        public void onDestroy() {
            mDialog = null;
            super.onDestroy();
        }
    }
}

Calling a method of GooglePlayServicesUtil.isGooglePlayServicesAvailable(), you can get Result code.If Result code is error, you can get a Dialog Object from calling method GooglePlayServicesUtil.getErrorDialog().You must use a DialogFragment to show a dialog.

If User close a dialog, onActivityResult is called with Result Code.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CONNECTION_FAILURE_RESOLUTION_REQUEST) {
            if (resultCode == Activity.RESULT_OK) {
                // retry process
            }
        }
    }

2/08/2014

About a permission "WRITE_EXTERNAL_STORAGE"

http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE
Starting in API level 19, this permission is not required to read/write files in your application-specific directories returned by getExternalFilesDir(String) and getExternalCacheDir()


If appliction is waking up on Android 4.4 Device, WRITE_EXTERNAL_STORAGE is not required to call getExternalFilesDir(String) and getExternalCacheDir(). Not returned Null.

2/01/2014

Add "disable swipe" to ViewPager.

I want to stop swipe in ViewPager. But ViewPager does not have "disable swipe" API.

Creat a custom ViewPager

I creat a custom ViewPager. Custom ViewPager has a "disable swipe" API.

package jp.baroqueworksdev.myapidemo.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class MyViewPager extends ViewPager {
    private boolean mIsEnabledSwipe = true;

    public MyViewPager(Context context) {
        super(context);
    }

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!mIsEnabledSwipe) {
            return false;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (!mIsEnabledSwipe) {
            return false;
        }
        return super.onInterceptTouchEvent(event);
    }

    public void setEnabledSwipe(boolean enabled) {
        mIsEnabledSwipe = enabled;
    }

}