android - Accessing UI elements from fragment class -
i have class extends fragment class below:
public class fragmentcreategroup extends fragment { @override public void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); imageview group=(imageview)findviewbyid(r.id.group_image);//shows error here } @override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) { // todo auto-generated method stub return inflater.inflate(r.layout.fragment_creategroup, container,false); } }
which associated following xml file
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:contentdescription="@string/cr_group_grpname_desc" android:gravity="center" android:orientation="vertical" > <imageview android:id="@+id/group_image" android:layout_width="139dp" android:layout_height="144dp" android:src="@drawable/background" /> <edittext android:id="@+id/cr_group_grpname_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="37dp" android:contentdescription="@string/cr_group_grpname_desc" android:ems="10" android:hint="@string/cr_group_grpname_hint" android:inputtype="text" > <requestfocus /> </edittext> <button android:id="@+id/cr_group_creategrp_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="42dp" android:background="@drawable/greenbutton" android:paddingleft="10dp" android:paddingright="10dp" android:text="@string/cr_group_creategrp_btn_label" /> </linearlayout>
i tried access imageview in fragment layout using findviewbyid(). since didn't extend activity class unable perform action. there way access ui elements non activity class? if how perform?
you need view after inflate in oncreateview
public class fragmentcreategroup extends fragment { private imageview group; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_creategroup, container, false); group = (imageview) v.findviewbyid(r.id.group_image); group.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { group.setimagedrawable(); // set image here } }); return v; }
}
Comments
Post a Comment