Home » Android » Android custom view should extend AppCompatTextView

Android custom view should extend AppCompatTextView

Posted by: admin April 23, 2020 Leave a comment

Questions:

I created simple custom view which that extended from TextView, in Android Studio i get this wanrning

This custom view should extend android.support.v7.widget.AppCompatTextView instead

and i can’t use clickable propertise, for example:

   <com.myapp.test.Widgets.FontAwesome
       android:layout_width="60dp"
       android:layout_height="match_parent"
       android:layout_marginRight="5dp"
       android:background="?selectableItemBackground"
       android:gravity="center"
       android:clickable="@{()->presenter.clickOnSend()}"
       android:text="@string/font_icon_post_message"
       android:textColor="@color/gray_text_color"
       android:textSize="40sp"/>

i get this error for clickable propertise:

Error:(91, 46) Cannot find the setter for attribute 'android:clickable' with parameter type lambda on com.myapp.test.Widgets.FontAwesome. 

enter image description here

my custom class:

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class FontAwesome extends TextView {
    public FontAwesome(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public FontAwesome(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FontAwesome(Context context) {
        super(context);
        init();
    }

    private void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                "fonts/fontawesome.ttf");
        setTypeface(tf);
    }
}

how can i resolve this problem?

How to&Answers:

UPDATE: If you’re using androidx libraries instead of the (legacy) v7 support libraries (which you ought to do so now…), please use this instead:

import androidx.appcompat.widget.AppCompatTextView;

OLD ANSWER: (still useful if you’ve not migrated to androidx yet…)

This custom view should extend
android.support.v7.widget.AppCompatTextView instead

It’s a Warning, not an Error.

Instead of

public class FontAwesome extends TextView

You should use AppCompatTextView

public class FontAwesome extends AppCompatTextView 

Answer:

I was with a similar problem but it’s fixed. It is probably you only see the result on execution time the first time. I didn’t test to rebuild project again without launch the app.

 import android.content.Context;
    import android.graphics.Color;
    import android.support.v7.widget.AppCompatTextView;
    import android.util.AttributeSet;

    public class CustomTxtView extends AppCompatTextView {


    public CustomTxtView(Context context) {
        super(context);
        init();
    }

    public CustomTxtView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomTxtView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init(){
        setText("Hello World");
        setTextColor(Color.RED);
    }
}