Reputation: 12207
I'm trying to make a compound control, and set custom attributes for it.
The class for the control:
package hu.ppke.itk.kozcs.android.activities;
import hu.ppke.itk.kozcs.android.project.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SettingBox extends RelativeLayout {
public SettingBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
String service = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater) getContext().getSystemService(service);
RelativeLayout layout = (RelativeLayout) li.inflate(R.layout.setting_box, this, true);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SettingBox);
String title = a.getString(R.styleable.SettingBox_title);
String description = a.getString(R.styleable.SettingBox_description);
TextView titleText = (TextView) layout.findViewById(R.id.title);
TextView descriptionText = (TextView) layout.findViewById(R.id.description);
titleText.setText(title);
descriptionText.setText(description);
a.recycle();
}
}
res/values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SettingBox">
<attr name="title" format="string" />
<attr name="description" format="string" />
</declare-styleable>
</resources>
layout xml for the control (setting_box.xml):
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="@+id/checkbox"
android:text="@string/use_gps"
android:textColor="@android:color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/title"
android:layout_marginLeft="5dp"
android:layout_toLeftOf="@+id/checkbox"
android:text="@string/gps_descr"
android:textSize="13sp" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp" />
</merge>
and finally the layout where i use this control (settings.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/hu.ppke.itk.kozcs.android.activities"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<hu.ppke.itk.kozcs.android.activities.SettingBox
andrid:id="@+id/test_setting_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:description="description"
app:title="title" />
</LinearLayout>
Every time i save the last xml, i got this error message:
settings.xml:9: error: Error parsing XML: unbound prefix
Also there is an error in the log:
hu.ppke.itk.kozcs.android.activities.SettingBox failed to instantiate.
What am i missing here?
Upvotes: 0
Views: 512
Reputation: 8559
Your android:id
attribute is called andrid:id
.
<hu.ppke.itk.kozcs.android.activities.SettingBox
andrid:id="@+id/test_setting_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:description="description"
app:title="title" />
Upvotes: 3