I'm attempting to create a custom widget which I can use in my activity's layout. I've created a class for the widget which extends View.
import android.app.Service;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import be.robinj.ubuntu.R;
public class AppLauncher extends View
{
private String name;
private String description;
private boolean special;
private AppIcon icon;
private View view;
public AppLauncher (Context context, AttributeSet attrs)
{
super (context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService (Service.LAYOUT_INFLATER_SERVICE);
this.view = inflater.inflate (R.layout.widget_applauncher, null, false);
TypedArray styleAttrs = context.getTheme ().obtainStyledAttributes (attrs, R.styleable.AppLauncher, 0, 0);
this.name = styleAttrs.getString (R.styleable.AppLauncher_label);
this.description = styleAttrs.getString (R.styleable.AppLauncher_description);
this.special = styleAttrs.getBoolean (R.styleable.AppLauncher_special, false);
this.icon = new AppIcon (styleAttrs.getDrawable (R.styleable.AppLauncher_icon));
}
...
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="64dp"
android:layout_height="64dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff9243"
android:layout_margin="6dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/launcher_icon_bg">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imgIcon"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AppLauncher">
<attr name="label" format="string" />
<attr name="description" format="string" />
<attr name="icon" format="integer" />
<attr name="special" format="boolean" />
</declare-styleable>
</resources>
That's all fine, but the Java code is only going to contain the logic part, preferably. What I can't figure out is how I can specify which layout XML file should be used for my custom view/widget. Inflating the layout presumably loads it. The inflate () methods require a second and third parameter, though. Every example I can find shows this being passed as the second parameter, but the second parameter should be a ViewGroup, while this extends View. Where do I magically get this required ViewGroup object from? Passing null and false does not throw an error, but neither does it do anything else (nothing shows up when I run the app).