3

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?

sarath
  • 3,181
  • 7
  • 36
  • 58
  • http://developer.android.com/reference/android/app/Fragment.html#getView() refer this link – Thiru Aug 07 '12 at 10:55

2 Answers2

2

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);


            }
        });
sarath
  • 3,181
  • 7
  • 36
  • 58
0

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

Saasha
  • 277
  • 1
  • 2
  • 8
  • Also take a look at this http://stackoverflow.com/questions/5907673/android-cant-replace-one-fragment-with-another – Saasha Aug 07 '12 at 11:09
  • @Saasha...My I am using four fragments like tabview. One fragment is need two view edit and save...if i click edit button of the fragment edit layout will come and after edit if i click save then it will save and save button will change to edit button – sarath Aug 07 '12 at 11:14