Reputation: 3201
I am working with an Android application. In my app I have to change the view of a fragment on button clicks. The following is my code:
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if (container == null) {
return null;
}
LinearLayout theLayout = (LinearLayout)inflater.inflate(R.layout.settings, container, false);
Button edit = (Button) theLayout.findViewById(R.id.btn_edit);
edit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Here I want to change the view.[xml file]
}
});
return theLayout;
}
In activity we can change the view by using setContentView()
. How can I do that in fragment?
Upvotes: 3
Views: 13177
Reputation: 3201
I found answer for my question.Actually very stupid doupt.
edit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/**
* Edit button click will replace Settings screen to
* edit screen
**/
theLayout = (LinearLayout)inflater.inflate(R.layout.edit_settings, container, false);
}
});
Upvotes: 2
Reputation: 277
Think it's better to use two fragments and replace one fragment by another on button click. If you want to add fragment dinamically - see this example: http://notionink.wikidot.com/rajeshbabu
Upvotes: 0