How to use Fragments in Android

Okie… Finally found a solution. Probably, it wasn’t much of a change.

Check out the code below…

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>

The layouts of fragment_fragment_1 and fragment_fragment_2 remain the same.

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

}

Here is the ScreenShot…

enter image description here

Leave a Comment