0

My application had a single activity and I decided to put the main content inside a fragment, so I can continue using my drawer with other activities. However I can't make the buttons work:

 Button profile= findViewById(R.id.button1);
    profile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplication(),"Sürücüler",Toast.LENGTH_SHORT).show();
        }
    });

    Button education= findViewById(R.id.button2);
    education.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplication(),"Takımlar ve arabalar",Toast.LENGTH_SHORT).show();
        }
    });

    Button health= findViewById(R.id.button3);
    health.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplication(),"Pistler",Toast.LENGTH_SHORT).show();
        }
    });

    Button goals= findViewById(R.id.button4);
    goals.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplication(),"Sıralama",Toast.LENGTH_SHORT).show();
        }
    });

    Button finance= findViewById(R.id.button5);
    finance.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplication(),"Grand Prix Tarihi",Toast.LENGTH_SHORT).show();
        }
    });

    Button comfort= findViewById(R.id.button6);
    comfort.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplication(),"GP kart oyunu",Toast.LENGTH_SHORT).show();
        }
    });

If I leave this code at MainActivity it doesn't work (because the buttons aren't created yet?) but when I move them into the fragment, findViewById and getApplicationContext doesn't work. How can make Toast messages compatible with fragments and how can I make sure my fragment can use findviewbyid?

I am beginner so I am sorry if this is simple.

Any help would be appreciated.

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
  • Have you inflated and returned a `View` inside the fragment's `getView()`? I can't make more assumptions without the entire fragment code. – Susmit Agrawal Aug 20 '18 at 17:36
  • return inflater.inflate(R.layout.fragment_home, container, false); This is what I have inside OnCreateView. – Ahmet B. Arpa Aug 20 '18 at 17:47

3 Answers3

0

Based on your comment,

return inflater.inflate(R.layout.fragment_home, container, false);

should actually be:

View root = inflater.inflate(R.layout.fragment_home, container, false);
Button profile =(Button)root.findViewById(R.id.button1);
profile.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(getApplication(),"Sürücüler",Toast.LENGTH_SHORT).show();
    }
});

//similarly for other buttons

return root;
Susmit Agrawal
  • 3,649
  • 2
  • 13
  • 29
0

if the button in the fragment XML you can use

 Button profile= v.findViewById(R.id.button1);

so v is your fragment view in the method "onCreateView"

View v=inflater.inflate(R.layout.fragment_home, container, false);

and in the last of the method

return v;
Momen Zaqout
  • 1,508
  • 1
  • 16
  • 17
0

A fragment is usually used as part of an activity's user interface and contributes its own layout to the activity. So first you need to create a layout for the fragment with all the button in it.

To provide a layout that we created to the fragment, you must implement the onCreateView() callback method in the fragment class, which the Android system calls when it's time for the fragment to draw its layout. Your implementation of this method must return a View that is the root of your fragment's layout.

public  class ExampleFragment extends Fragment {

 Button health;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View root = inflater.inflate(R.layout.example_fragment, container, false);
    // bind views like this:-
    health= root.findViewById(R.id.button3);

    return  root;  
  }
}

To manage the fragments in your activity, you need to use FragmentManager. To get it, call getSupportFragmentManager() from your activity. Now the activity class add the following code to add fragment to activity , :-

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

 // Commit the transaction
 transaction.commit();

Where R.id.fragment_container is id of the container layout where the fragments are to be added in the activty layout , generally a framelayout.

How to use Fragments in android