Posts

Showing posts from March, 2016

Android: de compile an android apk file

extract apk file as zip or rar file convert the dex file to jar using dex2jar tool then open the jar file with jd_gui tool.

Android: Display Manager

public class AppDisplayManager { private static AppDisplayManager ourInstance = new AppDisplayManager(); private Display mDisplay ; public static AppDisplayManager getInstance() { return ourInstance ; } private AppDisplayManager() { WindowManager wm = (WindowManager) CommonUtil. getAppContext ().getSystemService(Context. WINDOW_SERVICE ); mDisplay = wm.getDefaultDisplay(); } public int getDeviceWidth() { if ( null != mDisplay ) { if (android.os.Build.VERSION. SDK_INT >=  android.os.Build.VERSION_CODES. HONEYCOMB_MR2 ) { Point point = new Point(); mDisplay .getSize(point); return point. x ; } else { return mDisplay .getWidth(); } } else { return 0 ; } } public int getDeviceHeight() { if ( null != mDisplay ) { if (android.o

Android: Dynamic column/grid for ListView

public class DynamicColumnGridManager extends GridLayoutManager {     private int minItemWidth;     public DynamicColumnGridManager(Context context, int minItemWidth) {         super(context, 1);         this.minItemWidth = minItemWidth;     }     @Override     public void onLayoutChildren(RecyclerView.Recycler recycler,                                  RecyclerView.State state) {         updateSpanCount();         super.onLayoutChildren(recycler, state);     }     private void updateSpanCount() {         int spanCount = getWidth() / minItemWidth;         if (spanCount < 1) {             spanCount = 1;         }         this.setSpanCount(spanCount);     } } mLayoutManager = new DynamicColumnGridManager(getApplicationContext(), mItemWidth); mRecyclerView.setLayoutManager(mLayoutManager);

Android: animate/translate RecyclerView items using ItemTouchHelper

ItemTouchHelper.Callback callback = new ItemMoveHelper(ItemTouchHelper. UP |  ItemTouchHelper. DOWN , ItemTouchHelper. START | ItemTouchHelper. END ); ItemTouchHelper helper = new ItemTouchHelper(callback); helper.attachToRecyclerView(m RecyclerView ); ItemTouchHelper moves the items' of a recyclerView to translateX/Y properties to  reposition them class ItemMoveHelper extends ItemTouchHelper.SimpleCallback { public ItemMoveHelper( int dragDirs, int swipeDirs) { super (dragDirs, swipeDirs); } @Override public boolean onMove(RecyclerView recyclerView,  RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { Collections. swap ( mBagList , viewHolder.getAdapterPosition(), target.getAdapterPosition()); mAdapter .notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition()); return false ; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int

Android: Change the ActionBar primary and secondary colors

    <!-- Base application theme. -->     <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">         <item name="colorPrimary">@color/appPrimaryColor</item>         <item name="colorPrimaryDark">@color/appSecondaryColor</item>     </style>

Android: ListView Pagination

While having pagination do not re create the adapter object, instead update the existing adapter object. so that the list view will not be refreshed or re created. It will be updated with new items.

Android: Animate Navigation icon with DrawerArrowToggle style

<style name="AppTheme" parent="Theme.AppCompat.Light">     <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> </style> <!-- change the spinBars for diff animation  --> <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">     <item name="spinBars">true</item>     <item name="color">@android:color/white</item> </style>

Android: SSL/https POST request using Volley

RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext(), new HurlStack(null, getSSL())); final JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { if (null != jsonObject) { try { String accessToken = (String) jsonObject.get("access_token"); AppPreference.getInstance().setPreference(AppKeys.ACCESS_TOKE                                                          N, accessToken); haveAccessToken = true; mHandler.sendEmptyMessage(1); Log.i(getClass().getSimpleName(), accessToken); } catch (JSONException e) { e.printStackTrace(); } Toast.makeText(getActivity().getApplicationContext(), "Login Success",                                                    Toast.LENGTH_SHORT).show(); } } }, new Response.ErrorListener() { @Overri

Android: Standalone login with SQLiteOpenHelper

import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /**  * Created by Chandra on 31-10-2015.  */ public class LoginSQLiteHelper extends SQLiteOpenHelper {     public static final String TABLE_NAME = "table_login";     public static final String COLUMN_ID = "_id";     public static final String COLUMN_USER_EMAIL = "user_email";     public static final String COLUMN_USER_PWD = "user_pwd";     public static final String COLUMN_USER_NAME = "user_name";     public static final String COLUMN_USER_NO = "user_no";     private static final String DATABASE_NAME = "login.db";     private static final int DATABASE_VERSION = 1;     // Database creation sql statement     private static final String SQL_CREATE_ENTRIES = "CREATE TABLE "             + TABLE_NAME + "(" + COLUMN_ID + " IN

Android: WebView back navigation

webview = (WebView) rootView.findViewById(R.id. testWebView ); webview .setWebViewClient( new MyBrowser()); private class MyBrowser extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true ; } }  

Android: Check Mobile/Device network state

boolean isWiFi = false ; boolean isMobile = false ; boolean isBluetooth = false ; ConnectivityManager cm = (ConnectivityManager) CommonUtil. getAppContext ().getSystemService(Context. CONNECTIVITY_SERVICE ); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if ( null != activeNetwork) { boolean isConnected = activeNetwork.isConnectedOrConnecting(); if (isConnected) { if (activeNetwork.getType() == ConnectivityManager. TYPE_WIFI ) { isWiFi = activeNetwork.getType() == ConnectivityManager. TYPE_WIFI ; } else if (activeNetwork.getType() == ConnectivityManager. TYPE_MOBILE ) { isMobile = activeNetwork.getType() == ConnectivityManager. TYPE_MOBILE ; } else if (activeNetwork.getType() == ConnectivityManager. TYPE_BLUETOOTH ) { isBluetooth = activeNetwork.getType() == ConnectivityManager. TYPE_BLUETOOTH ; } return (isWiFi || isMobile || isBluetooth); } } return false ;

Android: Ping an external server from android device

// ping external servers public static final String PING_ONE = "ping -c 1 your server IP" ; public static final String PING_TWO = "ping -c 1 your server IP " ; try { Process p1 = java.lang.Runtime. getRuntime ().exec( PING_ONE ); int p1RetExitVal = p1.waitFor(); if (p1RetExitVal == 0 ) { // Internet available   networkState = true ; } else { Process p2 = java.lang.Runtime. getRuntime ().exec( PING_TWO ); int p2RetExitVal = p2.waitFor(); if (p2RetExitVal == 0 ) { // Internet available   networkState = true ; } } } catch (Exception e) { networkState = false ; e.printStackTrace(); }

Android: Layer Drawable uasage with Picasso Librarry

// create drawable array with index drawableArray [index] = new BitmapDrawable(getResources(),  Picasso. with (AppUtils. getAppContext ()) .load( componentUri ) .get()); //images will be drawn based on the index order. higher index will be drawn on top. LayerDrawable layerDrawable = new LayerDrawable( drawableArray ); image .setImageDrawable(layerDrawable);