Skip to content Skip to sidebar Skip to footer

Fragment Intermediate(iii): Creating A Activity That Alternate Between Fragments Onclick Of Respective Button

Aim: Create a activity that have two buttons, button 1 and button 2. When click, the fragment will alternate between the two fragments. Background: Fragmentone will take the editte

Solution 1:

Just have the FrameLayout and EditText in activity_main.xml. FrameLayout is a container (ViewGroup) to which you add or replace fragments. Your TableLayout can be in fragment layout.

You are adding/replacing fragment programatically so what is the need to have the same in xml.

Have this in activity_main.xml

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><EditTextandroid:id="@+id/input"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:ems="10" ><requestFocus /></EditText><FrameLayoutandroid:id="@+id/container" // idiscontainerandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@+id/button1"android:layout_below="@+id/input"
      ></FrameLayout><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignRight="@+id/input"android:onClick="selectFrag"android:text="Button1" /><Buttonandroid:id="@+id/button2"android:onClick="selectFrag"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/input"android:layout_alignParentBottom="true"android:text="Button2" /></RelativeLayout>

Then in MainActivity

publicclassMainActivityextendsActivity {
    Fragment fr; 
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    publicvoidselectFrag(View view) {

        if(view == findViewById(R.id.button2)) {
            fr = newFragmentTwo();

        }
        else {
            fr = newFragmentOne();
        }
        FragmentManagerfm= getFragmentManager();
        FragmentTransactionfragmentTransaction= fm.beginTransaction();
        fragmentTransaction.replace(R.id.container, fr);
        fragmentTransaction.commit();
   }
}

FragmentOne

publicclassFragmentOneextendsFragment {

    @Overridepublic View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) 
    {
        Viewview= inflater.inflate(R.layout.frag1, container, false);
        TextView warcraft=  (TextView) view.findViewById(R.id.moargold);
        EditTextmoargold= (EditText) getActivity().findViewById(R.id.input);
        Doublevespenegas= Double.parseDouble(moargold.getText().toString());

        warcraft.setText(String.valueOf(vespenegas));
        Toasttoast= Toast.makeText(getActivity(),String.valueOf(vespenegas) , Toast.LENGTH_SHORT);
        toast.show();
        return view;
    }
}

frag1.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TableLayoutandroid:id="@+id/TableLayout01"android:layout_width="fill_parent"android:layout_height="fill_parent"android:stretchColumns="1" ><TableRowandroid:id="@+id/tableRow4"android:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/dis_moargold"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Gold Count:" /><TextViewandroid:id="@+id/moargold"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /></TableRow></TableLayout></LinearLayout>

FragmentTwo

publicclassFragmentTwoextendsFragment {

    @Overridepublic View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) 
    {
        Viewview= inflater.inflate(R.layout.frag2, container, false);
        EditTextinput= (EditText) getActivity().findViewById(R.id.input);



        TableLayout tl=(TableLayout) view.findViewById(R.id.TableLayout01);


        TableRowtr=newTableRow(getActivity());
        tr.setLayoutParams(newLayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        TextViewtextview1=newTextView(getActivity());
        textview1.setText("happy");
        tr.addView(textview1);

        TextViewtextview2=newTextView(getActivity());
        textview2.setText("unhappy");
//###############To insert text from editview to table//      Double buygas = Double.parseDouble(input.getText().toString());//        textview2.setText(new Double(buygas).toString());
        tr.addView(textview2);

        tl.addView(tr, newTableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        return view;
    }
}

frag2.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TableLayoutandroid:id="@+id/TableLayout01"android:layout_width="fill_parent"android:layout_height="fill_parent"android:stretchColumns="1" ><TableRowandroid:id="@+id/tableRow4"android:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/dis_moargold"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Gold Count:" /><TextViewandroid:id="@+id/moargold"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /></TableRow></TableLayout></LinearLayout>

Snap

enter image description here

Solution 2:

While you add your Fragments dynamically, you keep your FragmentTwo inside your layout. Try to remove this in your layout:

<fragment
    android:name="com.example.sample.FragmentTwo"
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>  

Also, in your code, you add dynamically a Fragment and with replace method, you set the container as id fragment_place:

fragmentTransaction.replace(R.id.fragment_place, fr);  

However, this id is using by your fragment (FragmentTwo, the first piece of code above) and you cannot replace a fragment which is not added dynamically. You need to host your fragment inside a FrameLayout but yours has no id, try to add this:

<FrameLayout
    android:id="@+id/fragment_place"/// this line to create and id
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >  

Then, you will be able to host, add and replace your fragments into your framelayout as you want.

Post a Comment for "Fragment Intermediate(iii): Creating A Activity That Alternate Between Fragments Onclick Of Respective Button"