0

How do I choose a method called "favorited" through an xml layout file with the

android:onClick 

xml method?

the "favorited" method is located here:

tk.talcharnes.popularmovies.MovieDetailsFragment

I have tried many things but nothing works!

Here is the code for favorited

/**
 * A placeholder fragment containing a simple view.
 */
public class MovieDetailsFragment extends Fragment {

public MovieDetailsFragment() {
}

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

    //get movie object in order to extract details
    Intent intent = getActivity().getIntent();
    int movie_number = intent.getIntExtra("Movie_number", 0);
    MovieModel movie = PostersFragment.getMovieModelList().get(movie_number);

    //set title in details view
    TextView titleView = (TextView) rootView.findViewById(R.id.movie_details_text);
    titleView.setText(movie.getTitle());

    //set poster into details view
    ImageView poster = (ImageView)rootView.findViewById(R.id.poster);
    Picasso.with(getContext()).load(movie.getPoster_path()).placeholder(R.drawable.temp_poster).into(poster);

    // set movie year in details view
    TextView release_date = (TextView)rootView.findViewById(R.id.release_date);
    if(movie.getRelease_date().length() > 3){
    release_date.setText(movie.getRelease_date().substring(0,4));}
    else if (movie.getRelease_date() == null){
        release_date.setText("Release date not available");
    }
    else{
        release_date.setText((movie.getRelease_date()));
    };

    //set vote average in details view
    TextView vote_average = (TextView) rootView.findViewById(R.id.vote_average);
    vote_average.setText(movie.getVote_average() + " /10");

    //set overview in details view
    TextView overview = (TextView) rootView.findViewById(R.id.overview);
    overview.setText(movie.getOverview());


    return rootView;
}
public void favorited(){
    CheckBox favorited = (CheckBox) getView().findViewById(R.id.favorite);
    if (favorited.isChecked()){

        Toast.makeText(getContext(), "ITS CHECKED", Toast.LENGTH_SHORT).show();
    }
}

}
Tal C
  • 547
  • 1
  • 8
  • 17
  • 1
    Possible duplicate of [How to handle button clicks using the XML onClick within Fragments](http://stackoverflow.com/questions/6091194/how-to-handle-button-clicks-using-the-xml-onclick-within-fragments) – Janki Gadhiya Jun 15 '16 at 11:23

5 Answers5

1

if you declared android:onClick="favorited" in your xml you need to implement the following method in the activity the fragment is in:

public void favorited(View view) {
    //handle click
}

Important: the method needs to be in the activity. You can find more info at this answer

Community
  • 1
  • 1
Katharina
  • 1,612
  • 15
  • 27
0

Try below code

<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="@+id/btn_favorite"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Click me!"
  android:onClick="favorited" />
Nikita Shah
  • 156
  • 10
  • So it won't work if I put it in fragment? Or did I just mess up the fragment as it did work once I moved it elsewhere! Also thanks for the help!!! – Tal C Jun 15 '16 at 11:21
0

In your xml view specify onClick method like this:

<Button android:id="@+id/mybutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me!"
    android:onClick="favorited" />

and then in your fragment define method like this:

public void favorited(View v) {
    // does something very interesting
}

Remember without favorited(View v) your method won't be called from xml.

Er. Kaushik Kajavadara
  • 1,657
  • 2
  • 16
  • 37
  • So it won't work if I put it in fragment? Or did I just mess up the fragment as it did work once I moved it elsewhere! Also thanks for the help!!! – Tal C Jun 15 '16 at 11:21
  • 1
    no it won't work for fragment... for fragment use below code: Button button = (Button) view.findViewById(R.id.btn_conferma); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // do something } }); – Er. Kaushik Kajavadara Jun 15 '16 at 11:24
0

Use onClick listener instead xml click declaration: in your xml ad id to clickable element.

<Button android:id="@+id/btn_favorite"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>

And in your fragment

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

     Button faw = (Button) rootView.findViewById(R.id.favorite);
     faw.setOnclickListener(new View.OnClickListener(){
      @Override
       onClick(){
          favorited(); //or do another action
         }
      });
    }

This method better to reusing and more safety

once2go
  • 1,452
  • 1
  • 13
  • 21
0

add this to the xml

android:onClick="ButtonClick"

and this in the code

public void ButtonClick(View v) {
favorited();
}

you can also add multiple buttons to the same ButtonClick method, just simply add this in there:

switch (v.getId()) {
  case R.id.button1:
    doSomething1();
    break;
}

all of this and more is available all over the place though, here for example: Android OnClickListener - identify a button

Community
  • 1
  • 1