waylonion
waylonion

Reputation: 6976

Inflate a View Programmatically

How can I inflate a view programmatically?

I'm trying to implement a NavigationDrawer in Android and I have a main view named HomeworkMain with a superclass of Activity.

I also have a HomeworkListActivity which calls:

@Override
protected Fragment createFragment() {
    return new HomeworkListFragment();
}

to create a HomeworkListFragment.

I want HomeworkListActivity to be the root view of my HomeworkMain. But it doesn't have any xml file attached to it as you can see since it just calls HomeworkListFragment.

So how do I put this into the onCreateView method and inflate the view?

Upvotes: 1

Views: 186

Answers (1)

kimkevin
kimkevin

Reputation: 2222

You can inflate your own layout like below. try this one first.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.item, container, false);
    titleTxt = (TextView)   view.findViewById(R.id.title);
    return view;
}

If this is wrong, plz let me know more specific.

Upvotes: 2

Related Questions