Implement OnClickListener for fragment on android

I have a sliding menu project and inside the home layout another layout is called a snippet:

this is HomeFragment.java:

package info.androidhive.slidingmenu; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class HomeFragment extends Fragment { public HomeFragment(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_home, container, false); return rootView; } } 

I need to import this listener into my java class in order to submit the form.

 //CheckFal: btnCheckFalAction = (Button) findViewById(R.id.btnCheckFal); btnCheckFalAction.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show(); } } 

But when I add the code snippet above, it causes an error in undefined methods like findViewById, OnClickListener, onClick

The button is inside this layout fragment_home.xml

  <Button android:id="@+id/btnCheckFal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/relativeLayout1" android:layout_centerHorizontal="true" android:layout_marginTop="19dp" android:text="@string/CheckFal" /> 

+6
source share
4 answers

When working with Fragment you need to inflate the performance.

Therefore, when you want to use any widget, you should use a view object with findViewById() .

Just do it like this ...

 public class HomeFragment extends Fragment { public HomeFragment(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_home, container, false); btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal); // you have to use rootview object.. btnCheckFalAction.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(), "Hello World", Toast.LENGTH_LONG).show(); } }); return rootView; } } 

OR try another way.

 public class HomeFragment extends Fragment implements OnClickListener{ public HomeFragment(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_home, container, false); btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal); // you have to use rootview object.. btnCheckFalAction.setOnClickListener(this); return rootView; } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.btnCheckFal : //your code... break; default: break; } } } 
+16
source

you can use

 rootView.findViewById(...) 

in your code because your class does not inherit / does not implement this method

+3
source

first ... you should find the View in rootView, for example

 btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal); 

then you can finish the buttons

+2
source
  // Inflate the layout for this fragment in on create RelativeLayout mLinearLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_a, container, false); cd = new ConnectionDetector(getActivity()); ImageButton mButton = (ImageButton) mLinearLayout.findViewById(R.id.imageButton1); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // here you set what you want to do when user clicks your button, // eg launch a new activity Toast.makeText(getActivity(),"Opening Maps",Toast.LENGTH_LONG).show(); } }); 

you can call it by sight

0
source

Source: https://habr.com/ru/post/980391/


All Articles