0

This is a part of my calc_fragment.xml file ,

        <Button
            android:id="@+id/b7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@drawable/roundedbutton"
            android:fontFamily="sans-serif-thin"
            android:text="Add"
            android:textColor="@color/light_grey"
            android:textSize="45sp"
            android:onClick="clicked"/>

that is linked with the following fragment:

public class CalcFragment extends Fragment {

private TextView textView;
private String  text;
private Vibrator vibe;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.calc_fragment, container, false);
    return rootView;
}

public void clicked (View v){   <--- "Method clicked is never used"
...content...
}

I get a warning "Method clicked is never used" but I my button is linked with this method

tur1ng
  • 1,082
  • 1
  • 10
  • 24

3 Answers3

2

A Fragment is tightly linked with its activity. So, the above method declaration in xml will basically look for that method in the Activity class. One of the ways is that you can explicitly link your method and the button:

public class CalcFragment extends Fragment { 

private TextView textView;
private String  text;
private Vibrator vibe;

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.calc_fragment, container, false);

    View myButton = rootView.findViewById(R.id.b7);
    rootView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            clicked(v);
        }

    });

    return rootView;
} 

public void clicked (View v){   // Your method
...content... 
} 

Get rid of the 'android:onClick="clicked"' property in your xml.

Shaishav
  • 5,282
  • 2
  • 22
  • 41
0

This is because the XML code for handling clicks does not work for the Fragment class, it currently works only with the Activity class. As such, this method will never be called. See this question for more details.

Community
  • 1
  • 1
TR4Android
  • 3,200
  • 16
  • 21
0

In the onCreateView method of the CalcFragment, you need to declare and initialize the button after the initializing the rootView:

Button b7 = (Button) rootView.findViewById(R.id.b7);

You then need an onClickListener instead of defining your own clicked method. There are two methods to listen to a click of a button.

The first method:

You can set the onClickListener within the onCreatView as follows:

b7.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       // Do something         
    }
});   

The second method:

You can define an onClickListener as follows:

final View.OnClickListener b7OnClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // Do something
    }
};

When using the second method, you will need to set the onClickListener like you did in the first method however in this case you will pass the name of the onClickListener object you created.

Example:

b7.setOnClickListener(b7OnClickListener);