0

How do I add button click in tablayout? I tried everywhere but can't implement R.id and onclicklisener

My xml:

<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

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

    <Button
        android:id="@+id/quiz2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

My java class:

public class ChatFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setHasOptionsMenu(true);
        return inflater.inflate(R.layout.fragment_chat, container, false);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_calls, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_call) {
            Toast.makeText(getActivity(), "Clicked on " + item.getTitle(), Toast.LENGTH_SHORT)
                .show();
        }
        return true;
    }
}

I used this tutorial for my tablayout: https://www.youtube.com/watch?v=Vxiy_h5hNII

github: https://github.com/codingdemos/Tablayout

gotwo
  • 663
  • 8
  • 16
Rince Mathew
  • 27
  • 1
  • 6
  • What `TabLayout` are you talking about? I don't see one in either the layout XML, or the `Fragment` class. I also don't see where you're trying to set an `OnClickListener` on anything. – Mike M. Aug 20 '18 at 21:20
  • that's why i add github link. i cant declare findviewbyid anyware – Rince Mathew Aug 21 '18 at 20:21
  • That's not really clear from your question. You should've shown what you tried, and exactly what was giving you errors. Linking to someone else's demo project doesn't tell us what problems you're having. Anyway, have a look at [this answer](https://stackoverflow.com/a/6496013). – Mike M. Aug 21 '18 at 20:42
  • i got answer, thanks – Rince Mathew Aug 22 '18 at 19:39

2 Answers2

0

You have to add : private Button mButton2; after extends Fragment {

Then you change return in the onCreateView to View view. After that, you add mButton2 = (button) view.findViewById(R.id.button2) before setHasOptionsMenu(true). After this you should be able to find your id.

ToAsk
  • 1
  • 5
0
public class StatusFragment extends Fragment {
Button button2;




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


   View StatusFragmentView=inflater.inflate(R.layout.fragment_status, container, false);
   button2= StatusFragmentView.findViewById(R.id.quiz2);
   setHasOptionsMenu(true);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent=new Intent(getActivity(),Splash.class);
            startActivity(intent);
        }
    });


    return StatusFragmentView;
    }

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_calls, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.action_call) {
        Toast.makeText(getActivity(), "Clicked on " + item.getTitle(), Toast.LENGTH_SHORT)
                .show();
    }
    return true;
}

}

Rince Mathew
  • 27
  • 1
  • 6