Does someone know how to use PagerTitleStrip in Android - android

I decided to use a ViewPager in my application and everything is working fine.
I Know that I want to use a PagerTitleStrip in my ViewPager, but I've failed to find any info on how to do it...
The one and only page (sic!!) I found on this class is http://developer.android.com/reference/android/support/v4/view/PagerTitleStrip.html
So it seems I just need to add the PagerTitleStrip within my ViewPager layout, but I don't see anything new on my activity...
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pagerTitleStrip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</android.support.v4.view.ViewPager>
Does anyone know how to use it?
Edited:
Sorry it works when implementing the getPageTitle method in the ViewPagerAdapter... but I don't want any text displayed, just a small cursor to show the position of the current View compared to the previous and next ones...

I'm not sure what causing error in your case but this is how I use it, in your layout you should insert following code in your layout:
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<android.support.v4.view.PagerTitleStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
Then you should add the following code in your PageAdapter:
#Override
public CharSequence getPageTitle (int position) {
return "Your static title";
}
That's pretty it.

I had this issue too where the PagerTitleStrip was not visible. The answers above did not help. The problem was that I followed the example on http://developer.android.com/reference/android/support/v4/view/ViewPager.html.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
The code above shows the view pager but not the PagerTitleStrip.
I changed the code to
protected void onCreate(Bundle savedInstanceData) {
super.onCreate(savedInstanceData);
setTitle(R.string.my_title);
setContentView(R.layout.my_pager);
viewPager = (ViewPager)findViewById(R.id.pager);
res/layout/my_pager.xml:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content">
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
I was also able to be backward compatible to API 8 by removing all the code in the ViewPager (javadoc page) example to manipulate the ActionBar and ActionBar.Tab.

PagerTitleStrip is a non-interactive signal of the current, next, and former pages of a ViewPager. It really is designed to be used as a child view of a ViewPager widget
Step 1. Create layout activity_main.xml
<LinearLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidviewpagerapp.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<android.support.v4.view.ViewPager
android:id="#+id/myviewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="#+id/titlestrip"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.view.ViewPager>
</LinearLayout>
Step 2. Create Activity contains ViewPager this (MainActvity.java)
public class MainActivity extends Activity {
ViewPager viewPager;
MyPagerAdapter myPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.myviewpager);
myPagerAdapter = new MyPagerAdapter();
viewPager.setAdapter(myPagerAdapter);
PagerTitleStrip pagerTitleStrip = (PagerTitleStrip)findViewById(R.id.titlestrip);
}
}
Demo : http://photo-wonder.blogspot.com/2016/09/pagertitlestrip-on-viewpager-android.html

Related

How to enable and disable left and right swipe in viewpager in xamarin android

In my project I have used a viewpager with 3 pages in it. Now in the first page I have a next button. So first time when I am in first page, we should not allow swipe until next button is clicked. When next button is clicked we can come back to first page by swiping. So we should allow swipe from page 2 to page 1. Similarly i have a next button in page2. So there also first time when i am in page2, using swipe it should not allow to go to page 3. Only after clicking on next button in page2, then it will go to page3. From page 3 it should allow swipe to go to page 2 and page1. How can I achieve this. I am trying from past two weeks using some custom view pager classes available in internet tutorials but none of them worked for me as per the requirement.
My Code:
public class PagerFragment : Android.Support.V4.App.Fragment
{
public static ViewPager pager;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.pager_layout, container, false);
pager = view.FindViewById<ViewPager>(Resource.Id.viewPager);
TabLayout tabLayout = view.FindViewById<TabLayout>(Resource.Id.tab_layout);
pager.Adapter = new PagerAdapter(ChildFragmentManager);
tabLayout.SetupWithViewPager(pager, true):
return view;
}
pagerlayout.axml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_marginTop="60dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorAccent" />
<LinearLayout
android:id="#+id/mainLinearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="vertical">
<LinearLayout
android:id="#+id/lin_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="14dp"
android:text="title"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="12sp" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabBackground="#drawable/tab_selector"
app:tabIndicatorHeight="0dp" />
</LinearLayout>
</LinearLayout>
Please help me on this. Will be really thankful

No view found for id for fragment, even that the FrameLayout is a child of the main layout

