Munez NS
Munez NS

Reputation: 1011

Add fragment programmatically on button click

I know this is kind of a noob question, but I come from php and javascript, and I've just started learning java and android. I've read the dev documentation on fragments, and I'm trying to add a fragment on button click. App starts, but dies on button click. I don't know if my logic is wrong here? (Adding fragments directly in xml works).

Fragment class:

public class ExampleFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }


}

Fragment xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/seekBar1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:text="Button" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="text" >
        <requestFocus />
    </EditText>

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="14dp" />

</RelativeLayout>

Activity class:

public class MainActivity extends Activity implements OnClickListener{
    Button addFragment;
    android.app.FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
    }

    private void initialize() 
    {
        addFragment = (Button) findViewById(R.id.bAddF);
        addFragment.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
            ExampleFragment fragment = new ExampleFragment();
            transaction.add(R.id.fragment_holder, fragment);
            transaction.commit();

    }

}

Activity 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"
    tools:context="${packageName}.${activityClass}" >

    <TextView
        android:id="@+id/tvHello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button 
        android:id="@+id/bAddF"
        android:layout_below="@id/tvHello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Fragment"/>

    <View
        android:id="@+id/fragment_holder"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@id/bAddF">
    </View>


</RelativeLayout>

Upvotes: 0

Views: 1661

Answers (1)

Julian Suarez
Julian Suarez

Reputation: 4521

Your fragment_holder, needs to be a ViewGroup, I'll recommend a FrameLayout since it is the simplest ViewGroup you can use.

Upvotes: 2

Related Questions