Tuesday, 23 April 2013

Android: Widget with Button click to launch an Activity


layout/activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        android:textColor="#000000" />



 <Button
        android:id="@+id/wid_launch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:text="Launch"
        android:textColor="#000000" />



</RelativeLayout>

layout/launch.xml


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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity Launched"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


res/xml/hello_widget_provider.xml


<?xml version="1.0" encoding="utf-8"?>

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/activity_main"
    android:minHeight="100dp"
    android:minWidth="300dp"
    android:updatePeriodMillis="1000"
    >
</appwidget-provider>

LaunchingActivity.java


public class LaunchActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// change to our configure view
setContentView(R.layout.launch);
// don't call 'this', use 'getApplicationContext()', the activity-object
// is
// bigger than just the context because the activity also stores the UI
// elemtents
Toast.makeText(getApplicationContext(), "We are in LaunchActivity",
Toast.LENGTH_SHORT).show();
}
}

MainActivity.java



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


remoteViews.setOnClickPendingIntent(R.id.wid_launch,
actionPendingIntent(context));

pushWidgetUpdate(context, remoteViews);

}

public static PendingIntent actionPendingIntent(Context context) {
Intent intent = new Intent(context,LaunchActivity.class);
intent.setAction("LAUNCH_ACTIVITY");
return PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}

public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {

ComponentName myWidget = new ComponentName(context, MainActivity.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}

}



AndroidManifest.xml

In <application> add this


 <receiver android:name="MainActivity" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/hello_widget_provider" />
        </receiver>


<activity android:name="LaunchActivity" >
            <intent-filter>
                <action android:name="LAUNCH_ACTIVITY" />
            </intent-filter>
        </activity>



No comments:

Post a Comment