I'm new to Android, so this may have an obvious answer, but I can't find it.
I am writing a version of a calculator application. I want to make it so that when the user clicks on one of the buttons, a fragment begins (with a large number of buttons on it, which can then be used for more input).
The code for the fragment is as follows:
import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class VariableMenu extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);} public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment, container, false); } }
with the XML layout as follows:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/Bx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="X" android:onClick="onButtonClicked"/> <Button android:id="@+id/By" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/Bx" android:layout_alignTop="@id/Bx" android:text="Y" android:onClick="onButtonClicked"/> <Button android:id="@+id/Bz" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/By" android:layout_alignTop="@id/By" android:text="Z" android:onClick="onButtonClicked"/> </RelativeLayout>
(I know that I should not use hard-coded strings, but I will fix this as soon as I get back to work)
I tried adding it using fragmentation using the onTouch method of the activation button, for example:
public void onFragmentActivated(View v) { FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); VariableMenu fragment = new VariableMenu(); fragmentTransaction.add(R.layout.main, fragment); fragmentTransaction.commit(); }
but it gave an error saying that "No view found for id: ... (main) for the VariableMenu fragment."
So, I did this to view in the main XML file:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> (Irrelevant things) <fragment android:id="@+id/Fragment" android:name="calculator.app.VariableMenu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/B1"/> (Irrelevant things) </RelativeLayout>
But this led to the fragment being there as soon as the activity was created (and setContentView (R.layout.main); ran).
Is there an easy way to programmatically add and remove a fragment with a user interface for activity?