Wednesday 24 April 2013

Android: Widget and APP in the same APK File


 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_action_emo_laugh"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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>
        <receiver android:name="MainActivityProvider" >
            <intent-filter>
                <action android:name="CHANGE_TEXT" />
            </intent-filter>

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

        <activity android:name="LaunchActivity" 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>

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>



Android: Widget with TextView and Button(TextView changes when we click on Button)

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_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:text="Click"
        android:textColor="#000000" />


</RelativeLayout>


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>

MainActivity.java

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

RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.activity_main);
remoteViews.setOnClickPendingIntent(R.id.wid_button,
buildButtonPendingIntent(context));

pushWidgetUpdate(context, remoteViews);
}

public static PendingIntent buildButtonPendingIntent(Context context) {
Intent intent = new Intent();
intent.setAction("CHANGE_TEXT");
return PendingIntent.getBroadcast(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);
}

}

MainActivityProvider.java

public class MainActivityProvider extends BroadcastReceiver {

private static int clickCount = 0;

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("CHANGE_TEXT")) {
updateWidgetText(context);
}
}

private void updateWidgetText(Context context) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.activity_main);
remoteViews.setTextViewText(R.id.textView1, getImageToSet());
// remoteViews.setOnClickPendingIntent(R.id.wid_button,
// MainActivity.buildButtonPendingIntent(context));

MainActivity.pushWidgetUpdate(context.getApplicationContext(),
remoteViews);
}

private CharSequence getImageToSet() {
clickCount++;
return clickCount % 2 == 0 ? "Hai" : "Bye";
}
}


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>


 <receiver android:name="MainActivityProvider" >
            <intent-filter>
                <action android:name="CHANGE_TEXT" />
            </intent-filter>

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

Android: Widget with TextView

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" />


</RelativeLayout>


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>

MainActivity.java

public class MainActivity extends AppWidgetProvider {

}

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>