I need to use the setSupportActionBar in fragment which I am unable to also I am unable to use setContentView please to help with it also Thankyou in advance the related code is given
public class StudentrFragment extends Fragment { Toolbar toolbar; TabLayout tabLayout; ViewPager viewPager; ViewPagerAdapter viewPagerAdapter; public StudentrFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.tabbar_layout); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); tabLayout = (TabLayout) findViewById(R.id.tabLayout); viewPager = (ViewPager) findViewById(R.id.viewPager); viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); viewPagerAdapter.addFragments(new CivilFragment(),"Civil Dept"); viewPagerAdapter.addFragments(new ComputerFragment(),"CSE Dept"); viewPagerAdapter.addFragments(new EeeFragment(),"EEE Dept"); viewPagerAdapter.addFragments(new EceFragment(),"ECE Dept"); viewPager.setAdapter(viewPagerAdapter); tabLayout.setupWithViewPager(viewPager); } }
Asking for help, clarification, or responding to other answers.
getActivity()). getToolbar(); will be the right answer!! for getting the Toolbar in fragment!!
Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Create two FragmentActivity and add the codes which are given below.
To use the ActionBar utility methods, call the activity's getSupportActionBar() method. This method returns a reference to an appcompat ActionBar object. Once you have that reference, you can call any of the ActionBar methods to adjust the app bar. For example, to hide the app bar, call ActionBar.
You can setSupportActionbar like this in fragments:
((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);
You need to inflate tabbar_layout
in onCreateView
of Fragment
. Like this:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.tabbar_layout, container, false); //YOUR STUFF return rootView; }
The suggested solution works, but it doesn't look elegant, ideally a fragment shouldn't know anything about its parent activity. An alternative might be not to use setSupportActionBar
at all. If you use navigation library it might be easier to add a Toolbar to the fragment layout and setup it using NavigationUI, for example:
<!-- my_fragment.xml --> <androidx.constraintlayout.widget.ConstraintLayout ... > <com.google.android.material.appbar.MaterialToolbar android:id="@+id/toolbar" app:menu="@menu/my_fragment_menu" ... /> </androidx.constraintlayout.widget.ConstraintLayout>
class MyFragment : Fragment() { ... override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val navController = findNavController() binding.toolbar.setupWithNavController(navController) binding.toolbar.setOnMenuItemClickListener { when (it.itemId) { // these ids should match the item ids from my_fragment_menu.xml file R.id.edit -> { Log.i("MyFragment", "edit menu item is clicked") // by returning 'true' we're saying that the event // is handled and it shouldn't be propagated further true } else -> false } } // if you prefer not to use xml menu file // you can also add menu items programmatically val shareMenuItem = binding.toolbar.menu.add(R.string.share) shareMenuItem.setOnMenuItemClickListener { Log.i("MyFragment", "share menu item is clicked") true } } }
You can find the full GitHub example here. Also take a look at another question Is setSupportActionbar required anymore? and my answer for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With