1

Here I made my own custom widgets in android and they all work fine. However, i have like 20 xml files and I don't want to change the EditText in xml to com.example.customwidget.MyEditText in all the xml layouts that I have. Any fast way to do that?

For example: The below xml won't work. It will crash the application because there is no MyEditText in the android sdk widgets, that's my own widget.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <MyEditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </MyEditText>

</RelativeLayout>

However, This one will work:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.customwidget.MyEditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </com.example.customwidget.MyEditText>

</RelativeLayout>

What I would like to do is to simply keep the EditText tag in the xml. However, I want to by my own custom EditText.

Ok I know I will have to change the class name first to be EditText instead of MyEditText but how can I let all the XML Layout files know that I want my custom EditText and not the native one?

1 Answer 1

2

Implement custom LayoutInflater.Factory and set it to your activity's LayoutInflater

Here is an example of such factory:

public class MyLayoutInflaterFactory implements LayoutInflater.Factory
{
    public View onCreateView(String name, Context context, AttributeSet attrs)
    {
        if ("EditText".equals(name))
            return new MyEditText(context, attrs);
        return null;
    }
}

Then you need to use it in your activity:

public class MyActivity extends Activity
{
    ⋮

     LayoutInflater layoutInflater;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        layoutInflater = LayoutInflater
            .from(this)
            .cloneInContext(this)
            .setFactory(new MyLayoutInflaterFactory());

         setContentView(layoutInflater.inflate(R.layout.my_activity, null));
    }

    @Override
    public LayoutInflater getLayoutInflater()
    {
        return layoutInflater;
    }

    ⋮
}
Sign up to request clarification or add additional context in comments.

7 Comments

no that's not what I want i'm sure you got me wrong. All I want is that when I use EditText in xml it won't use the native EditText it will use My EditText that I modified
This is the way to do it.
ah i think i'm starting to get you now.. but i have no idea how to do it, any tip is appreciated :)
The LayoutInflater.Factory.onCreateView gets a name that is found in the XML. If that name is "EditText", you would create your EditText (using supplied context and attribute set), otherwise you would just return null and let the original factory create other views.
Ok thank you i'll try to do it and will post a solution when done
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.