Sep 3, 2012

Home Screen Widget step-by-step - implement configure activity

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

In the previous articles, we have NO android:configure defined in our App widget provider XML, /res/xml/widgetproviderinfo.xml. So, the App Widget Provider will be called when user add our widget. Optionally we can implement our Configure Activity, it will be called at the first time user add the Widget, instead of App Widget Provider. Such that user can do something when he add the widget.

Configure Activity



Modify /res/xml/widgetproviderinfo.xml to add android:configure="com.example.androidhomewidget.ConfigureWidgetActivity".
<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"
    android:configure="com.example.androidhomewidget.ConfigureWidgetActivity">
</appwidget-provider>


ConfigureWidgetActivity.java
package com.example.androidhomewidget;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.TextView;

public class ConfigureWidgetActivity extends Activity {
 
 int appWidgetId;
 Button configureOkButton;
 TextView wId;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.configure_layout);
  wId = (TextView)findViewById(R.id.wid);
  configureOkButton = (Button)findViewById(R.id.confighreok);
  configureOkButton.setOnClickListener(configureOkButtonOnClickListener);
  
  Intent intent = getIntent();
  Bundle extras = intent.getExtras();
  
  if (extras != null) {
   appWidgetId = extras.getInt(
     AppWidgetManager.EXTRA_APPWIDGET_ID,
     AppWidgetManager.INVALID_APPWIDGET_ID);
   
   wId.setText("appWidgetId = " + appWidgetId);
   
  }else{
   finish();
  }
      
 }
 
 OnClickListener configureOkButtonOnClickListener
 = new OnClickListener(){

  @Override
  public void onClick(View v) {

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());

    RemoteViews remoteViews = new RemoteViews(
      getApplicationContext().getPackageName(), 
      R.layout.widget_layout);
    
    remoteViews.setTextViewText(R.id.widget_id, String.valueOf(appWidgetId));
    remoteViews.setTextViewText(R.id.widget_status, "Waiting...");
    
    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

    Intent intent = new Intent();
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    setResult(RESULT_OK, intent);
    finish();
   
  }};

}


Create /res/layout/configure_layout.xml to define the layout of the configure activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Widget Configure Activity" />
    <TextView
        android:id="@+id/wid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/confighreok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="OK" />

</LinearLayout>


Modify AndroidManifest.xml to add <activity> of ConfigureWidgetActivity.java.
<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>

<!--  define Configure Activity -->
        <activity android:name=".ConfigureWidgetActivity">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
        </activity>
        
    </application>

</manifest>



No comments:

Post a Comment

Infolinks In Text Ads