Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Compound Controls With Custom XML Attributes

I'm been trying to combine a TextView and an EditText into one compound control which uses custom xml elements to pass in default values for each individual element. I've been looking at the tutorials/docs here:
Building Compound Controls
Passing Custom Attributes

What I have so far.

Attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FreeText">
        <attr name="label" format="string" />
        <attr name="default" format="string" />
    </declare-styleable>
</resources>

My Main Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <com.example.misc.FreeText  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        myapp:label="label"
        myapp:default="default"
    />
</LinearLayout>

My Compound Control, FreeText:

public class FreeText extends LinearLayout {

    TextView label;
    EditText value;

    public FreeText(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.setOrientation(HORIZONTAL);

        LayoutParams lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
        lp.weight = 1;

        label = new TextView(context);
        addView(label, lp);

        value = new EditText(context);
        addView(value, lp);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FreeText);
        CharSequence s = a.getString(R.styleable.FreeText_label);
        if (s != null) { 
            label.setText(s);
        }

        a.recycle();
    }
}

When I run the program I see the views OK but the value of my CharSequence, s, is always null. Can someone tell me where I'm going wrong?

like image 929
Mini Dez Avatar asked Feb 16 '11 09:02

Mini Dez


People also ask

How do I use attribute attributes in XML?

Attributes can be used to control the XML serialization of an object or to create an alternate XML stream from the same set of classes. For more details about creating an alternate XML stream, see How to: Specify an Alternate Element Name for an XML Stream.

How to create a custom component with attributes in Java?

Create an XML res/values/attrs.xml file to define new attributes alongwith their data type. Create src/DateView.java file and add the code to define your custom component.

How do I create custom controls in XAML?

Custom Controls There are two approaches to creating custom controls in XAML: user controls and templated controls. User controls are an easy, designer-­friendly approach to creating a reusable layout.

What are custom properties in XAML?

Custom properties. The control exposes properties to allow the consuming developer to influence the content of the layout. This has been done in a way to fully support XAML data binding. API syntax. The control supports an intuitive approach that allows developers to declare their content with literal strings and in a straightforward way.


1 Answers

I hate it when you notice the problem right after you ask for help.

The problem was that my namespace for my custom XML elements should have been like so:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <com.example.misc.FreeText  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        myapp:label="label"
        myapp:default="default"
    />
</LinearLayout>
like image 133
Mini Dez Avatar answered Nov 15 '22 22:11

Mini Dez