Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create Fragment Communication in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 455
    Comment on it

    In the below example I am describing "how to communicate fragments"?, fragment is a part of Activity in android. In one Activity we can use multiple fragment easily and each fragment have it's own functinality. Without activity we can not use fragment, Fragments should never communicate directly.

    Here first I have created two new xml layouts. In first fragment1. xml layout I have created ListView within LinearLayout, And then in fragment2.xml layout I have created a TextView , In actvity_main.xml I have created two fragments .

    Now see programming area here I have created Fragment1 and Fragment2 class. In Fragment1 I have used addListItemsToListView method and added all item in ArrayList after add element I have used ArrayAdapter and then I have created ListItemSelectedListener interface. In Fragment2 I have set text and used getActivity() method. In last step MainActivity I have extend FragmentActivity and implemented a interface Fragment1.ListItemSelectedListener. You can see below program it will clearly describe you how can communicate two diffrent fragment classes in android.

    Step(1)Created a new fragment1.xml layout -
     

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout
    
        android:id="@+id/listOfdists"
    
        xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:layout_width="fill_parent"
    
        android:layout_height="fill_parent">
    
        <ListView
    
            android:id="@+id/list"
    
            android:layout_width="match_parent"
    
            android:layout_height="match_parent" >
    
        </ListView>
    
    </LinearLayout>
    
    

    Step(2)Created a new fragment2.xml layout -
     

    <?xml version ="1.0" encoding="utf-8"?>
    <LinearLayout
    
        xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:id="@+id/details"
    
        android:layout_width="match_parent"
    
        android:layout_height="match_parent"
    
        android:orientation="vertical" >
    
        <TextView
    
            android:id="@+id/detailsView"
    
            android:layout_width="fill_parent"
    
            android:layout_height="wrap_content"/>
    
    </LinearLayout>
    
    

    Step(3)activity_main.xml layout-
     

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tool="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:baselineAligned="false">
        <fragment
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:id="@+id/empList"
            android:name="com.tiwari.rajshekhar.myfragment.Fragment1"/>
        <fragment
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:id="@+id/empdetails"
            android:name="com.tiwari.rajshekhar.myfragment.Fragment2"/>
    
    
    </LinearLayout>
    
    

    Step(4)-Created a New Fragment1 class-
     

    public class Fragment1 extends Fragment implements AdapterView.OnItemClickListener{
        private View v;
        private static ListView lvOne;
        private List<String> listOfDistricts;
        private ArrayAdapter<String>adapter;
        ListItemSelectedListener itemListerner;
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            inflater = (LayoutInflater)getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            v=inflater.inflate(R.layout.fragment1, null, false);
            addListItemsToListView();
    
            initializeListeners();
    
            return v;
        }
    
        @Override
        public void onAttach(Activity activity)
        {
            super.onAttach(activity);
    
            itemListerner = (ListItemSelectedListener)activity;
    
        }
    
        private void addListItemsToListView()
        {
            lvOne = (ListView)v.findViewById(R.id.list);
    
            listOfDistricts = new ArrayList<String>();
    
            listOfDistricts.add("Raj");
    
            listOfDistricts.add("Inder");
    
            listOfDistricts.add("Om");
    
            adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, listOfDistricts);
    
            lvOne.setAdapter(adapter);
        }
    
        private void initializeListeners()
        {
            lvOne.setOnItemClickListener(this);
        }
    
        public interface ListItemSelectedListener
        {
            public void listItemSelectedListener(String name);
        }
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            itemListerner.listItemSelectedListener(listOfDistricts.get(position));
        }
    
    }
    
    


    Step(5)-Created a New Fragment2 class-

    public class Fragment2 extends Fragment {
        private static TextView tvOne;
        private View v;
        Context context;
        private static final String TAG="Fragment2";
        public void getItemValue(String itemName,Context context)
        {
            this.context=context;
            tvOne.setText("Evon-tec:"+itemName);
        }
        @Override
        public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
        {
            inflater=(LayoutInflater)getActivity().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
            v=inflater.inflate(R.layout.fragment2,null,false);
            tvOne=(TextView)v.findViewById(R.id.detailsView);
            onCreate(savedInstanceState);
            return v;
        }
    }

    Step(6)MainActivity-
     

    public class MainActivity extends FragmentActivity implements Fragment1.ListItemSelectedListener {
        public static final String TAG="MainActivity";
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                try {
                    setContentView(R.layout.activity_main);
                }
                catch (Exception e)
                {
                    Log.e(TAG,e.toString());
                }
            }
    
    
           @Override
         public void listItemSelectedListener(String name)
           {
               Fragment2 fragment2 = new Fragment2();
               fragment2.getItemValue(name,this);
           }
    }
    
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: