Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implementing OnClickListener for a fragment on android

I have a sliding menu project and inside home layout another layout is called as a fragment :

this is the 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 click listener inside my java class in order to submit a 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 above code snippet it throws me an error on undefined methods such as 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" />

like image 355
Mac Taylor Avatar asked Jan 01 '15 05:01

Mac Taylor


People also ask

Which are the ways to set OnClickListener for button in android?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

How does OnClickListener work in android?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.

How do I use OnClickListener?

To implement View. OnClickListener in your Activity or Fragment, you have to override onClick method on your class. Firstly, link the button in xml layout to java by calling findViewById() method. R.

How do you go from activity to fragment?

Can I go from fragment to activity Android? You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.


1 Answers

While working with Fragment, you have to inflate view.

So when you want to use any widget you have to use view object with findViewById().

Simply Do 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 other 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;

     }

    }
}
like image 104
Pragnesh Ghoda シ Avatar answered Nov 03 '22 00:11

Pragnesh Ghoda シ