Reputation: 2933
I am trying to replace a fragment with a button. Basically I have this form (which is all the input views on helloFragment) and I want to click submit and show the form information on the next fragment (showFormFragment).
It renders the first fragment on my device, but the button won't replace it. It doesn't crash however.
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/hello"
android:name="teaminfamous.com.fragments.helloFragment"
tools:layout="@layout/fragment_hello">
</FrameLayout>
</RelativeLayout>
My onCreate and Button listner:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction ft = fm.beginTransaction();
helloFragment hf = new helloFragment();
FormShow fs = new FormShow();
ft.replace(android.R.id.content, hf);
Button submitbtn = (Button) findViewById(R.id.submitButton);
submitbtn.setOnClickListener(submitForm);
}
EDIT:
In Fragment Class helloFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_form_show, container, false);
Button submitbtn = (Button) view.findViewById(R.id.submitButton);
submitbtn.setOnClickListener(submitForm);
// Inflate the layout for this fragment
return view;
}
private View.OnClickListener submitForm = new View.OnClickListener() {
public void onClick(View v) {
//do handling here
final EditText nameField = (EditText) getView().findViewById(R.id.nameEdit);
final EditText originField = (EditText) getView().findViewById(R.id.originEdit);
String name = nameField.getText().toString();
String origin = nameField.getText().toString();
FragmentTransaction ft = fm.beginTransaction();
FormShow show = new FormShow();
ft.replace(R.id.hello, show, show.toString() ).addToBackStack(null).commit();
//final TextView nameText = (TextView) findViewById(R.id.nameShow);
// nameText.setText(name);
}
};
I hit submit and the onClickListner gets called and it shows a blank screen now
I'm pretty new to Android and a novice in Java, so an explanation for the usual pattern for doing something like this would also be helpful.
Upvotes: 1
Views: 110
Reputation: 11191
In order to replace fragment dynamically you have to add it at runtime. To do this you need a container that will host your fragment e.g. FrameLayout:
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then you need an access the FragmentManager to begin transaction:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
String tag = fragment.getClass().getSimpleName();
transaction.replace( R.id.fragment_container, new YourFragmentHere(), tag);
transaction.commit();
Upvotes: 2
Reputation: 1873
You should try to change your
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/hello"
android:name="teaminfamous.com.fragments.helloFragment"
tools:layout="@layout/fragment_hello">
</fragment>
to <FrameLayout>
Then, instead of this code:
ft.replace(android.R.id.content, hf);
use
ft.replace(R.id.hello, hf).commit();
and it should work
Upvotes: 1