Android ViewPager with LinearLayout at the button - android

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);
...
}

Related

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);

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>

Using Tabs with Views

I have a little problem using Tabs with Views.
First I just copied the sample code where Tabs are used with activitys:
My LayoutFile looks like this:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
And this is my Java-code:
public class MyActivity extends TabActivity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState)
setContentView(R.layout.main);
TabHost tH = getTabHost();
Indent intent = new Intent().setClass(this, AnotherActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
//TextView Test = new TextView(this);
//Test.setText("test");
tH.addTab(tH.newTabSpec("t1").setIndicator("Tab1").setContent(intent));
tH.setCurrentTab(0);
}
}
And this works as expected.
But when I uncomment the TextView-lines and call setContent(Test.getId()) instead of setContent(intent), the app crashes.
I also tried to create a textview in the layoutfile, and call setContent(R.id.test),
that also makes it crash.
So this is one problem.
The seccond point is. I do not want to use activitys, because i want to be able to call methods on those classes, which shall represent the Tab-content.
So my original idea is, to derive some classes from view. 1 for each tab, and pass their ids. But therefor the codesample above needs to work first.
greetings Uzaku
I know you said you tried a TextView in the layout file but this should work...
Change the FrameLayout section as follows...
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<TextView
android:id="#+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="TEST" />
</FrameLayout>
Then in your code do the following...
tH.addTab(tH.newTabSpec("t1").setIndicator("Tab1").setContent(R.id.test));

Does someone know how to use PagerTitleStrip in 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

Android Tab Like Footer

I want to reuse my tabs like footer for each activities in my application.
I'm using this code
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#android:id/tabs" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</TabHost>
and I want to add components like ListView, Button etc on top of my tabs for each layout. How can I manage this?
I've done something similar by extending a common base activity, in which I've overriden the method setContentView.
abstract public class BaseActivity extends Activity {
#Override
public void setContentView(View view) {
// get master
LayoutInflater inflater = LayoutInflater.from(this);
// this is the master view with a container and the footer, you can
// as well add the header
RelativeLayout rlMasterView = (RelativeLayout) inflater.inflate(R.layout.master, null);
rlMasterView.addView(view);
super.setContentView(rlMasterView);
}
}
The setContentView creates the footer and attaches it to the view that I'm setting in each activity.
I could as well just use the include tag in each layout.
<include src="footer" />

Categories

Resources