AngularJS – simple MVC app

<!doctype html>
<html lang="en" ng-app>
<head>
    <meta charset="UTF-8">
    <title>Angular Demo</title>
    <script src="lib/angular/angular.min.js"></script>
</head>
<body>
    <div ng-controller = "MyController">
        <h1>{{author.name}}</h1>
        <p>{{ author.title + ','+ author.company}}</p>
    </div>

    <script>
        function MyController($scope){
            $scope.author = {
            'name' : 'Joe Richard',
            'title': 'Android Developer',
            'company' : 'Sunet Technologies'
            }
        }
    </script>
</body>
</html>

Android – Image resource ids in one array. How to use?

    <array name="sun_drawable_list">
        <item>@drawable/sun_0</item>
        <item>@drawable/sun_1</item>
        <item>@drawable/sun_2</item>
        <item>@drawable/sun_3</item>
        <item>@drawable/sun_4</item>
        <item>@drawable/sun_5</item>
        <item>@drawable/sun_6</item>
        <item>@drawable/sun_7</item>
        <item>@drawable/sun_8</item>
    </array>

values/strings.xml

        TypedArray sun_drawable_list = context.getResources()
                .obtainTypedArray(R.array.sun_drawable_list);

        ivSun.setImageResource(sun_drawable_list.getResourceId(0, -1));

ImageView ivSun will be set to this drawable: @drawable/sun_0

Java – Convert time in ‘yyyy-MM-dd HH:mm:ss’ format to ‘HH:mm’ format

    public static String convertLongTimeToShort(String time){
        try {

            SimpleDateFormat input_sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat output_sdf = new SimpleDateFormat("HH:mm");

            Date date = input_sdf.parse(time);
            String formattedTime = output_sdf.format(date);
            return formattedTime;

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

Android – How to get mobile data state(on/off)?

    public boolean getMobileDataEnabled(Context context) {
        try {
            ConnectivityManager mcm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            Class ownerClass = mcm.getClass();
            Method method = ownerClass.getMethod("getMobileDataEnabled");
            return (Boolean) method.invoke(mcm);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return false;
    }

Use this method to get mobile data state whether it is turned on or off

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

Do not forget to add this permission to get network state.

Android – Widget on click listener easy example

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/ivWifi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/wifi_on"
        android:clickable="true"/>

</LinearLayout>

layout_wifi.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="72dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="86400000"
    android:initialLayout="@layout/layout_wifi">
</appwidget-provider>

res/xml/widget_wifi.xml

        <receiver android:name=".WifiReceiver" android:icon="@drawable/wifi_on">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/widget_wifi" />
        </receiver>

AndroidManifest.xml

public class WifiReceiver extends AppWidgetProvider {

    private static final String WIFI_WIDGET_CLICKED    = "WIFI_WIDGET_CLICKED";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_wifi);
        ComponentName watchWidget = new ComponentName(context, WifiReceiver.class);

        remoteViews.setOnClickPendingIntent(R.id.ivWifi, getPendingSelfIntent(context, WIFI_WIDGET_CLICKED));
        appWidgetManager.updateAppWidget(watchWidget, remoteViews);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onReceive(context, intent);

        if (WIFI_WIDGET_CLICKED.equals(intent.getAction())) {

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_wifi);
            ComponentName watchWidget = new ComponentName(context, WifiReceiver.class);

            appWidgetManager.updateAppWidget(watchWidget, remoteViews);

            Toast.makeText(context, WIFI_WIDGET_CLICKED, Toast.LENGTH_SHORT).show();

        }
    }

    protected PendingIntent getPendingSelfIntent(Context context, String action) {
        Intent intent = new Intent(context, getClass());
        intent.setAction(action);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }

}

WifiReceiver.java