Android action-toolbar tabs size in landscape mode - android

So I'm creating a Material design app for Android. I'm using the new Toolbar instead of using an ActionBar. In one activity, I want to display tabs below the toolbar. This works for both portrait and landscape mode alike, but in landscape mode, the tabs are centered and do not cover the entire width of the view.
Is this by design or is there something I need to know?
This is the layout for my activity:
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:height="?attr/actionBarSize"
android:background="#color/primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:titleTextAppearance="#style/ToolbarTitle"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/toolbar_tabs"
android:height="?attr/actionBarSize"
android:background="#color/primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
The following code initializes this in onCreate:
// Set up the toolbar
this.setSupportActionBar(mToolbar);
ActionBar ab = this.getSupportActionBar();
if (ab != null)
{
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);
ab.setDisplayShowTitleEnabled(true);
}
// Set up the pager and tabs from the pager
mTabViewPager.setAdapter(mTabPagerAdapter);
mTabLayout.setupWithViewPager(mTabViewPager);
The mTabPagerAdapter is a pager adapter that holds the pages to be shown by the tabs as found in the docs.

You can use app:tabGravity="fill" to fit width in entire screen.
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabIndicatorColor="#color/white"
app:tabMode="fixed"
app:tabMaxWidth="0dp"
app:tabSelectedTextColor="#color/white"
app:tabTextColor="#color/white_dim" />`
Meterial design Tab layout detailed tutorial

Related

Centralize TabLayout

I want to centralize TabLayout when app:tabMode="scrollable"
My TabLayout
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="center"
android:background="#android:color/white"
android:gravity="center"
android:visibility="visible"
app:tabGravity="fill"
app:tabIndicatorHeight="0dp"
app:tabMaxWidth="0dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="#color/colorTabSelected"
app:tabTextAppearance="#style/CustomTextStyle"
app:tabTextColor="#color/colorTabNormal"></android.support.design.widget.TabLayout>
With that layout. Tablayout like this (but all tabs are aligned left):
if app:tabMode="fixed", the text in the center tab will have smaller size than the others.
So. I want make the same size of all text in TabLayout if app:tabMode="fixed" (all tabs are aligned central). If app:tabMode="scrollable", I want to centralize tab(like tabMode fixed).
Thanks
SmartTabLayout library could help you having your tabs always in center.
I work around by adding padding, still in app:tabMode="fixed"
app:tabMode="fixed"
app:tabPaddingEnd="1dp"
app:tabPaddingStart="1dp"
to TabLayout.

Toolbar not displaying from inside collapsing toolbar

Here is the code.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/mytoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="#string/app_name"
app:layout_collapseMode="pin"
app:theme="#style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="#+id/main_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_collapseMode="none"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!--<android.support.v4.widget.NestedScrollView-->
<!--android:id="#+id/nestedscroll"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:fillViewport="true"-->
<!--android:scrollbars="horizontal"-->
<!--app:layout_behavior="#string/appbar_scrolling_view_behavior">-->
<android.support.v4.view.ViewPager
android:id="#+id/main_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<!--</android.support.v4.widget.NestedScrollView>-->
<android.support.design.widget.FloatingActionButton
android:id="#+id/main_fab"
android:layout_margin="16dp"
android:src="#android:drawable/ic_media_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="#id/main_viewpager"
app:layout_anchorGravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>
The problem:
1) Toolbar is not visible.
2) Collapsing toolbar doesn't collapse at all. [Solved]
3) Viewpager and FAB also not visible if put inside nestedScrollView. [Solved]
Extra detail:
Layout for fragments of Viewpager have Linearlayout as root and inside that have a recyclerview.
Everything seems to be alright as per the code. Unable to understand what is missing. A good explanation of how coordinator layout and collapsing toolbar work together would also indeed help.
1) Toolbar is not visible.
First of all you need define what Toolbar do you want to use in your activity class:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Change existing xml code:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:title="#string/app_name"
app:layout_collapseMode="parallax">
</android.support.v7.widget.Toolbar>
to:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" //set initial height
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" //this might be also useful
app:title="#string/app_name"
app:layout_collapseMode="parallax" />
2) Collapsing toolbar doesn't collapse at all.
Did your activity using correct theme. Set to your AppBarLayout:
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
as in this example: include_list_viewpager.xml
3) Viewpager and FAB also not visible if put inside nestedScrollView.
There's no reason to do that. Adding these lines:
android:layout_marginTop="?attr/actionBarSize"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
to ViewPager should be enough.
Both of them should be direct children of CoordinatorLayout.
Follow this example: http://blog.nkdroidsolutions.com/collapsing-toolbar-with-tabs-android-example/
If you're new to Material Design or feel a bit lost with some its behaviours, I highly recommend to check Chris Banes Material Design project cheesequare: https://github.com/chrisbanes/cheesesquare/
Hope it will help
First of all you should tell your activity that what toolbar you are using, so in onCreate method you should have:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
your second and third problem should solve together. You should use NestedScrollView as the main layout for fragments inside ViewPager and then inside that, put your LinearLayout or anything else.

Tabs indide material design Toolbar

In pre-5.0 documentation there is an example that shows tabs inside the Action bar:
https://developer.android.com/guide/topics/ui/actionbar.html#Tabs
What is the correct way to achieve that in 5.0+ interface with new Toolbar?
Do I just place widget.TabLayout inside widget.Toolbar?
You can place the tablayout widget inside the toolbarwidget like
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolbar"
android:background="?attr/colorPrimary">
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</android.support.v7.widget.Toolbar>

How to put tablayout into toolbar and they are in the same line in android developer

I want this effect.The tab is in the toolbar,not under the toolbar
I searched all of the website but can't find a way to do that thing.All document tells me how to add a tab under the toolbar but it's not what i want
I just came up with the answer, and it's simpler than I thougth:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<android.support.design.widget.TabLayout
android:id="#+id/main_tabs"
style="#style/MainTabLayout"
app:tabIndicatorHeight="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/home_toolbar"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
So there we have.
It's not the exactly same design as yours, but you just change whatever you want.
And don't forget to set your toolbar on your fragment or activity:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Programmatically hide / show android support design TabLayout inside AppBarLayout

I want to programmatically hide / show the TabLayout in my AppBarLayout. Simply setting visibility to VISIBLE and GONE is not enough, as I want to animate the changes and reclaim the space with my content while the tab retreats and leave the space once the tab is shown back.
Below is the relevant part of my layout XML -
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
app:tabMode="scrollable"
android:layout_marginStart="42dp"
android:layout_marginLeft="42dp"
style="#style/MainTabLayout">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
As any ViewGroup subclass, AppBarLayout allows for automatic animations during the adding/removal of child views. You just need to add android:animateLayoutChanges="true" (default to false) in your layout file.
As for reclaiming the space content when the view is gone, all you have to do is use setVisibility(View.GONE) rather than setVisibility(View.INVISIBLE), because the latter holds the space for the invisible view.

Categories

Resources