Thursday, March 26, 2015

mipmap vs drawable

The mipmap folders are for placing your app icons in only. Any other drawable assets you use should be placed in the relevant drawable folders as before.


Google Related Link


Wednesday, February 11, 2015

Android Material Design Open sources

Material Design Reference Projects:

  1. MaterialDesignLibrary This is a library with components of Android L to you use in android 4.0
    https://github.com/navasmdc/MaterialDesignLibrary#flat-button
  2. MaterialEverywhere Showcase of the new AppCompat 21, which includes new Material Theme, working in pre-21 devices.
    https://github.com/antoniolg/MaterialEverywhere
  3. MaterialWidget Android L design widget in Android 4.0 ~ 4.4.
    https://github.com/keithellis/MaterialWidget
  4. MaterialTabs Custom Tabs with Material Design effects. It requires 14+ API and android support v7 (Toolbar)
    https://github.com/neokree/MaterialTabs
  5. material-ripple Android L Ripple effect wrapper for Views
    https://github.com/balysv/material-ripple
  6. RippleEffect Implementation of Ripple effect from Material Design for Android API 14+
    https://github.com/traex/RippleEffect
  7. LDrawer Android drawer icon with material design animation
    https://github.com/ikimuhendis/LDrawer
  8. material-design-icons Material Design icons by Google
    https://github.com/google/material-design-icons
  9. AndroidMaterialDesignToolbar Android Sample Project with Material Design and Toolbar.
    https://github.com/tekinarslan/AndroidMaterialDesignToolbar
  10. MaterialEditText EditText in Material Design
    https://github.com/rengwuxian/MaterialEditText
  11. material-menu Morphing Android menu, back, dismiss and check buttons
    https://github.com/balysv/material-menu
  12. material-dialogs Not even AppCompat uses Material theming for AlertDialogs on pre-Lollipop. This is a beautiful and easy solution.
    https://github.com/afollestad/material-dialogs
  13. MaterialNavigationDrawer Navigation Drawer Activity with material design style and simplified methods
    https://github.com/neokree/MaterialNavigationDrawer
  14. MaterialDialog An Android library for conveniently building Material Design Dialog in Android version 2.2 ~ L.
    https://github.com/drakeet/MaterialDialog
  15. materialish-progress A material style progress wheel compatible with 2.3
    https://github.com/pnikosis/materialish-progress
  16. MaterialList An Android library aimed to get the beautiful CardViews that Google shows at its official design specifications
    https://github.com/dexafree/MaterialList
  17. android-floating-action-button Floating Action Button for Android based on Material Design specification
    https://github.com/futuresimple/android-floating-action-button
  18. Android-Material-circular-button Animated Material circular button
    https://github.com/glomadrian/Android-Material-circular-button
  19. material-drawer DrawerLayout implementation for Material design apps
    https://github.com/HeinrichReimer/material-drawer
  20. snackbar Snackbar Android Library
    https://github.com/nispok/snackbar
  21. Android-DialogFragments Various DialogFragments for Android.
    https://github.com/wada811/Android-DialogFragments
  22. material-range-bar Android widget for selecting a range of values.
    https://github.com/oli107/material-range-bar
  23. CircularProgressView A Material style circular progress bar for Android
    https://github.com/rahatarmanahmed/CircularProgressView
  24. RippleDrawable A port of Ripple Effect from Android L to non L devices
    https://github.com/ozodrukh/RippleDrawable

Tuesday, February 3, 2015

Android Cross Walk - Hybrid Application


Android Cross walk - Hybrid Application.


  Android WebView has many compatibility issues with the different versions of Android. Now this inconsistency in development can be fixed using Cross Walk from Intel. Cross Walk Provides you latest browser as a library project.

Android Cross walk supports the version 4.0 and above. ( Ice Cream Sandwich , Jelly Bean, Kit Kat,  Lollipop) which is API version 14 and above.