I've created code so that Fragment is added dynamically when I tap on a menu button. However I get the error:
No view found for id 0x7f0d009a (tech.glasgowneuro.attysecg:id/fragment_container2) for fragment XYPlotFragment{a75aa2e #0 id=0x7f0d009a}
I've been through all the other posts here and the std answer is that the Frame layout needs to be child of the main layout. I think I've done that right. Still no id is found.
activity_plot_window.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<tech.glasgowneuro.attysecg.InfoView
android:id="#+id/infoview"
title="Plot Window"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
<tech.glasgowneuro.attysecg.RealtimePlotView
android:id="#+id/realtimeplotview"
title="Plot Window"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
</RelativeLayout>
<FrameLayout
android:id="#+id/fragment_container2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout><!-- </LinearLayout> -->
main JAVA code of the Activity:
public class AttysECG extends AppCompatActivity {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
setContentView(R.layout.activity_plot_window);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
....
}
...
case R.id.showplot:
// Create a new Fragment to be placed in the activity layout
XYPlotFragment plotFragment = new XYPlotFragment();
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container2, plotFragment).commit();
The question is why do I get this exception? If I do a findViewById(fragment_container2) then the result is null.
How can I get a valid id for the fragment_container2?
At this point the view is certainly inflated because I trigger this crash long after the app has been started.
I've figured it out. Android studio created two "activity_plot_window.xml" files. One has the suffix _v21 so that there are two versions for the older APIs and the newer ones. It's explained here:
https://developer.android.com/training/material/compatibility.html
I've added the Framelayout to the _v21 file and now it works. See the screenshot. It's very easy to oversee when not expanded (just a grey (2)).
You need to find it through FragmentManager:
getSupportFragmentManager().findFragmentById(R.id.fragment_container2);

Fragment getting overlapped in ViewPager

I am getting overlapped Fragments in PageViewer of Android .
PFB my layout for PageViewer
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="900dp"
android:layout_alignParentBottom="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs" >
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
</TabWidget>
</RelativeLayout>
</TabHost>
I am calling it via my First Fragment Say FragmentA of first Tab . I need to replace it with Fragment A1
Calling this from the button click of list from BaseAdapter
Fragment duedateFrag = new FieldVisitFragment();
FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
ft.replace(android.R.id.tabcontent, duedateFrag);
ft.commit();
My Fragment gets called but its overlapped on viewpager .
The tabs runs from below the new Fragment . kindly help
On the hindsight , I think somewhere Nested Fragment or Child Fragment should come into play . Please guide.
I have used ViewPager, extending from PagerAdapter. I think you're not referencing the correct layout, you're using id/tabcontent. I can show an example of what I did, and I think you can follow the same.
In the Fragment xml:
<LinearLayout
...
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#android:color/white" />
...
</LinearLayout>
In Fragment code :
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
In the ViewPager code,
#Override
public Object instantiateItem(ViewGroup container, int position) {
...
View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item, container, false);
...
return view
}
Note: I did not use FragmentTransaction class for displaying the fragment or the tab view.
Keep us posted...

JakeWhartons View pager indicator does not display dots

I am trying to implement the view pager indicator. This is the code in my onCreate method. I have followed the instructions on this page to implement the library and yet nothing is being displayed as I flip through the pages.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_view_tablet);
Bitmap backgroundPic = mainPicBitmap("image_uri_from_db");
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
Drawable backgroundDrawable = new BitmapDrawable(getResources(),backgroundPic);
mViewPager.setBackground(backgroundDrawable);
mViewPager.setAdapter(mSectionsPagerAdapter);
CirclePageIndicator titleIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
titleIndicator.setViewPager(mViewPager);
db.close();
}
This part CirclePageIndicator titleIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
titleIndicator.setViewPager(mViewPager); is supposed to make it work, but nothing displays. I checked the sample application and it is used the same way. What am I doing wrong? Any suggestions appreciated
My layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="400sp"
android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res/co.za.datasolve.acollect">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewCategoryTabletActivity" >
</android.support.v4.view.ViewPager>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:padding="10dip"
android:layout_height="20sp"
android:layout_width="fill_parent"
/>
</LinearLayout>
You use a vertical LinearLayout and you've set the height of the ViewPager(the first child of the LinearLayout) to match_parent which will make it take the whole height of the parent pushing the indicator outside. Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="400sp"
android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res/co.za.datasolve.acollect">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
tools:context=".ViewCategoryTabletActivity" >
</android.support.v4.view.ViewPager>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:padding="10dip"
android:layout_height="20dp"
android:layout_width="fill_parent"
/>
</LinearLayout>

Android ViewPager with LinearLayout at the button

I have an Activity, that uses a ViewPager to page through two ListFragments -- this works great.
I am attemtpting to add some type of layout at the bottom that has an EditText, a Button, and a TextView. I want this to be fixed at the bottom of the screen, not scroll with the ListFragment.
My view looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/white">
<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" />
</LinearLayout>
But because I use this in my onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
}
This view isn't even being read. The problem is I'm not sure how to use a ViewPager AND specify a layout, allowing me to add such a layout at the bottom.. can anyone help me out?
Looks like you setting your content view to mViewPager, which isn't using the layout.
Rather than create new ViewPager object, use the layout xml you've crated. If it's called activity_main.xml set it via setContentView(R.layout.activity_main);
Maybe something like this?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/white">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/pager"
android:orientation="horizontal"
>
<Button... your button here />
<TextView ... your text here />
</LinearLayout>
</RelativeLayout >
Figured it out, just use the setConentView as normal, and reference the ViewPager by id.. not sure why I had a problem with this..?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.application);
mViewPager = (ViewPager) findViewById(R.id.pager);
mTabsAdapter = new OURSVPTabsAdapter(this, mViewPager);
...
}

Categories

Resources