Aug 30, 2012

Home Screen Widget step-by-step - modify our App Widget Provider (AppWidgetProvider) to update widget RemoteViews

It's part of the Home Screen Widgets step-by-step series.

Up to last post, we have a dummy home screen widget without any function. In this step, we are going to modify WidgetProvider.java, override onUpdate() method to updateAppWidget() with RemoteViews.

override onUpdate() method to updateAppWidget() with RemoteViews.

WidgetProvider.java
package com.example.androidhomewidget;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WidgetProvider extends AppWidgetProvider {

 @Override
 public void onDeleted(Context context, int[] appWidgetIds) {
  // TODO Auto-generated method stub
  super.onDeleted(context, appWidgetIds);
 }

 @Override
 public void onDisabled(Context context) {
  // TODO Auto-generated method stub
  super.onDisabled(context);
 }

 @Override
 public void onEnabled(Context context) {
  // TODO Auto-generated method stub
  super.onEnabled(context);
 }

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

 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager,
   int[] appWidgetIds) {
  
  for(int i = 0; i < appWidgetIds.length; i++){
   
   int id = appWidgetIds[i];
   
   Toast.makeText(context, 
     "onUpdate: " + String.valueOf(id), 
     Toast.LENGTH_LONG).show();
   
   RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
   
   remoteViews.setTextViewText(R.id.widget_id, String.valueOf(id));
   
   SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");
   String now = simpleDateFormat.format(new Date());
   remoteViews.setTextViewText(R.id.widget_status, now);
   
   appWidgetManager.updateAppWidget(id, remoteViews);
   
   super.onUpdate(context, appWidgetManager, appWidgetIds);
  }
 }

}


Refer to our app widget provider, onUpdate() will be call every 30 minutes (1800000 millisecond). It's a approximated reference, no guarantee how accurate is it.

Aug 29, 2012

Home Screen Widget step-by-step - define Widget Provider Receiver in AndroidManifest.xml

It's part of the Home Screen Widgets step-by-step series.

In this step, modify AndroidManifest.xml to add <receiver> to define Widget Provider Receiver.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidhomewidget"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
<!-- define Widget Provider Receiver -->        
        <receiver android:name=".WidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/widgetproviderinfo" />
        </receiver>
        
    </application>

</manifest>


Pay attention how android:name=".WidgetProvider" and android:resource="@xml/widgetproviderinfo" correspond to WidgetProvider.java and /res/xml/widgetproviderinfo.xml respectively.

After modified AndroidManifest.xml to define Widget Provider Receiver, we can add our dummy Widget on Home Screen. Up to here, the widget do nothing actually!

The dummy Home Screen Widget

Aug 28, 2012

Home Screen Widget step-by-step - create our AppWidget Provider by extending AppWidgetProvider.

It's part of the Home Screen Widgets step-by-step series.

android.appwidget.AppWidgetProvider is a convenience class to aid in implementing an AppWidget provider. Everything you can do with AppWidgetProvider, you can do with a regular BroadcastReceiver. AppWidgetProvider merely parses the relevant fields out of the Intent that is received in onReceive(Context,Intent), and calls hook methods with the received extras.

Extend this class and override one or more of the onUpdate(Context, AppWidgetManager, int[]), onDeleted(Context, int[]), onEnabled(Context) or onDisabled(Context) methods to implement your own AppWidget functionality.


At this moment, just implement our dummy AppWidgetProvider class, WidgetProvider.java. We will include it in our AndroidManifest.xml later.

package com.example.androidhomewidget;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;

public class WidgetProvider extends AppWidgetProvider {

 @Override
 public void onDeleted(Context context, int[] appWidgetIds) {
  // TODO Auto-generated method stub
  super.onDeleted(context, appWidgetIds);
 }

 @Override
 public void onDisabled(Context context) {
  // TODO Auto-generated method stub
  super.onDisabled(context);
 }

 @Override
 public void onEnabled(Context context) {
  // TODO Auto-generated method stub
  super.onEnabled(context);
 }

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

 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager,
   int[] appWidgetIds) {
  // TODO Auto-generated method stub
  super.onUpdate(context, appWidgetManager, appWidgetIds);
 }

}

Aug 24, 2012

Get 3gp link of Youtube video

The post "A simple example using VideoView to play 3gp from YouTube" describe how to play Youtube video in 3gp format on Android. But how can you get the 3gp link of Youtube video?

