Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to communication between an Activity and Fragments in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 811
    Comment on it

    In the below example I have created a commuication between an activity and fragments classes. In the below example user enters name in SendFragment part and click submit button then in second fragment on displayFragment show user name input result.

    Here I have created first two new display.xml and send.xml layout.In send.xml I have added EditText and Button. And in display.xml I have added a TextView. After then in third Step, in activity_main.xml layout I have added two fragemnts.

    Now See coding area I have created SendFragment and DisplayFragment classes. In SendFragment class I have created StartCommuncation interface and used OnClickListener method. In DisplayFragment class i have used setText method. Now atLast In MainActivity part I have extend Activity and implements SendFragment class and StartCommunication interface after that I have used setComm method and toast function. You can see below program it will clearly describe you How to communication between an Activity and Fragments in android.

     

     

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

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:maxLength="15"
            android:id="@+id/edit"
            android:hint="Send Text"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Message"
            android:id="@+id/button1"/>
    </LinearLayout>

     

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

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

     

    Step(3)activity_main.xml layout-
     

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        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/fragment1"
            android:name="com.tiwari.rajshekhar.actfrag.DisplayFragment"/>
        <fragment
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:id="@+id/fragment2"
            android:name="com.tiwari.rajshekhar.actfrag.SendFragment"/>
    
    </LinearLayout>
    
    

     

    Step(4)-Created a New SendFragment class-
     

    public class SendFragment extends Fragment {
        StartCommunication mStartCommunicationListner;
        String msg="Welcome";
        @Override
        public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
            View mView =(View)inflater.inflate(R.layout.send,container);
            final EditText mEditText =(EditText)mView.findViewById(R.id.edit);
            Button mButton =(Button)mView.findViewById(R.id.button1);
            mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    msg=mEditText.getText().toString();
                    sendMessage();
    
                }
            });
            return mView;
        }
        interface StartCommunication{
                public void setComm(String msg);
    
        }
        @Override
        public void onAttach(Activity activity){
            super.onAttach(activity);
            if (activity instanceof StartCommunication)
            {
                mStartCommunicationListner =(StartCommunication)activity;
    
            }
            else
                throw new ClassCastException();
        }
    
        private void sendMessage() {
    
            mStartCommunicationListner.setComm(msg);
        }
    
    
    }
    
    

     

    Step(5)-Created a New DisplayFragment class-

    public class DisplayFragment extends Fragment {
        View mView;
        @Override
        public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstaneceState){
            mView=(View)inflater.inflate(R.layout.display,container);
            return mView;
        }
        void setText(String msg){
            TextView mTextView =(TextView)mView.findViewById(R.id.text);
            mTextView.setText(msg);
        }
    }


     

    Step(6)MainActivity-
     

    public class MainActivity extends AppCompatActivity implements SendFragment.StartCommunication
    {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public void setComm(String msg) {
    
    
            DisplayFragment mDisplayFragment = (DisplayFragment)getSupportFragmentManager().findFragmentById(R.id.fragment1);
            if(mDisplayFragment != null && mDisplayFragment.isInLayout())
            {
                mDisplayFragment.setText(msg);
            }
            else
            {
                Toast.makeText(this, "Error Sending Message", Toast.LENGTH_SHORT).show();
            }
        }
    }

     

 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: