0% found this document useful (0 votes)
39 views2 pages

Activity - Main - XML: Fragment - 1 Fragment

The document describes an Android application that uses two fragments in a linear layout. Fragment 1 contains an edit text and button. When the button is clicked, it gets the text from the edit text and passes it to Fragment 2. Fragment 2 displays the text in a text view by calling its setName method. This allows information to be shared between the two fragments in the single activity layout.

Uploaded by

Natheem Safin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views2 pages

Activity - Main - XML: Fragment - 1 Fragment

The document describes an Android application that uses two fragments in a linear layout. Fragment 1 contains an edit text and button. When the button is clicked, it gets the text from the edit text and passes it to Fragment 2. Fragment 2 displays the text in a text view by calling its setName method. This allows information to be shared between the two fragments in the single activity layout.

Uploaded by

Natheem Safin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

activity_main.

XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<fragment
android:id="@+id/fragment_content_1"
android:name="com.example.fragmentexample.Fragment_1"
android:layout_width="0dip"
android:layout_weight="0.50"
android:layout_height="fill_parent" >
</fragment>

<fragment
android:id="@+id/fragment_content_2"
android:name="com.example.fragmentexample.Fragment_2"
android:layout_width="0dip"
android:layout_weight="0.50"
android:layout_height="fill_parent" >

<!-- Preview: layout=@layout/fragment_basic -->


</fragment>

</LinearLayout>

Fragment_1.Java
public class Fragment_1 extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub

View view = inflater.inflate(R.layout.fragment_fragment_1, container, false);

final EditText edtxtPersonName_Fragment = (EditText)


view.findViewById(R.id.edtxtPersonName);
Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi);

btnSayHi_Fragment.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String name = edtxtPersonName_Fragment.getText().toString();

FragmentManager fm = getFragmentManager();
Fragment_2 f2 = (Fragment_2)
fm.findFragmentById(R.id.fragment_content_2);

if(f2 != null && f2.isInLayout())


{
f2.setName(name);
}
Activity activity = getActivity();

if(activity != null)
{
Toast.makeText(activity, "Say&ing Hi in Progress...",
Toast.LENGTH_LONG).show();
}
}
});

return view;

Fragment_2.Java
public class Fragment_2 extends Fragment{

View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub

view = inflater.inflate(R.layout.fragment_fragment_2, container, false);


return view;
}

public void setName(String name)


{
TextView txtName = (TextView) view.findViewById(R.id.txtViewResult);
txtName.setText("Hi " + name);
}

You might also like