(Note : For developer with basic knowledge of Android)


Step 1 : Download the Android web view (x86) form here. No need of Android (ARM +86).

Step 2 : Import the project to Eclipse. It will be a library project.

Step 3 : Create a New project and add cross walk webview as a dependency project.

Step 4 : Lets start coding. Instead of web view use XWalkView here is the api.

<org.xwalk.core.XWalkView 
    android:id="@+id/web_container"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

</org.xwalk.core.XWalkView>

Step 5 : As the entire custom WebView is being imported we have to give the following permission in the AndroidManifest.xml.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

Step 6: Loading web page.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mXWalkView = (XWalkView) findViewById(R.id.activity_main);

mXWalkView.load("http://videojs.com", null); // API here
}


Step 7: Remote Debugging using Chrome .

Remote debugging was supported by android after version KitKat. By using the below code.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      WebView.setWebContentsDebuggingEnabled(true);
  }

But now by using Cross walk it can works form Ice Cream Sandwich.

Note: XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGINGtrue); This method has be insert ed before setContentView(R.layout.activity_main);.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);//Api here

               
setContentView(R.layout.activity_main);
mXWalkView = (XWalkView) findViewById(R.id.activity_main);
mXWalkView.load("http://videojs.com", null);
}


Step 8: Communication between client javascript and android.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
setContentView(R.layout.activity_main);
mXWalkView = (XWalkView) findViewById(R.id.activity_main);

mXWalkView.addJavascriptInterface(new JsInterface(), "NativeInterface");

mXWalkView.load("http://videojs.com", null);
}

       private class JsInterface{
@org.xwalk.core.JavascriptInterface
public String sayHello(){
return "Hello World!!";
}
}

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
      function click(){
NativeInterface.sayHello();
      }
</script>
</head>
<body>
      <a onclick="click()">click</a>
</body>
</html>

Step 9: Communication between client Android and javascript.

        mXWalkView.evaluateJavascript("javascript:nativeCall()"
                                    new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
         // You can get a return value from Javascript.
}

                                       });



<script type="text/javascript">
      function nativeCall(){
return "Hello World!!"; // Return string to native method.
      }
</script>


Step 10: Replacement of WebViewClient is XWalkResourceClient.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
setContentView(R.layout.activity_main);
mXWalkView = (XWalkView) findViewById(R.id.activity_main);
mXWalkView.setResourceClient(new MyReasourceClient(mXWalkView));
mXWalkView.addJavascriptInterface(new JsInterface(), "NativeInterface");
mXWalkView.load("http://videojs.com", null);
}


private class MyReasourceClient extends XWalkResourceClient{

public MyReasourceClient(XWalkView view) {
super(view);
}

@Override
public void onLoadFinished(XWalkView view, String url) {
Log.d(TAG,"Onfinished");
mXWalkView.evaluateJavascript("javascript:showAlert()", new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
    Log.d(TAG, value);
}
} );
super.onLoadFinished(view, url);
}

@Override
public void onLoadStarted(XWalkView view, String url) {
Log.d(TAG,"onLoadStarted");
super.onLoadStarted(view, url);
}

@Override
public void onProgressChanged(XWalkView view, int progressInPercent) {
Log.d(TAG,"onProgressChanged");
super.onProgressChanged(view, progressInPercent);
}

@Override
public void onReceivedLoadError(XWalkView view, int errorCode,
String description, String failingUrl) {
Log.d(TAG,"onReceivedLoadError");
super.onReceivedLoadError(view, errorCode, description, failingUrl);
}

@Override
public void onReceivedSslError(XWalkView view,
ValueCallback<Boolean> callback, SslError error) {
Log.d(TAG,"onReceivedSslError");
super.onReceivedSslError(view, callback, error);
}

@Override
public WebResourceResponse shouldInterceptLoadRequest(XWalkView view,
String url) {
Log.d("shouldInterceptLoadRequest","shouldInterceptLoadRequest");
return super.shouldInterceptLoadRequest(view, url);
}

