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
    • 878
    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 -

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:orientation="vertical" android:layout_width="match_parent"
    4. android:layout_height="match_parent">
    5. <EditText
    6. android:layout_width="match_parent"
    7. android:layout_height="wrap_content"
    8. android:ems="10"
    9. android:maxLength="15"
    10. android:id="@+id/edit"
    11. android:hint="Send Text"/>
    12. <Button
    13. android:layout_width="wrap_content"
    14. android:layout_height="wrap_content"
    15. android:text="Message"
    16. android:id="@+id/button1"/>
    17. </LinearLayout>

     

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

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:orientation="vertical" android:layout_width="match_parent"
    4. android:layout_height="match_parent">
    5. <TextView
    6. android:layout_width="250dp"
    7. android:layout_height="wrap_content"
    8. android:id="@+id/text"
    9. android:text="Type here"/>
    10.  
    11. </LinearLayout>

     

    Step(3)activity_main.xml layout-
     

    1. <LinearLayout
    2. xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:orientation="horizontal"
    6. android:baselineAligned="false">
    7. <fragment
    8. android:layout_width="0dp"
    9. android:layout_height="fill_parent"
    10. android:layout_weight="1"
    11. android:id="@+id/fragment1"
    12. android:name="com.tiwari.rajshekhar.actfrag.DisplayFragment"/>
    13. <fragment
    14. android:layout_width="0dp"
    15. android:layout_height="fill_parent"
    16. android:layout_weight="1"
    17. android:id="@+id/fragment2"
    18. android:name="com.tiwari.rajshekhar.actfrag.SendFragment"/>
    19.  
    20. </LinearLayout>
    21.  
    22.  

     

    Step(4)-Created a New SendFragment class-
     

    1. public class SendFragment extends Fragment {
    2. StartCommunication mStartCommunicationListner;
    3. String msg="Welcome";
    4. @Override
    5. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
    6. View mView =(View)inflater.inflate(R.layout.send,container);
    7. final EditText mEditText =(EditText)mView.findViewById(R.id.edit);
    8. Button mButton =(Button)mView.findViewById(R.id.button1);
    9. mButton.setOnClickListener(new View.OnClickListener() {
    10. @Override
    11. public void onClick(View arg0) {
    12. msg=mEditText.getText().toString();
    13. sendMessage();
    14.  
    15. }
    16. });
    17. return mView;
    18. }
    19. interface StartCommunication{
    20. public void setComm(String msg);
    21.  
    22. }
    23. @Override
    24. public void onAttach(Activity activity){
    25. super.onAttach(activity);
    26. if (activity instanceof StartCommunication)
    27. {
    28. mStartCommunicationListner =(StartCommunication)activity;
    29.  
    30. }
    31. else
    32. throw new ClassCastException();
    33. }
    34.  
    35. private void sendMessage() {
    36.  
    37. mStartCommunicationListner.setComm(msg);
    38. }
    39.  
    40.  
    41. }
    42.  
    43.  

     

    Step(5)-Created a New DisplayFragment class-

    1. public class DisplayFragment extends Fragment {
    2. View mView;
    3. @Override
    4. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstaneceState){
    5. mView=(View)inflater.inflate(R.layout.display,container);
    6. return mView;
    7. }
    8. void setText(String msg){
    9. TextView mTextView =(TextView)mView.findViewById(R.id.text);
    10. mTextView.setText(msg);
    11. }
    12. }


     

    Step(6)MainActivity-
     

    1. public class MainActivity extends AppCompatActivity implements SendFragment.StartCommunication
    2. {
    3.  
    4. @Override
    5. protected void onCreate(Bundle savedInstanceState) {
    6. super.onCreate(savedInstanceState);
    7. setContentView(R.layout.activity_main);
    8. }
    9.  
    10. @Override
    11. public void setComm(String msg) {
    12.  
    13.  
    14. DisplayFragment mDisplayFragment = (DisplayFragment)getSupportFragmentManager().findFragmentById(R.id.fragment1);
    15. if(mDisplayFragment != null && mDisplayFragment.isInLayout())
    16. {
    17. mDisplayFragment.setText(msg);
    18. }
    19. else
    20. {
    21. Toast.makeText(this, "Error Sending Message", Toast.LENGTH_SHORT).show();
    22. }
    23. }
    24. }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: