Search code examples
androidandroid-layoutlayoutsetcontentview

Add custom view (tileview) to layout?


I'm an Android Beginner and I would like to know how I can add a custom view to the layout activity_main.xml.

In my MainActivity I have the following:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);         

    }

And I want to add a Tileview to my activity_main layout:

 TileView tileView = new TileView( this );
  tileView.setSize( 2000, 3000 );  // the original size of the untiled image
  tileView.addDetailLevel( 1f, "tile-%d-%d.png");
  setContentView( tileView );

Both of them work good separately. While I can't create multiple setContentView, I would like to know how I can create the tileView into the activity_main layout (or to an id). I tried several things with LayoutInflater but without success yet.

Thanks in advance.

For tileview see: https://github.com/moagrius/TileView


Solution

  • Add one of the ViewGroup subclass (E.g. LinearLayout) provided by the framework it to the activity_main.xml and assign it an id. It will act as container for your TileView. After you instantiate your TileView, retrieve this container with findViewById and call addView on it, providing as parameter the TileView you just instantiated and, optional, the LayoutParams for it.