A N M Bazlur Rahman
A N M Bazlur Rahman

Reputation: 2300

how to to create custom & composite view in android

I would like to create a custom and composite view in android. My custom view will include 1 textview, 5 radiobuttons two buttons and some images. I don't know how to do it. if there would be some example or code spinet, it would be nice..

Upvotes: 2

Views: 7980

Answers (3)

Rony Tesler
Rony Tesler

Reputation: 1401

you might want to read about the extended class' constructors here: Do I need all three constructors for an Android custom view?

Upvotes: 2

I think it could help you:

First you can define a RelativeLayout in xml, with all the elements you want on it, placed like you want.

Second, when you have that layout defined, you can develop a custom class, extending RelativeLayout, and inflate that layout in the constructor method of the class, something like this:

public class MyCustomView extends RelativeLayout {

 ...

 public MyCustomView(Context context) {

  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  addView(inflater.inflate(R.layout.your_layout, null));

  oneTextView = (TextView) findViewById(R.id.oneTextView);
  oneRadioButton = (RadioButton) findViewById(R.id.oneRadioButton);
  ...
 }
 ...
}

At this point, you can use oneTextView, oneRadioButton, etc., in your class in a normal way.

Upvotes: 18

Phil
Phil

Reputation: 36289

You should read the documentation here to get started.

Upvotes: 0

Related Questions