How to use Fragments in Android
You need to add an empty public constructor to your Fragments, like it says in the stack trace:
public class Fragment_2 extends Fragment{
public Frament_2() {
//BLAH!
}
// The rest of your code
}
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...
This is the classic case of Communication between two Fragments.
But let's cover more basic things first.
Skeleton of the Fragments
FirstFragment
EditText
Button
public class FirstFragment extends Fragment{
private EditText editext;
private Button button;
public static FirstFragment getInstance(){
return new FirstFragment();
}
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle){
View view = inflater.inflate(R.Layout.fragment1,parent,false);
editext = (EditText)view.findViewById(R.id.editText)
button = (Button)view.findViewById(R.id.button)
return view;
}
}
SecondFragment
TextView
public class SecondFragment extends Fragment{
private TextView textView;
public static SecondFragment getInstance(){
return new SecondFragment();
}
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle){
View view = inflater.inflate(R.Layout.fragment2,parent,false);
textView = (TextView)view.findViewById(R.id.editText)
return view;
}
public void showMessage(String msg)
{
textView.setText(msg)
}
}
How to createInstance of Fragments
FirstFragment fragmne1 = FirstFragment.getInstance();
SecondFragment fragmne1 = SecondFragment.getInstance();
How to Add them in the Activity?
getSupportFragmentManager()
.beginTransaction()
.add(R.id.placeholder1,fragment1,"TAG1")
.add(R.id.placeholder2,fragment2,"TAG2")
.commit();
Now the communication Part
Create an Interface
public interface FragmentListener{
public void buttonClicked();
}
Implement this interface in the Activity and override the method
public MyActivity extends AppcompatActivity implements MyFragmentListener{
@Override
public void buttonClicked(){
fragment2.showMessage(String msg);
}
}
Send data from Fragment
- initialize the listener in the FirstFragment
- call interface method on Button click.
The final structure of FirstFragment
public class FirstFragment extends Fragment{
private EditText editext;
private Button button;
private MyFragmentListener listener;
public static FirstFragment getInstance(){
return new FirstFragment();
}
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle){
View view = inflater.inflate(R.Layout.fragment1,parent,false);
editext = (EditText)view.findViewById(R.id.editText)
button = (Button)view.findViewById(R.id.button)
// button click method
listener.buttonClicked(editText.getText().toString());
return view;
}
@Override
public void onAttach(Context context){
listener = (MyFragmentListener)context;
}
}