@Override
public boolean shouldOverrideUrlLoading(XWalkView view, String url) {
Log.d("shouldOverrideUrlLoading","shouldOverrideUrlLoading");
return super.shouldOverrideUrlLoading(view, url);
}
}



Step 11: Replacement/Similar to WebChormeClient.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
setContentView(R.layout.activity_main);
mXWalkView = (XWalkView) findViewById(R.id.activity_main);
mXWalkView.setResourceClient(new MyReasourceClient(mXWalkView));
mXWalkView.addJavascriptInterface(new JsInterface(), "NativeInterface");
mXWalkView.setUIClient(new CustomUiClient(mXWalkView));
mXWalkView.load("http://videojs.com", null);
}

private class CustomUiClient extends XWalkUIClient{

        public CustomUiClient(XWalkView view){
            super(view);
        }

        public void onReceivedTitle(XWalkView view,
                                    java.lang.String title){
            if(title.contains("404")){
            mXWalkView.evaluateJavascript("javascript:window.location.replace('')", new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
}
});
            }

        }
        public void openFileChooser(XWalkView view,
                                    android.webkit.ValueCallback<android.net.Uri> uploadFile,
                                    java.lang.String acceptType,
                                    java.lang.String capture){

        }

    }


Step 12: Pause, Resume and Destroy.

@Override
protected void onPause() {
super.onPause();
if(mXWalkView != null){
mXWalkView.pauseTimers();
mXWalkView.onHide();
}
}
@Override
protected void onResume() {
super.onResume();
if(mXWalkView != null){
mXWalkView.resumeTimers();
mXWalkView.onShow();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
XWalkPreferences.setValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW, false);
 
if(mXWalkView !=null){
mXWalkView.onDestroy();
}
}

Step 13: Orientation changes to stop reload.

   <activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Step 14: Complete code.


MainActivity.java

package com.example.crosswalkd;

import org.xwalk.core.JavascriptInterface;
import org.xwalk.core.XWalkPreferences;
import org.xwalk.core.XWalkResourceClient;
import org.xwalk.core.XWalkUIClient;
import org.xwalk.core.XWalkView;

import android.app.Activity;
import android.content.res.Configuration;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebResourceResponse;

public class MainActivity extends Activity {

private XWalkView mXWalkView;

private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
XWalkPreferences.setValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW, true);
XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
setContentView(R.layout.activity_main);


mXWalkView = (XWalkView) findViewById(R.id.activity_main);

mXWalkView.setResourceClient(new MyReasourceClient(mXWalkView));

mXWalkView.addJavascriptInterface(new JsInterface(), "NativeInterface");
mXWalkView.setUIClient(new CustomUiClient(mXWalkView));

mXWalkView.load("http://videojs.com", null);

}


private class JsInterface{

@JavascriptInterface
public String sayHello(){
return "Hello World!!";
}

}



private class MyReasourceClient extends XWalkResourceClient{

public MyReasourceClient(XWalkView view) {
super(view);
}

@Override
public void onLoadFinished(XWalkView view, String url) {
Log.d(TAG,"Onfinished");
mXWalkView.evaluateJavascript("javascript:showAlert()", new ValueCallback<String>() {

@Override
public void onReceiveValue(String value) {

   Log.d(TAG, value);

}
} );
super.onLoadFinished(view, url);
}

@Override
public void onLoadStarted(XWalkView view, String url) {
Log.d(TAG,"onLoadStarted");
super.onLoadStarted(view, url);
}

@Override
public void onProgressChanged(XWalkView view, int progressInPercent) {
Log.d(TAG,"onProgressChanged");
super.onProgressChanged(view, progressInPercent);
}

@Override
public void onReceivedLoadError(XWalkView view, int errorCode,
String description, String failingUrl) {
Log.d(TAG,"onReceivedLoadError");
super.onReceivedLoadError(view, errorCode, description, failingUrl);
}

@Override
public void onReceivedSslError(XWalkView view,
ValueCallback<Boolean> callback, SslError error) {
Log.d(TAG,"onReceivedSslError");
super.onReceivedSslError(view, callback, error);
}

@Override
public WebResourceResponse shouldInterceptLoadRequest(XWalkView view,
String url) {
Log.d("shouldInterceptLoadRequest","shouldInterceptLoadRequest");
return super.shouldInterceptLoadRequest(view, url);
}

@Override
public boolean shouldOverrideUrlLoading(XWalkView view, String url) {
Log.d("shouldOverrideUrlLoading","shouldOverrideUrlLoading");
return super.shouldOverrideUrlLoading(view, url);
}



}



