Why does the Tabbed Activity work well after I remove the code setSupportActionBar(toolbar) created by the...












0















The following code is created by the Create New Project wizard of Android Studio 3.2.1 , it's a Tabbed Activity.



I'm so strange why Android Studio 3.2.1 want to add the code setSupportActionBar(toolbar), and why the App works well and can display 3 tabs after I delete setSupportActionBar(toolbar).



Could you tell me?



Code



class MainActivity : AppCompatActivity() {

private var mSectionsPagerAdapter: SectionsPagerAdapter? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

setSupportActionBar(toolbar) //I can delete it


mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)

// Set up the ViewPager with the sections adapter.
container.adapter = mSectionsPagerAdapter

container.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs))
tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(container))

}



inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

override fun getItem(position: Int): Fragment {

}

override fun getCount(): Int {
// Show 3 total pages.
return 3
}
}


class PlaceholderFragment : Fragment() {


}
}


Layout



<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
app:title="@string/app_name"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:layout_scrollFlags="scroll|enterAlways">

</android.support.v7.widget.Toolbar>

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.design.widget.TabItem
android:id="@+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_1"/>

<android.support.design.widget.TabItem
android:id="@+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_2"/>

<android.support.design.widget.TabItem
android:id="@+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_3"/>

</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>









share|improve this question



























    0















    The following code is created by the Create New Project wizard of Android Studio 3.2.1 , it's a Tabbed Activity.



    I'm so strange why Android Studio 3.2.1 want to add the code setSupportActionBar(toolbar), and why the App works well and can display 3 tabs after I delete setSupportActionBar(toolbar).



    Could you tell me?



    Code



    class MainActivity : AppCompatActivity() {

    private var mSectionsPagerAdapter: SectionsPagerAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    setSupportActionBar(toolbar) //I can delete it


    mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)

    // Set up the ViewPager with the sections adapter.
    container.adapter = mSectionsPagerAdapter

    container.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs))
    tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(container))

    }



    inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

    override fun getItem(position: Int): Fragment {

    }

    override fun getCount(): Int {
    // Show 3 total pages.
    return 3
    }
    }


    class PlaceholderFragment : Fragment() {


    }
    }


    Layout



    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    app:title="@string/app_name"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_weight="1"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:layout_scrollFlags="scroll|enterAlways">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.TabItem
    android:id="@+id/tabItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/tab_text_1"/>

    <android.support.design.widget.TabItem
    android:id="@+id/tabItem2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/tab_text_2"/>

    <android.support.design.widget.TabItem
    android:id="@+id/tabItem3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/tab_text_3"/>

    </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email"/>

    </android.support.design.widget.CoordinatorLayout>









    share|improve this question

























      0












      0








      0








      The following code is created by the Create New Project wizard of Android Studio 3.2.1 , it's a Tabbed Activity.



      I'm so strange why Android Studio 3.2.1 want to add the code setSupportActionBar(toolbar), and why the App works well and can display 3 tabs after I delete setSupportActionBar(toolbar).



      Could you tell me?



      Code



      class MainActivity : AppCompatActivity() {

      private var mSectionsPagerAdapter: SectionsPagerAdapter? = null

      override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)

      setSupportActionBar(toolbar) //I can delete it


      mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)

      // Set up the ViewPager with the sections adapter.
      container.adapter = mSectionsPagerAdapter

      container.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs))
      tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(container))

      }



      inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

      override fun getItem(position: Int): Fragment {

      }

      override fun getCount(): Int {
      // Show 3 total pages.
      return 3
      }
      }


      class PlaceholderFragment : Fragment() {


      }
      }


      Layout



      <?xml version="1.0" encoding="utf-8"?>
      <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/main_content"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:context=".MainActivity">

      <android.support.design.widget.AppBarLayout
      android:id="@+id/appbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingTop="@dimen/appbar_padding_top"
      android:theme="@style/AppTheme.AppBarOverlay">

      <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      app:title="@string/app_name"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      android:layout_weight="1"
      android:background="?attr/colorPrimary"
      app:popupTheme="@style/AppTheme.PopupOverlay"
      app:layout_scrollFlags="scroll|enterAlways">

      </android.support.v7.widget.Toolbar>

      <android.support.design.widget.TabLayout
      android:id="@+id/tabs"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

      <android.support.design.widget.TabItem
      android:id="@+id/tabItem"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/tab_text_1"/>

      <android.support.design.widget.TabItem
      android:id="@+id/tabItem2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/tab_text_2"/>

      <android.support.design.widget.TabItem
      android:id="@+id/tabItem3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/tab_text_3"/>

      </android.support.design.widget.TabLayout>
      </android.support.design.widget.AppBarLayout>

      <android.support.v4.view.ViewPager
      android:id="@+id/container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

      <android.support.design.widget.FloatingActionButton
      android:id="@+id/fab"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="end|bottom"
      android:layout_margin="@dimen/fab_margin"
      app:srcCompat="@android:drawable/ic_dialog_email"/>

      </android.support.design.widget.CoordinatorLayout>









      share|improve this question














      The following code is created by the Create New Project wizard of Android Studio 3.2.1 , it's a Tabbed Activity.



      I'm so strange why Android Studio 3.2.1 want to add the code setSupportActionBar(toolbar), and why the App works well and can display 3 tabs after I delete setSupportActionBar(toolbar).



      Could you tell me?



      Code



      class MainActivity : AppCompatActivity() {

      private var mSectionsPagerAdapter: SectionsPagerAdapter? = null

      override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)

      setSupportActionBar(toolbar) //I can delete it


      mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)

      // Set up the ViewPager with the sections adapter.
      container.adapter = mSectionsPagerAdapter

      container.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs))
      tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(container))

      }



      inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

      override fun getItem(position: Int): Fragment {

      }

      override fun getCount(): Int {
      // Show 3 total pages.
      return 3
      }
      }


      class PlaceholderFragment : Fragment() {


      }
      }


      Layout



      <?xml version="1.0" encoding="utf-8"?>
      <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/main_content"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:context=".MainActivity">

      <android.support.design.widget.AppBarLayout
      android:id="@+id/appbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingTop="@dimen/appbar_padding_top"
      android:theme="@style/AppTheme.AppBarOverlay">

      <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      app:title="@string/app_name"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      android:layout_weight="1"
      android:background="?attr/colorPrimary"
      app:popupTheme="@style/AppTheme.PopupOverlay"
      app:layout_scrollFlags="scroll|enterAlways">

      </android.support.v7.widget.Toolbar>

      <android.support.design.widget.TabLayout
      android:id="@+id/tabs"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

      <android.support.design.widget.TabItem
      android:id="@+id/tabItem"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/tab_text_1"/>

      <android.support.design.widget.TabItem
      android:id="@+id/tabItem2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/tab_text_2"/>

      <android.support.design.widget.TabItem
      android:id="@+id/tabItem3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/tab_text_3"/>

      </android.support.design.widget.TabLayout>
      </android.support.design.widget.AppBarLayout>

      <android.support.v4.view.ViewPager
      android:id="@+id/container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

      <android.support.design.widget.FloatingActionButton
      android:id="@+id/fab"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="end|bottom"
      android:layout_margin="@dimen/fab_margin"
      app:srcCompat="@android:drawable/ic_dialog_email"/>

      </android.support.design.widget.CoordinatorLayout>






      android kotlin






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 7:00









      HelloCWHelloCW

      11743098




      11743098
























          1 Answer
          1






          active

          oldest

          votes


















          1














          setSupportActionBar() method is used to designate the Toolbar as the ActionBar.



          If you want to add backButton or add menu items, etc. in the Toolbar it should be designated as ActionBar.



          Here you have not done any above things. So you were able to safely remove the line of code.



          For details about Toolbar refer: Toolbar | Android Developers






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406779%2fwhy-does-the-tabbed-activity-work-well-after-i-remove-the-code-setsupportactionb%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            setSupportActionBar() method is used to designate the Toolbar as the ActionBar.



            If you want to add backButton or add menu items, etc. in the Toolbar it should be designated as ActionBar.



            Here you have not done any above things. So you were able to safely remove the line of code.



            For details about Toolbar refer: Toolbar | Android Developers






            share|improve this answer




























              1














              setSupportActionBar() method is used to designate the Toolbar as the ActionBar.



              If you want to add backButton or add menu items, etc. in the Toolbar it should be designated as ActionBar.



              Here you have not done any above things. So you were able to safely remove the line of code.



              For details about Toolbar refer: Toolbar | Android Developers






              share|improve this answer


























                1












                1








                1







                setSupportActionBar() method is used to designate the Toolbar as the ActionBar.



                If you want to add backButton or add menu items, etc. in the Toolbar it should be designated as ActionBar.



                Here you have not done any above things. So you were able to safely remove the line of code.



                For details about Toolbar refer: Toolbar | Android Developers






                share|improve this answer













                setSupportActionBar() method is used to designate the Toolbar as the ActionBar.



                If you want to add backButton or add menu items, etc. in the Toolbar it should be designated as ActionBar.



                Here you have not done any above things. So you were able to safely remove the line of code.



                For details about Toolbar refer: Toolbar | Android Developers







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 7:24









                Mangaldeep PannuMangaldeep Pannu

                8218




                8218
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406779%2fwhy-does-the-tabbed-activity-work-well-after-i-remove-the-code-setsupportactionb%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                    ComboBox Display Member on multiple fields

                    Is it possible to collect Nectar points via Trainline?