JSNinja
JSNinja

Reputation: 715

Unable to replace fragment on button click

I'm trying to learn how to implement fragments in android. So, I created two buttons and a fragment in the activity_main.xml. If I click on the first button, fragment_one should be inflated, similarly if I click the second button, fragment_two should be inflated.

But the issue is it doesn't replace the fragment. When I debug the code, performClick() method in the View.class returns false.

Also there is no error in LogCat.

I'm unable to figure out what is the issue with the code.

Here's the activity_main.xml

<?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="@string/fragment1" />

<Button
    android:id="@+id/button2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/fragment2" />

<fragment
    android:id="@+id/fragment_place"
    android:name="com.example.myfragmentwithbuttonapp.FragmentTwo"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

Here's the fragment_one.xml

<?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"
android:background="#00ffff">

   <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1"
       android:text="@string/thisisfragment1"
       android:textStyle="bold" />

</LinearLayout>

Here's the fragment_two.xml

<?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"
android:background="#ffff00">

   <TextView
       android:id="@+id/textView2"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:text="@string/thisisfragment2"
       android:textStyle="bold" />

</LinearLayout>

Here's the FragmentOne.java

package com.example.myfragmentwithbuttonapp;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

     //Inflate the layout for this fragment

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

Here's the FragmentTwo.java

package com.example.myfragmentwithbuttonapp;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


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

       // Inflate the layout for this fragment

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

Here's the MainActivity.java

package com.example.myfragmentwithbuttonapp;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    Fragment fr;
    FragmentManager fm;
    FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Button button1 = (Button) findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);

        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                fr = new FragmentOne();

                fm = getFragmentManager();
                fragmentTransaction = fm.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_place, fr);
                fragmentTransaction.commit();

            }
        });

        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                fr = new FragmentTwo();

                fm = getFragmentManager();
                fragmentTransaction = fm.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_place, fr);
                fragmentTransaction.commit();

            }
        });
    }

}

I hope I have been able to explain the problem.

Any help is appreciated.

Thanks

Upvotes: 1

Views: 5064

Answers (1)

ig343
ig343

Reputation: 277

For fragmentTransaction.replace() you need to specify a container for your fragment and the fragment itself. The container is the parent view which is going to hold your fragment. Tipically a FrameLayout.

Right now you are passing R.id.fragment_place as the container, but that id refers to a fragment and not to a fragment container.

In activity_main.xml, replace:

<fragment
android:id="@+id/fragment_place"
android:name="com.example.myfragmentwithbuttonapp.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="match_parent" />

with:

<FrameLayout
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

It works fine like this. The rest of your code is OK.

Upvotes: 1

Related Questions