Center active tab inside TabLayout - android

I am using Google's new TabLayout from design support library and was wondering how can I center active tab inside that layout.
Currently I am centering whole layout like this:
<android.support.design.widget.TabLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
And now I would like to center active tab inside of that layout. Something like how it was done before TabLayout. No matter how many tabs were there, active one was always in center. How can I accomplish that? Thank you.

Need to use padding for SlidingTabStrip
Look here: https://stackoverflow.com/a/36886331/651770

Related

TabLayout inside ViewPager is not working

In this page Google explicitly said TabLayout can be integrated into ViewPager in layout xml file.
ViewPager integration
If you're using a ViewPager together with this layout, you can call
setupWithViewPager(ViewPager) to link the two together. This layout
will be automatically populated from the PagerAdapter's page titles.
This view also supports being used as part of a ViewPager's decor, and
can be added directly to the ViewPager in a layout resource file like
so:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
However, whatever I tried, I cannot make TableLayout appears. Separate both is fine. I wonder is this actually working??
I don't think TabLayout works as a ViewPager child as they claim.
Any view that wants to be a child of ViewPager has to set a boolean layout param property specific to ViewPager called isDecor. I just looked through the source code and I don't see any place where either the ViewPager or TabLayout set this layout param property.
I think it was something they meant to do and either a) they never did it or b) there was an issue so they took it out. Just use the TabLayout outside ViewPager, there's no real benefit to having it be a decor view.

How is TabItem used when placed in the layout XML?

The TabLayout documentation gives an example of nesting TabItem directly inside TabLayout like so:
<android.support.design.widget.TabLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.design.widget.TabItem
android:text="#string/tab_text"/>
<android.support.design.widget.TabItem
android:icon="#drawable/ic_android"/>
</android.support.design.widget.TabLayout>
But it gives no example of how this could be used in practice, and the documentation for TabItem says:
This view is not actually added to TabLayout, it is just a dummy which allows setting of a tab items's text, icon and custom layout.
So what is TabItem for? After extensive Googling, I cannot find a single example of anyone defining TabItems in XML. Is there any way to set up a tabbed activity using TabItem in the resource file as shown above?
This appears to be a relatively recent addition to the design library, apparently added in version 23.2.0, though it's not mentioned in the revision history. It's functionality is pretty basic, and the only attributes it seems to use are the three given in its docs: text, icon, and layout.
From testing, it seems it's basically an XML shortcut for creating a new Tab, and setting its text, icon, and custom View, as one would usually do in code. When it says "This view is not actually added to TabLayout", I believe it's meant to suggest that it's not a View in the regular sense, in that you can't set any kind of standard layout attribute on it, like layout_width or background. It simply serves to cause the TabLayout to create a new Tab for each TabItem, and call setText(), setIcon(), and setCustomView() accordingly.
For example, to add a Tab in code, we would usually do something like this:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
// Add Tab
TabLayout.Tab tab = tabLayout.newTab();
tab.setCustomView(R.layout.tab);
tab.setText("Tab 1");
tab.setIcon(R.drawable.ic_launcher);
tabLayout.addTab(tab);
Whereas now we can replace everything after the comment above by adding a TabItem in the layout.
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:layout="#layout/tab"
android:text="Tab 1"
android:icon="#drawable/ic_launcher" />
</android.support.design.widget.TabLayout>
Do note that the same requirements for the custom View layout still apply. That is, the TextView for the text must have the system Resource ID #android:id/text1, and the ImageView for the icon must have the ID #android:id/icon. As an example, the R.layout.tab from above:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView android:id="#android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Quick addition to #Mikes very helpful answer:
Android Studio now has a Template on how to use a TabLayout with TabItem setup in an XML layout. Create all needed files with "New > Activity > Tabbed Activity" and choose "Action Bar Tabs(with ViewPager)" as shown in the screenshot:
If you want to adjust the look of the TabItem without a custom view: use white vector assets as tab android:icon and tint them with a selector (providing different colors based on android:state_selected)
The color of the line under the currently selected tab is set as app:tabIndicatorColor on tag TabLayout.
It took me a while to get it to work, so the complete steps turned into such a long answer that I don't want to copy them here. You can find my more detailed answer with full code at:
https://stackoverflow.com/a/49603559/414581
please see com.google.android.material.tabs.TabItem class it accepts icon text from attributes, but seems like you will need to add Tags on runtime.

Is there any way to have embedded scrollable tabs?

I have a layout in my head that should look like that: http://i.imgur.com/H1nTRvd.png
Only part that will be dynamic is the blue one. I don't know the number of tabs that will be created before I load the activity, hence the number is acquired from server (could be either 5 or 24 for all I know).
The bottom buttons should not move when I swipe and the blue area changed.
Currently I have this implemented w/o tabs list using embedded fragment in my activity and gesture listener. There's no good looking transaction animation between fragments.
#Nick Pakhomov: You can use PagerTabStrip
PagerTabStrip
is intended to be used as a child view of a ViewPager widget in your XML layout. PagerTabStrip is most often used with fragment, which is a convenient way to supply and manage the Lifecycle of each fragment.
So here’s how a ViewPager with a PagerTabStrip in it would look like in the layout file:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_tab_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
/>
</android.support.v4.view.ViewPager>
Please check this PagerSlidingTabStrip demo . I hope it will helps you .

I want to devleope dynamic android screen like this

I want to develope screen like the below..which contains two tabs with right alignment!
actually it's the dynamic screen and top part should be fixed with two tabs when ever they click the tabs it's different fragment. please suggest me the best way.
You can use ViewPager for this.
<android.support.v4.view.ViewPager
android:id="#+id/vpPager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
Here is a link for your reference.
Design two different layout files and switch them on Tabchange.
You can either use RadioGroup to act like tabs with fragments
like this https://stackoverflow.com/a/17541555/3020568
OR you can use PagerSlidingTabStrip library and customize it like the way you want
https://github.com/astuetz/PagerSlidingTabStrip

How to make a swipe view screens without tab layout in android?

I am trying to figure out how to make a swipe view without a tab layout.
In the figure above,
1. I want to make a swipe view that can navigate page left and right.
2. Icon 1 is a global menu that needed to be there all the time while swipping.
3. Icon 3 is a bottom bar. How can I make like that way?
Any kind of suggestions and tutorial links would be appreciated. Thanks in advance.
i don't have any links for the same,but still i will tell you the very simple logic to create:
1.First remove the title bar using
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
2.Use the following Structure
<RelativeLayout>
<ViewPager android:layout_height="fill_parent"
android:layout_width="fill_parent"> //full screen
<RelativeLayout android:id="#+id/header"> -->for header
android:layout_alignParentTop="true"
</RelativeLayout>
<RelativeLayout> -->for inicators
android:below="#+id/header"
</RelativeLayout>
<RelativeLayout> --> for footer
android:layout_alignParentBottom="true"
</RelativeLayout>
</ViewPager>
</RelativeLayout>
3.now make the images for header and footer and set as background.
4.for view pager indicator go Through This Post.just download it and import in your eclipse and set as a lib in your project. how to use circle pager indicator Check My Answer.
and you are done now!!
You can simply use for example a ViewPager as explained in the official documentation because it's not mandatory to have tabs.
http://developer.android.com/training/animation/screen-slide.html
There's a full example available in that link.
If you need also to display dots for your slides, you can take advantage of this library as pointed out by other users: http://viewpagerindicator.com/
Check this library I think this is what you need
and you can customize it as per your needs
https://github.com/pakerfeldt/android-viewflow

Categories

Resources