Tuesday, 15 September 2015

android - Add fragment programmatically on button click -



android - Add fragment programmatically on button click -

i know kind of noob question, come php , javascript, , i've started learning java , android. i've read dev documentation on fragments, , i'm trying add together fragment on button click. app starts, dies on button click. don't know if logic wrong here? (adding fragments straight in xml works).

fragment class:

public class examplefragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment homecoming 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>

your fragment_holder, needs viewgroup, i'll recommend framelayout since simplest viewgroup can use.

android android-fragments

No comments:

Post a Comment