1
How to send data from one fragment to another fragment in same activity?
How to send data from one fragment to another fragment in same activity?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginEnd="11dp"
android:layout_marginRight="11dp"
android:layout_marginTop="35dp"
android:layout_toLeftOf="@+id/button1"
android:layout_toStartOf="@+id/button1"
android:textColor="#f00"
android:textSize="40dp">
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:text="Post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp" />
</RelativeLayout>
package com.example.msclient009.fragmentinactivity;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class FirstFragment extends android.app.Fragment {
private static EditText mEditText;
FirstFragmentListener activityCallback;
//Listener for onButtonClick UI
public interface FirstFragmentListener {
public void onButtonClick(String text);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
activityCallback = (FirstFragmentListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement FirstFragmentListener");
}
}
//We get the reference to the editText and the button setUp the OnClickListener
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.first_fragment, container, false);
mEditText = (EditText) view.findViewById(R.id.editText1);
final Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonClicked(v);
}
});
return view;
}
//Set the activityCallback to onButtonClick passing the text in the mEditText
public void buttonClicked(View view) {
activityCallback.onButtonClick(mEditText.getText().toString());
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="45dp"
android:layout_marginStart="45dp"
android:layout_marginTop="116dp"
android:text="This text could be change after click the button !!!"
android:textColor='#00f'
android:textSize="30dp" />
</RelativeLayout>
package com.example.msclient009.fragmentinactivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class SecondFragment extends android.app.Fragment {
private static TextView mTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.second_fragment, container, false);
mTextView = (TextView) view.findViewById(R.id.textView1);
return view;
}
public void changeTextProperties(String text) {
mTextView.setText(text);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.msclient009.fragmentinactivity.MainActivity">
<fragment
android:id="@+id/first_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="31dp"
android:background="#0f0"
tools:layout="@layout/first_fragment"
class = "com.example.msclient009.fragmentinactivity.FirstFragment"/>
<fragment
android:id="@+id/second_fragment"
class="com.example.msclient009.fragmentinactivity.SecondFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#0f0"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="50dp"
tools:layout="@layout/second_fragment" />
</RelativeLayout>
package com.example.msclient009.fragmentinactivity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity implements FirstFragment.FirstFragmentListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onButtonClick(String text) {
SecondFragment textFragment = (SecondFragment) getFragmentManager().findFragmentById(R.id.second_fragment);
textFragment.changeTextProperties(text);
}
}
