简体   繁体   中英

How to display a toast message on clicking a button in a fragment?

First of all, forgive me if this is a silly question because I am relatively new in this field. So basically, I have an activity which contains a fragment. This fragment has three buttons. And when any of this buttons are clicked, I wanted to display specific Toast messages . I used the onClick() method which works fine in an activity. But when I use the same in a fragment, the app crashes. Please help me in this regard.Name of the Activity is "User.java" and its XML file is "activity_user.xml". Name of Fragment "user_home.java" and its xml file is "user_home_layout.xml".
I have attached two images, first one contains the activity and its XML file while the second one contains the fragment and its sml file.

1. Activity 2. Fragment

You dint find button in your fragment,

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.user_home_layout, container, false);

    btn1=(Button)view.findViewById(R.id.GOT);  
    btn2=(Button)view.findViewById(R.id.SH);  
    btn3=(Button)view.findViewById(R.id.TD);

return view;
}

and then implement click listener and and in Toast just use getActivity()

Toast.makeText(getActivity(), "Game Of Thrones", Toast.LENGTH_SHORT).show();

Implement method

public void ButtonClick(View v) {
switch(v.getId()) {

 case R.id.GOT:


  Toast.makeText(getActivity(), "Game of Thornes", Toast.LENGTH_LONG).show();

   break;


 case R.id.SH:


  Toast.makeText(getActivity(), "Sherlock", Toast.LENGTH_LONG).show();

   break;


 case R.id.TD:


  Toast.makeText(getActivity(), "True Detective", Toast.LENGTH_LONG).show();

   break;
   }
 }    

You need to set a custom OnClickListener to your button :

yourButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "your toast text", Toast.LENGTH_LONG).show();
            }
        });

In your activity:

Toast.makeText(YourActivity.this, "your message", Toast.LENGTH_SHORT).show();

In your Fragment:

Toast.makeText(this.getActivity(), "your message", Toast.LENGTH_SHORT).show();

@TonyMathew, welcome to the Android world. Your implementation of ButtonClick is wrong. This is an extensive tutorial of how you should use Fragments. There is an example of handling click events as well. Long story short, you need to find your buttons in your fragment xml layout by id. DON'T USE BUTTON'S TEXT!

Please, search the web for info, before posting a question here. The chances are that you will find what you are looking for!

PS Usually in Java you should name your class in the following way: ClassName.java and not class_name.java

只需将getActivity()放在makeText()的上下文中,如下所示:

Toast.makeText(getActivity(), "Your message", Toast.LENGTH_SHORT).show();

The problem is that your method is not found in activity, and so, an exception is raised (the methods from fragments doesn't count here). To fix that you have 2 options:

  1. Move the ButtonClick method in activity and do some small changes (you'll have to obtain in a different way the Context ) and keep the xml file the same;
  2. Set onClickListener for button and handle the onClick event inside the listener.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM