0

so i have this class called book. this book is both a java class and an xml file for the layout. i then have a class/xml file called book_shelf. in my book_shelf xml file i have a view called book1.

public class book_shelf extends Activity {
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.mainmenu, menu);

        book MyBook = new book();
        final View book1 = (View) findViewById(R.id.book1);
        book1.(load my book xml layout in this view)
        return true;

    }

}

i want to load a book layout into the view in my bookshelf frame. Please and thank you.

Mikecit22
  • 157
  • 1
  • 14

1 Answers1

1

You need to use inflator to inflat the desired Layout and than add the layout in the view. E.g.

book MyBook = new book();
final View book1 = (View) findViewById(R.id.book1);

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

//assuming R.layout.book your book xml
View bookView = inflater.inflate(R.layout.book, null);

//Add bookView layout to your book1.
book1.addView(bookView)

Edited: Try this

final View book1 = (View) findViewById(R.id.book1);
((ViewGroup) book1).addView(bookView, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
Tabrej Khan
  • 894
  • 6
  • 15
  • thank you so much but the book1.addView(bookView) gives an error. i noticed that the addView is only for ViewGroup tho. i tried casting book1 as a ViewGroup but that did not work. ((ViewGroup) book1).addView(bookView); – Mikecit22 Dec 26 '13 at 04:50
  • Check the Edited: part. – Tabrej Khan Dec 26 '13 at 06:48
  • that one crashes as well with the same error, View cant be casted to ViewGroup. maybe im going about this wrong. Is there an element "ViewGroup", instead of a "View", that i can add multiple views to? for example my bookShelf will be of type ViewGroup holding many Views book? – Mikecit22 Dec 26 '13 at 20:04
  • OK so i kinda got it. the issue is that i can not add it to the View i needed to add it to a layout, FrameLayout in my case. final FrameLayout book1 = (FrameLayout) findViewById(R.id.book1); but it does not shrink the book to the size of the FrameLayout size. – Mikecit22 Dec 27 '13 at 03:14