if try inflate view within fragment getting null.. example:
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // here inflate view using layout id & return. return view; }
whenever button clicked need create dynamic view e.g.: button & add linearlayout. perform operation inside fragment class this:
public void addplaces() { button button = new button(null); button.settext("button name"); // e.g. adding button enter code here linear layout linearlayout.addview(button); }
so, if inflate linearlayout inside oncreateview , use in add class, i'm getting null. how achieve?
declare variable instance variable , initialize linear layout
linearlayout linearlayout; public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment1, container, false); linearlayout = (linearlayout) rootview.findviewbyid(r.id.linearlayout); return rootview; }
then
public void addplaces() { button button = new button(getactivity()); // needs activity context // fragment hosted activity. use getactivity() context of hosting activity. button.settext("button name"); linearlayout.addview(button); }
example: modify below according requirement.
fragment1.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_alignparentbottom="true" android:layout_centerhorizontal="true" android:text="button" /> <linearlayout android:layout_width="fill_parent" android:id="@+id/linearlayout" android:layout_height="fill_parent" android:layout_above="@+id/button1" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:orientation="vertical" > </linearlayout> </relativelayout>
myfragment.java
public class myfragment extends fragment { linearlayout linearlayout; view rootview; @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); button b = (button) rootview.findviewbyid(r.id.button1); b.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub addplaces(); } }); linearlayout = (linearlayout) rootview.findviewbyid(r.id.linearlayout); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { rootview = inflater.inflate(r.layout.fragment1, container, false); return rootview; } public void addplaces() { button button = new button(getactivity()); // needs activity context button.settext("button name"); linearlayout.addview(button); } }
snap shot of emulator
edit :
activity-main.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" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <fragment android:name="com.example.fragments.myfragment" android:id="@+id/frag" android:layout_above="@+id/button1" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:text="button" /> </relativelayout>
mainactivity.java
public class mainactivity extends fragmentactivity { button b; myfragment fragment; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragment = new myfragment(); fragmenttransaction.add(r.id.frag, fragment); fragmenttransaction.commit(); b = (button) findviewbyid(r.id.button1); b.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub fragment.addplaces(); } }); } }
myfragment.java
public class myfragment extends fragment { linearlayout linearlayout; view rootview; @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); linearlayout = (linearlayout) rootview.findviewbyid(r.id.linearlayout); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { rootview = inflater.inflate(r.layout.fragment1, container, false); return rootview; } public void addplaces() { button button = new button(getactivity()); // needs activity context button.settext("button name"); linearlayout.addview(button); } }
Comments
Post a Comment