@Override
protected void onPause() {
super.onPause();

if(mXWalkView != null){
mXWalkView.pauseTimers();
mXWalkView.onHide();
}
}

@Override
protected void onResume() {
super.onResume();

if(mXWalkView != null){
mXWalkView.resumeTimers();
mXWalkView.onShow();

}
}


@Override
protected void onDestroy() {
super.onDestroy();
XWalkPreferences.setValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW, false);

if(mXWalkView !=null){
mXWalkView.onDestroy();
}

}





 private class CustomUiClient extends XWalkUIClient{

       public CustomUiClient(XWalkView view){
           super(view);
       }

       public void onReceivedTitle(XWalkView view,
                                   java.lang.String title){
           if(title.contains("404")){
            mXWalkView.evaluateJavascript("javascript:window.location.replace('')", new ValueCallback<String>() {

@Override
public void onReceiveValue(String value) {

}
});
           }

       }
       public void openFileChooser(XWalkView view,
                                   android.webkit.ValueCallback<android.net.Uri> uploadFile,
                                   java.lang.String acceptType,
                                   java.lang.String capture){

       }

   }


}

File name: activity_main.xml

<org.xwalk.core.XWalkView android:id="@+id/activity_main"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
</org.xwalk.core.XWalkView>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.crosswalkd"
    android:versionCode="1"
    android:versionName="1.0" >
    
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>












Wednesday, July 2, 2014

Insertion Sort in Java

For theoretical reference:
http://en.wikipedia.org/wiki/Insertion_sort


Java Program:


public class InsertionSort {

    public static void main(String[] args) {
        int[] input = { 2, 7, 4, 1, 5, 3};
        insertionSort(input);
    }
   
    private static void printNumbers(int[] input) {       
        for (int i = 0; i < input.length; i++) {
            System.out.print(input[i] + ", ");
        }
        System.out.println("\n");
    }

    public static void insertionSort(int array[]) {
        int n = array.length;
        for (int j = 1; j < n; j++) {
            int key = array[j];
            int i = j-1;
            while ( (i > -1) && ( array [i] > key ) ) {
                array [i+1] = array [i];
                i--;
            }
            array[i+1] = key;
            printNumbers(array);
        }
    }
}

Monday, June 30, 2014

Bubble sort algorithm using array in Java.

For theoretical reference you can see wiki link.

http://en.wikipedia.org/wiki/Bubble_sort

For programmatic reference you can check this.


public class BubbleSort {

    public static void main(String[] args) {
       
        int arr[] = { 2, 7, 4, 1, 5, 3};
        showAll(bubbleSort(arr));
    }
   
    private static int [] bubbleSort(int arr[]){
        while(true){
            int count = 0;
            for (int i = 0; i < arr.length - 1; i++) {
                if(arr[i] > arr[i+1]){
                    int temp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = temp;
                    count ++;
                }
            }
            if(count == 0)
                break;
        }
        return arr;
    }
   
    private static void showAll(int[] arr){
        for (int i : arr) {
            System.out.println("" + i);
        }
    }

}