Lucas Ignacio Alfaro Godoy Lucas Ignacio Alfaro Godoy - 7 months ago 51
Java Question

Toolbar title not showing in tabbed activity

Implemented a tabbed activity using TabsLayout, but now the title of the activity doesn't show on the toolbar, tried various fixes I found on the internet but none seem to work.

activity's xml:




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


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

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay"
app:tabMode="scrollable">

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

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

</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" />




OnCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ordenes_de_viaje_tabbed);

// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
Intent intent = getIntent();
item = intent.getIntExtra("item", 0);
mViewPager.setCurrentItem(item);
Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mActionBarToolbar);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("My title");
getSupportActionBar().setDisplayShowTitleEnabled(true);

Answer

Your tabLayout should not be a child of your toolbar it should be below it and user ?attr/actionBarSize witch will give you accurate action bar size regardless to the device dpi

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="title"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    app:popupTheme="@style/AppTheme.PopupOverlay">
</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:marginTop="?attr/actionBarSize"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:tabMode="scrollable">
    </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" />