harveyslash
harveyslash

Reputation: 6012

Changing Fragments on button Click

I want to change fragments depending on button click. (I have made two fragment classes called fragmentLeft and fragmentMiddle)

This is my MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);


    }
Fragment fr;
    public void selectFrag(View view) {


         if(view == findViewById(R.id.bigbutton_left)) {
             fr = new fragmentLeft();



         }else {
             fr = new fragmentMiddle();

         }

         FragmentManager fm = getFragmentManager();

         if (fm != null) {
             // Perform the FragmentTransaction to load in the list tab content.
             // Using FragmentTransaction#replace will destroy any Fragments
             // currently inside R.id.fragment_content and add the new Fragment
             // in its place.
             FragmentTransaction ft = fm.beginTransaction();
             ft.replace(R.id.fragment_place, fr);
             ft.commit();
         }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);


        return true;
    }

}

This is fragmentLeft.java

public class fragmentLeft extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {

       //Inflate the layout for this fragment

      return inflater.inflate(
              R.layout.fragmentleft, container, false);
   }
}

the same is for fragmenttwo with the exception of the layout id passed to infater.inflate()

I have made two layout files two, with the correct names as well . This is the definition of my fragment from my activity_main.xml

But I am unable to get the buttons to do anything. No transactions take place, and I am stuck with the first fragment.

After a bit of googling , I came to know that its not recommended that you use fragment in the layout, but some other type of layout, but I saw this example code http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/ that seems to work just fine.

what is the problem ?

::EDIT:: There is something really weird going on. If I use this layout xml file for main Activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Fragment No.1"
        android:onClick="selectFrag" />

     <Button
         android:id="@+id/button2"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:onClick="selectFrag"
         android:text="Fragment No.2" /> 

   <fragment
        android:name="com.mainpackage.FragmentOne"
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

The fragments change and behave as they are expected to. But if I use this layout:

<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"
    tools:context=".MainActivity" >



    <Button
        android:id="@+id/button2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignBaseline="@+id/bigbutton_middle"
        android:layout_alignBottom="@+id/bigbutton_middle"
        android:layout_marginLeft="7dp"
        android:layout_toRightOf="@+id/bigbutton_middle"
        android:background="@drawable/mainbutton_right" />

    <Button
        android:id="@+id/bigbutton_left"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignBaseline="@+id/bigbutton_middle"
        android:layout_alignBottom="@+id/bigbutton_middle"
        android:layout_marginRight="7dp"
        android:layout_toLeftOf="@+id/bigbutton_middle"
        android:background="@drawable/mainbutton_left" />

    <Button
        android:id="@+id/bigbutton_middle"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="187dp"
        android:background="@drawable/mainbutton_middle" />
      <fragment
    android:id="@+id/fragment_place"
    android:name="com.mainpackage.FragmentOne"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_below="@+id/bigbutton_right" />


</RelativeLayout>

The fragments dont seem to change,i.e. the buttons seem to be dead.

What could be the problem ?

I am using Eclipse Juno

Upvotes: 1

Views: 5588

Answers (1)

Emmanuel
Emmanuel

Reputation: 13223

You are not getting a reference of the Buttons you are clicking in order for the FragmentTransaction to take place. You need to set OnClickListeners on the Buttons so they can execute relevant code.

One reason why the second layout doesn't display is because you are referencing an inexistant id:

<fragment
android:id="@+id/fragment_place"
android:name="com.mainpackage.FragmentOne"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_below="@+id/bigbutton_right" />

I do not see a View with an id of bigButton_right on that xml.

Another reason why the first xml works and the second doesn't is because you are not setting an onClick: property to any Button on the second xml.

Upvotes: 2

Related Questions