For example, you want to embed the 3gp video of "Google I/O 2012 - What's New in Android?" in your app.

Right click the video to copy Video URL.



Paste and open the link, replace the leading "www.youtube.com" with "m.youtube.com" to get the mobile version.



Corresponding mobile version of the video will be loaded. Right click on the video to Copy Link Address. It's the 3gp link of the video:

rtsp://v4.cache7.c.youtube.com/CjYLENy73wIaLQky7ThXrRjPYRMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYKjR78WV1ZH5Tgw=/0/0/0/video.3gp

You can copy this link to replace SrcPath in the example "A simple example using VideoView to play 3gp from YouTube".



Aug 23, 2012

Home Screen Widget step-by-step - define widget layout

It's the second step to create Home Screen Widgets.

Refer to the last article to "Define app widget provider in XML". It's specified android:initialLayout="@layout/widget_layout", means we have a xml file named widget_layout.xml in /res/layout/ folder, to define our widget layout.

/res/layout/widget_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/widget_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="#F06030"
        android:textColor="#101010"
        android:textSize="30sp"
        android:text="id"/>
    <TextView
        android:id="@+id/widget_status"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/background_dark"
        android:textColor="@android:color/white"
        android:textSize="30sp"
        android:text="status"/>
</LinearLayout>

Home Screen Widget step-by-step - define app widget provider in XML

It's the first step of Home Screen Widgets: create a XML file, /res/xml/widgetproviderinfo.xml, to define  a minimal app widget provider info.

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="146dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="1800000"
    android:initialLayout="@layout/widget_layout">
</appwidget-provider>

Where:
  • android:minWidth and android:minHeight specify the minimum amount of space the App Widget consumes by default. The default Home screen positions App Widgets in its window based on a grid of cells that have a defined height and width. If the values for an App Widget's minimum width or height don't match the dimensions of the cells, then the App Widget dimensions round up to the nearest cell size.
  • android:updatePeriodMillis specify how often, in milliseconds, that this AppWidget wants to be updated. The AppWidget manager may place a limit on how often a AppWidget is updated.

    Note: Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes.
  • android:initialLayout specify the resource id of the initial layout for this AppWidget. We will implement it later.


Next Step >> Define widget layout.

Aug 19, 2012

Home Screen Widgets

"Home Screen Widgets" is widget that people can drop onto their home screen and interact with. It can provide a quick glimpse into full-featured apps, such as showing upcoming calendar events, or viewing details about a song playing in the background.

When widgets are dropped onto the home screen, they are given a reserved space to display custom content provided by your app. Users can also interact with your app through the widget, for example pausing or switching music tracks. If you have a background service, you can push widget updates on your own schedule, or the AppWidget framework provides an automatic update mechanism.


In the coming articles, I will show how to create Home Screen Widgets step-by-step.

Notes:
  • Starting in Android 3.1, developers can make their homescreen widgets resizeable — horizontally, vertically, or on both axes. Users touch-hold a widget to show its resize handles, then drag the horizontal and/or vertical handles to change the size on the layout grid.
  • Starting from Android 4.0, home screen widgets should no longer include their own padding. Instead, the system now automatically adds padding for each widget, based the characteristics of the current screen. This leads to a more uniform, consistent presentation of widgets in a grid.


Step-by-step to create Home Screen Widget:


Aug 3, 2012

Apply translate effect in activity transition

Create XML files to define shift in/shift out effect:

/res/anim/shift_in.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromXDelta="-100%"
    android:toXDelta="0.0"
    android:duration="1000" />


/res/anim/shift_out.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromXDelta="0.0"
    android:toXDelta="100%"
    android:duration="1000" />


Apply the effect to activity by calling overridePendingTransition(R.anim.shift_in, R.anim.shift_out).
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        overridePendingTransition(R.anim.shift_in, R.anim.shift_out);
    }



Aug 2, 2012

Apply fade-in, fade-out effect in activity transition

Create XML files to define fadein/fadeout effect:

/res/anim/fadein.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="0.0" 
    android:toAlpha="1.0" 
    android:duration="3000" />


/anim/fadeout.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="1.0" 
    android:toAlpha="0.0" 
    android:duration="3000" />


Apply the effect to activity by calling overridePendingTransition(R.anim.fadein, R.anim.fadeout).
package com.example.androidfadein;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        overridePendingTransition(R.anim.fadein, R.anim.fadeout);
        
    }

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
}



Infolinks In Text Ads