Using ViewPager with Tabs without actionBar - android

I am using the google example called EffectiveNavigation to create a ViewPager with tabs. The problem is that in the manifest, for my main activity, I have set
android:theme="#android:style/Theme.Holo.NoActionBar.Fullscreen"
So that my app has no actionBar. Therefore I am getting a NullPointerException at
final ActionBar actionBar = getActionBar();//null from getActionBar()
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);//NullPointerException
Now, all I want is to be able to create a simple ViewPager with tabs. That's it. Nothing grand. I am using the google example because it is what I found.
Basically in the google example, they are using the statusBar to hold the tabs. How else might I hold the tabs? Anything less than a good example or instruction on how to modify the goggle example is not going to be much help as I don't know much about ViewPagers. The link to the google example is http://developer.android.com/training/implementing-navigation/lateral.html
A simple workaround the statusBar might be enough.

Using the ActionBar to hold the tabs is actually deprecated because, even if your app does have action bar, it may lead to NullPointerException. Good news are the TabLayout from the Design package brings you the possibility to easily create a ViewPager with tabs. Just write this in the XML of your Activity (or Fragment):
<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.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
And once you have your Adapter for the ViewPager, include this code in the onCreate method of your Activity (or Fragment) after the setContentView method:
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
Hope it helps ;)

I found a fix. Basically in onCreate, before setContentView, I call
requestWindowFeature(Window.FEATURE_ACTION_BAR);
Then everything works fine.

To anyone else arriving here finding that #learner's solution doesn't work for them, take a look at http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/

Related

Put ViewPager in fragment of a menu drawer Android

I searched a solution everywhere but I didnt find anything, so I need one more time your help.
I have a Navigation drawer who works perfectly
Navigation drawer
I have also a ViewPager, that I have take on the Android developer's website. It works fine also.
Now I would like to put the PageViewer in my "jour" tab for exemple. But the PageViewer is not a "Fragment" but a "ActivityFragment" so I cannot. The help that I can find on the other threads does'nt match to my problem
How Can I do this ?
Thanks a lot for reading me.
Annex:
Tutorial Android : https://developer.android.com/training/animation/screen-slide.html
I dont know if i should create a new thread or not. I have put the code in the main activity but the fragment cover all the activity. screen of the fragment
It is strange because the other fragments work.
main Activity
TestSlideFragment fragment = new TestSlideFragment();
fragmentTransaction.replace(R.id.RelativeLayout_for_Fragment, fragment);
fragmentTransaction.commit();
layout of the fragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="oytoch.iut_info.test.TestSlideFragment">
<!-- TODO: Update blank fragment layout -->
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/page"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Open your Jour's xml and add the ViewPager Tag.
Then open Jour's java and bind an object of ViewPager with the xml. Add the adapter to the viewpager object and you are done.

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.

Design support library Tablayout Sliding abnormal

I'm learning to use Tablayout,but I found that the Tablayout can not slide normal.The tab slides with my gesture.The question is that
When I stop gesture, it will stop immediately, and does not automatically slide to the next tab.
the layout:
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
The code:
SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter
(getSupportFragmentManager(), this);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
When I had this happen to me, it was because either 1) My adapter didn't properly create a fragment for that page, or 2) The fragment didn't properly create a view for the page.
Use a tool like Android Device Monitor to check the view on the viewpager and see if it is there as you expect, and that it is not zero width.

Android TabLayout with active tab always at center, just like in Play Newsstand app

In android.support.design.widget.TabLayout, how to make the active tab always appear at the center, just like in Play Newsstand app, as shown below.
The first and last tab should also appear at the center.
I tried using padding on the TabLayout. It is not working. This is the code that I wrote:
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tabIndicatorColor="#color/yellow"
app:tabSelectedTextColor="#android:color/white"
app:tabTextColor="#EEE"
app:tabMode="scrollable"
android:gravity="bottom"
android:clipToPadding="false"
android:paddingLeft="90dp"
android:paddingRight="90dp"
/>
In this case, the tabIndicator will also shift 90dp from left. It should remain at the center independent of the padding.
Checkout this solution: https://stackoverflow.com/a/36886331/651770
Could be solved by tabContentStart, but if you want to center both sides of Tablayout, you need to extend this class and set the padding by hand.
Maybe a little late, but you can try using this library :
https://github.com/jpardogo/PagerSlidingTabStrip
This can be done as they said :
Include the following dependency in your build.gradle file.
compile 'com.jpardogo.materialtabstrip:library:1.1.1'
Or add the library as a project. I tried to send a pull request, but looks like the original developer doesn't maintain it anymore.
Include the PagerSlidingTabStrip widget in your layout. This should usually be placed above the ViewPager it represents.
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
In your onCreate method (or onCreateView for a fragment), bind the widget to the ViewPager:
// Initialize the ViewPager and set an adapter
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new TestAdapter(getSupportFragmentManager()));
// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
Hope this help.

How to implement a tabbed viewpager without actionbar in android

I want to implement a tabbed view pager where the tabs are not part of the action bar.
What is the correct way of doing this since it doesn't seem supported by standard.
So far I have been using the following approach and it seems like it's been so much more effort than it should be that it bothers me:
Create horizontally oriented linear layout to act as tab bar view
Create individual tab views to populate tab bar along with styling
Create custom ongesturelistener for left and right swipe to go between tabs
Create animations for sliding-in/out upon swiping (fragment transactions)
Create animations for the tab bar upon swiping
This seems like it's way too much work when there is a viewpager and tabbar built in. It seems though this "proper" approach is limited to only being used in conjunction with the actionbar. Can anyone provide a simple example of what I am trying to achieve? (see following picture for clarity).
You don't need to build a ViewPager bundled with an ActionBar, they are both independent views.
You can simply hide the ActionBar on that activity doing something like:
ActionBar mActionBar = activity.getActionBar();
mActionBar.hide();
Although it's an ugly solution you can do that to test your code. I would suggest either add a Theme to that specific Activity without an ActionBar (something like Theme.Material.NoActionBar) - not sure it's the correct name of the theme, but you get the idea.
Not sure I answered your question accordingly, but if not please show us some code.
You just add a viewpager with a pager tab strip. Plenty examples out there.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm = getSupportFragmentManager();
fragmentPagerAdapter = new MyTabsPagerAdapter(
fm, tabsArray);
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(fragmentPagerAdapter);
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/default_screen_bg"
android:minWidth="1000dp">
put your image here
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingBottom="4dp"
android:paddingTop="4dp" />
</android.support.v4.view.ViewPager>

Categories

Resources