We can make fragments to communicate with one another and exchange data.
The following code shows how one fragment can access the views contained within another fragment.
In the res/layout
folder, add a new file and name it fragment1.xml
.
Populate it with the following:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout/* w w w. ja va 2 s .co m*/ xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00FF00" > <TextView android:id="@+id/lblFragment1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is fragment #1" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout>
Also in the res/layout
folder, add another new file and name it fragment2.xml
.
Populate it as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout// w w w .j a v a 2s. c o m xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFE00" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is fragment #2" android:textColor="#000000" android:textSize="25sp" /> <Button android:id="@+id/btnGetText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get text in Fragment #1" android:textColor="#000000" android:onClick="onClick" /> </LinearLayout>
In activity_main.xml
, add the following code:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > //ww w.j a va 2 s .co m <fragment android:name="com.java2s.myapplication3.app.Fragment1" android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" /> <fragment android:name="com.java2s.myapplication3.app.Fragment2" android:id="@+id/fragment2" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" /> </LinearLayout>
Modify the MainActivity.java
file as follows:
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
Under the com.java2s.Fragments
package name, add two
Java class files and name them Fragment1.java
and Fragment2.java
Add the following code to Fragment1.java
:
import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; //w ww . ja v a 2 s .c om public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //Inflate the layout for this fragment return inflater.inflate(R.layout.fragment1, container, false); } }
Add the following code to Fragment2.java
:
import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; /*w ww . j a va2 s .com*/ public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate( R.layout.fragment2, container, false); } @Override public void onStart() { super.onStart(); Button btnGetText = (Button) getActivity().findViewById(R.id.btnGetText); btnGetText.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { TextView lbl = (TextView) getActivity().findViewById(R.id.lblFragment1); Toast.makeText (getActivity(), lbl.getText(), Toast.LENGTH_SHORT).show(); } }); } }
Debug the application on the Android emulator.
In the second fragment on the right, click the button. You should see the Toast class displaying the text "This is fragment #1".