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

Viewed 99

I created a Fragment and called it Kabar and I can see that the Button and the TextView appears as expected.

Here is my 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 the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_kabar , container, false);
        return v;
    }
}

This is my 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 to use the Button and the TextView.

Where can I place this 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");
    }
});
5 Answers
Related