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.