pixelscreen
pixelscreen

Reputation: 1975

Doubt regarding Views

Suppose MyView is a class derived from View and mView is a variable of type MyView. How to display mView when the android app is started?

Upvotes: 0

Views: 168

Answers (2)

Aleadam
Aleadam

Reputation: 40401

Short answer: Like any other View, use setContentView(View v), although you will probably want to add it to a ViewGroup first (usually one of the many layout classes).

Long answer: Rather than answering the basics in each of your questions, I believe the best the community can do for you here is to point you to the sources to learn android basic programming. One of the best sources to start is the android.com website itself. Please, take your time to read the Android Developers Guide (link: http://developer.android.com/guide/topics/fundamentals.html).

Also, there are several questions here in SO that link to many resources to learn:

Also, in order to avoid getting close votes in your questions, please take a moment to read the FAQ and these tips

Upvotes: 2

Gabriel Negut
Gabriel Negut

Reputation: 13960

Let's say you have a LinearLayout in your main.xml.

In the onCreate method, you will have:

MyView mView = new MyView(this);
mView.setId(ID_OF_YOUR_CUSTOM_VIEW);
//modify next line as needed
LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
mView.setLayoutParams(layoutParams);
LinearLayout layout = (LinearLayout) findViewById(R.id.ID_OF_YOUR_LAYOUT);
layout.addView(mView);

Upvotes: 0

Related Questions