JeffLemon
JeffLemon

Reputation: 273

Android add a class

My TextSwitcher class extends view. How can I add it in to my main file and envoke methods in the class?

Ive tried:

addView(new TextSwitcher(this));

But no luck.

Upvotes: 0

Views: 92

Answers (1)

Seva Alekseyev
Seva Alekseyev

Reputation: 61380

By "my main file", you mean the main layout file? First, you can do that straight in the layout XML. The syntax is:

<com.mypackage.TextSwitcher android:id="@+id/TheSwitcher" />

and don't forget width/height/weight/etc.

Second, you can add it programmatically via addView(). Be careful to pick the right container object for that. And don't forget to set its layout params, which must be compatible with the container (for objects in LinearLayout, you provide params as as instance of LinearLayoutParams, etc.).

I recommend the first approach though. Layout params are not particularly code-friendly, especially if you want density-independent sizing.

Upvotes: 1

Related Questions