Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Add missing code-references

...

  1. Create an XML layout that uses a LinearLayout to display a TextView and two Button widgets: 

    Code Block
    title/platform/android/res/layout/customview.xml
    languagehtml/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="horizontal" >
        <TextView android:id="@+id/message"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textSize="18dp"
                  android:text="Default text" />
        <Button android:id="@+id/okbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="OK" />
        <Button android:id="@+id/cancelbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Cancel" />
    </LinearLayout>
  2. In the application, create a RemoteViews object using the XML layout as a reference, then modify the RemoteViews by changing the default text and binding PendingIntents to the buttons: 

    Code Block
    title/Resources/app.js
    languagejavascript
    var customView = Ti.Android.createRemoteViews({
        layoutId: Ti.App.Android.R.layout.customview
    });
     
    // Blank intents (example) that will remove the notification when the user taps one of them
    var downloadIntent = Ti.Android.createPendingIntent({intent: Ti.Android.createIntent()});
    var cancelIntent = Ti.Android.createPendingIntent({intent: Ti.Android.createIntent()});
    
    
    // Reference elements in the layout by prefixing the IDs with 'Ti.App.Android.R.id'
    customView.setTextViewText(Ti.App.Android.R.id.message, "Update available!");
    customView.setTextViewText(Ti.App.Android.R.id.okbutton, "Download");
    customView.setOnClickPendingIntent(Ti.App.Android.R.id.okbutton, downnloadIntentdownloadIntent);
    customView.setTextViewText(Ti.App.Android.R.id.cancelbutton, "Not now");
    customView.setOnClickPendingIntent(Ti.App.Android.R.id.cancelbutton, cancelIntent);
  3. Finally, assign the RemoteViews object to the Notification object's contentView property. 

    Code Block
    title/Resources/app.js
    languagejavascript
    var notification = Titanium.Android.createNotification({contentView: customView});

...