Reputation: 7809
I'm trying to create a square layout for a widget.
I have the following layout class:
package com.trollhammaren;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
public class SquareLayout extends LinearLayout {
// constructors
public SquareLayout(Context context) {
super(context);
}
public SquareLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
// methods
@Override
protected void onMeasure(int width, int height) {
Log.v("widget", "resized");
super.onMeasure(width, height);
}
}
And the following xml:
<?xml version="1.0" encoding="utf-8"?>
<com.trollhammaren.SquareLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dip"
android:background="@drawable/myshape" >
</com.trollhammaren.SquareLayout>
Instead of a square I see a rectangle widget with the text "Problem loading widget". When I place the widget I see the following message in logcat:
Error inflating AppWidget AppWidgetProviderInfo(provider=ComponentInfo{com.trollhammaren.wakeonlandroid/com.trollhammaren.wakeonlandroid.WidgetProvider}): android.view.InflateException: Binary XML file line #2: Error inflating class com.trollhammaren.SquareLayout
If I change the layout to LinearLayout, I do see a normal LinearLayout, but it's not square.
What am I doing wrong?
Upvotes: 2
Views: 550
Reputation: 10184
Inside of the Widget you can only have Views and Layouts that are tagged as RemoteViews. So I don't think you can send your own custom class over to the AppWidget context. It has to be done with only the views within the OS version that are tagged this way.
To be clear the way these work, the context which hosts the Widget is not your process. So the AppWidget's context basically sends everything via an RPC like mechanism from your app to another context's view. If it supported custom widgets, that would allow you basically to send arbitrary code to the other apps process and assume a lot more permissions than I bet they would really like you to have. Plus it would be nasty for the IPC, since you would need to parcel the entire class hierarchy and load it in a separate classloader and all that stuff just to guarantee dependancies are upheld.
Upvotes: 2