Gabriel Llamas
Gabriel Llamas

Reputation: 18437

How to use Compound Controls

I've created a custom ViewGroup based on a LinearLayout.

ClearableEditText.java

package test.todolist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class ClearableEditText extends LinearLayout{
    private EditText editText;
    private Button button;

    public ClearableEditText (Context context){
        super (context);

        String service = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li = (LayoutInflater)getContext ().getSystemService (service);
        li.inflate (R.layout.clearable_edit_text, this, true);

        editText = (EditText)findViewById (R.id.clearEditText);
        button = (Button)findViewById (R.id.clearButton);

        configButton ();
    }

    private void configButton (){
        button.setOnClickListener (new Button.OnClickListener (){
            public void onClick (View v){
                editText.setText ("");
            }
        });
    }
}

clearable_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/clearEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />
    <Button
        android:id="@+id/clearButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/clear"
    />
</LinearLayout>

How can I use ClearableEditText now?

I've tried to put a node inside a layout (main.xml) in 2 ways:

<test.todolist.ClearableEditText/>

and

<test.todolist.clearable_edit_text/>

but none of them have worked.

My main.xml:

<?xml version="1.0" encoding="utf-8"?>
<test.todolist.ClearableEditText/>

My ToDoList.java (main activity):

package test.todolist;

import android.app.Activity;
import android.os.Bundle;

public class ToDoList extends Activity{
    @Override
    public void onCreate (Bundle savedInstanceState){
        super.onCreate (savedInstanceState);
        setContentView (R.layout.main);
    }
}

Thanks.

Upvotes: 3

Views: 3109

Answers (1)

Gabriel Llamas
Gabriel Llamas

Reputation: 18437

Solved. The main.xml should be like:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <test.todolist.ClearableEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</merge>

First, merge tag because it is needed when using custom views. My custom view have a LinearLayout root, so it's inefficient if I set another LinearLayout or FrameLayout root in main.xml to use my custom view. merge solves that.

And second, all views must have the layout_width and layout_height attributes.

Upvotes: 4

Related Questions