android - Where can I add the FindViewById() and the listeners in a fragment? -


i created fragment , called kabar , can see button , textview appears expected.

here fragment:

public class kabar extends fragment   {     private button button;     private textview text;      public kabar() {         // required empty public constructor     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         view v= inflater.inflate(r.layout.fragment_kabar , container, false);         return v;     } } 

this fragment_kabar layout:

 <framelayout  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="com.examplee.my.kabar">       <!-- todo: update blank fragment layout -->      <relativelayout          android:layout_width="match_parent"          android:layout_height="match_parent">          <textview              android:id="@+id/tt1"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="mohsen kabar" />           <button              android:id="@+id/tt2"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_margintop="113dp"              android:text="button"              android:layout_below="@+id/mohsenkabar"              android:layout_alignparentright="true"              android:layout_alignparentend="true"              android:layout_marginright="93dp"              android:layout_marginend="93dp" />      </relativelayout> 

i need use button , textview.

where can place code?

text=(textview) text.findviewbyid(r.id.tt1); button=(button) button.findviewbyid(r.id.tt2); button.setonclicklistener(new view.onclicklistener() {      @override     public void onclick(view v) {         tt1.settext("kkk mmm bbb ddd");     } }); 

try add code in kabar fragment

  @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     // inflate layout fragment     view v= inflater.inflate(r.layout.fragment_kabar , container, false);        textview  text=(textview) v.findviewbyid(r.id.tt1);       textview  button=(textview) v.findviewbyid(r.id.tt2);        button.setonclicklistener(new view.onclicklistener() {       @override       public void onclick(view v) {       text.settext("kkk mmm bbb ddd");        }    });     return v; } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -