To Get a CustomView Instance

Asked 2 years ago, Updated 2 years ago, 66 views

Is it possible to treat multiple views together as a single view on Android?

For example, suppose you have a layout that is represented by the following outline:

<!--layout/my_view.xml (approximate)-->
<LinearLayout>
    <TextView android:id="@+id/title"/>
    <TextView android:id="@+id/content"/>
</LinearLayout>

And let's say you have MyView that inherited this route, LinearLayout

class MyView extensions LinearLayout {
    TextView titleTV, contentTV;

    // Constructors and all

    publicMyView(Context context, AttributeSetattrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
        View root=LayoutInflater.from(context).inflate(R.layout.my_view,this);
        titleTV=root.findViewById(R.id.title);
        contentTV=root.findViewById(R.id.content);

        //TypedArray, etc.
    }

    public void setTitle (String title) {
        titleTV.setText(title);
    }

    // All of the following methods
}

Suppose you have the following activities to take advantage of it:
Layout Files

<!--layout/activity_main.xml (summary)-->
<LinearLayout>
    <***.***.MyView android:id="@+id/my_view_1"/>
    <***.***.MyView android:id="@+id/my_view_2"/>
</LinearLayout>

Class

class MainActivity extensions AppCompatActivity{
    @ Override
    protected void onCreate (Bundle savedInstanceState) {
        super.oncreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view=findViewById(R.id.my_view_1);
        // The view instance of LinearLayout is displayed, so I cannot do the following.

        MyView myView= (MyView) findViewById (R.id.my_view_2); // Fails without cast
        myView.setTitle("Title");
    }
}

As written in the code, the instance obtained by findViewbyId becomes the instance of the root and the critical method is not available.
I remember inheriting LinearLayout, but if I inherit View instead, the View is not a ViewGroup, so it doesn't match the inflate argument.

class MyView extensions View {
    publicMyView(Context context, AttributeSetattrs, int defStyleAttr){
        View root=LayoutInflater.from(context).inflate(R.layout.my_view,this);
        // This is not a ViewGroup, so x

        View root=LayoutInflater.from(context).inflate(R.layout.my_view, null);
        // Error running x
    }
}

Also, I saw Q&A and DevelopersBlogs that should be enclosed with <merge>, but the above problem did not solve it well.
How can I get an instance of CustomView?

/*Note*/

First of all, I would like to report the resolution.(I don't understand...)
About 1
·This time, I think there is a problem with the class, and I have extracted the minimum number that shows the hierarchy.
About 2
·I just called this (context, attrs, defStyleAttr) internally, so I omitted it
About 3
·Explicit cast is omitted due to generic type because API Level 26 was used.

I should have described all of them in detail

I was programming on Android Studio Preview, so I rewritten it on Android Studio 2.6 and it worked fine.
So I ran it again to check for errors in the Preview version, and this time it ran smoothly.
I'm not sure, but it's solved.
I have not rewritten the affected part from the time the error occurred until it was successfully executed, and I am not sure why the type suddenly changed.

android android-layout

2022-09-30 17:00

2 Answers

You should be able to do it

MyView myView=(MyView) findViewById(R.id.my_view_1);
myView.setTitle("Title");


2022-09-30 17:00

I think it was written in a hurry, but I think there are some mistakes in the description.

Otherwise, I think you can get a more accurate answer by pasting the log.

By the way, #inflate(Context,int,ViewGroup) is available for the class that inherits View (although I'm just doing the same internally…) for your information.


2022-09-30 17:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.