I've tried multiple ways of adding a footer and a header in activity_main.xml which has a viewpager inside it. So far when I add a header above the viewpager, it's shown in final result but the footer isn't.
Here is the xml code
<LinearLayout 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:background="#drawable/bg50"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF0000"
android:orientation="vertical">
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/colorAccent"
app:tabTextColor="#FFF" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF0000"
android:orientation="vertical">
</LinearLayout>
And here is the activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new LoginFragment(), "Login");
adapter.addFragment(new RegisterFragment(), "Register");
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
sreenshot of the result
So the question is ,how to display content below the view pager.
Change your viewpager layout height to wrap_content.
Or just change your root layout to <RelativeLayout> and set add this tag to your view pager.
Like so:
<RelativeLayout>
...
<ViewPager
...
android:layout_height="match_parent"
android:layout_above="#id/your_footer_id"/>
...
</RelativeLayout>
If you want a static footer below ViewPager you can use layout below.
<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:background="#FFFFFF"
android:orientation="vertical">
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF0000"
android:orientation="vertical">
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:background=""
android:minHeight="?attr/actionBarSize"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/colorAccent"
app:tabTextColor="#FFF" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/footer"
android:layout_below="#+id/tabs" />
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#FF0000"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
Set to your viewpager:
android:layout_height="0dp"
android:layout_weight="1"
This will make it fill all available space in LinearLayout after other children are laid out.
Related
I am trying to show 4 fragments in one only ViewPager but when I scroll between Fragments the second fragment is not showing at all. It only shows Fragment 1,3 and 4 correctly.
I have checked a lot of other answers about stuff like this and they suggest to use
getChildFragmentManager()
instead of
getSupportFragmentManager()
and seems to work but, I can't use that because I'm using androidx.
This is my code in my MainActivity class:
public class MainMenu extends AppCompatActivity {
private ViewPager mViewPager;
private SectionPagerAdapter sectionPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
mViewPager = findViewById(R.id.menu_viewPager);
sectionPagerAdapter = new SectionPagerAdapter(getSupportFragmentManager(),this, getFragmentList());
bottomNavigationView.setOnNavigationItemSelectedListener(this);
mViewPager.setAdapter(sectionPagerAdapter);
}
private List<Fragment> getFragmentList() {
List<Fragment> fragmentList = new ArrayList<>();
fragmentList.add(new HomeMenuFragment(this));
fragmentList.add(new RestaurantsListFragment(this));
fragmentList.add(new OrdersMenuFragment(this));
fragmentList.add(new AccountConfigurationFragment(this));
return fragmentList;
}
And this is my Adapter class for ViewPager:
public class SectionPagerAdapter extends FragmentStatePagerAdapter {
Context context;
private List<Fragment> fragments = new ArrayList<>();
public SectionPagerAdapter(FragmentManager fm, Context context, List<Fragment> fragments) {
super(fm);
this.context=context;
this.fragments=fragments;
}
#NonNull
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
case 3:
return "SECTION 4";
}
return null;
}
}
This is the my XML file for the first Fragment (HomeMenuFragment):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="192dp"
android:background="#color/grayLight2"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_collapseMode="parallax"
android:background="#color/grayLight2"
>
<androidx.viewpager.widget.ViewPager
android:id="#+id/banner_view_pager"
android:layout_width="match_parent"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="10dp"
android:layout_height="150dp"
android:overScrollMode="never"/>
<LinearLayout
android:id="#+id/slider_dots"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"/>
</LinearLayout>
<androidx.appcompat.widget.Toolbar
android:id="#+id/anim_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/food_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>
</LinearLayout>
I realize that what is causing this problem is what I have in my first Fragment's XML file:
CoordinatorLayout
AppBarLayout
CollapsingToolbarLayout
Toolbar
If I remove this elements the ViewPager displays all fragments correctly!
Any idea of what could I be doing wrong or what am I missing?
Thanks in advance!
In your SectionPagerAdapter class add break in the case statements and try
I have android project that have three tabs. in one of 3 tabs has a ListView of different activities in which if that item clicked should come direct to assigned fragment
those tabs are TABA TABB TABC on TABB that has list of activities in which if someone click should direct to another fragment and activities containing these tabs should not be shown
I tried to call those fragmenttransaction inside istView.setOnItemClickListener but no any thing change
In tabs TABB
here are the code
public class Activities extends Fragment implements View.OnClickListener{
private SectionsPagerAdapter sectionsPagerAdapter;
public Activities() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_activities, container, false);
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for(int i = 0; i<7; i++){
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("listview_title", ListTitle[i]);
hm.put("listview_image", Integer.toString(ListImage[i]));
aList.add(hm);
}
String [] from = {"listview_image", "listview_title"};
int [] to = {R.id.listview_image, R.id.listview_item_title};
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(),aList,
R.layout.activity_listview, from, to);
ListView listView = (ListView)rootView.findViewById(R.id.list_view);
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
//R.id.container is on activity_main.xml
transaction.replace(R.id.container, new FileDownloadFragment());
transaction.commit();
}
});
return rootView;
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_content1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<!--!app:layout_scrollFlags="scroll|enterAlways"-->
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="#string/app_name">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed">
<android.support.design.widget.TabItem
android:id="#+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_1" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_2" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_3" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<!-- <android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />-->
</LinearLayout>
fragment_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context="com.example.jeliel.easycontrol.activities.Activities">
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I'm expecting this when item clicked on ListView should bring me on fragment called FileDownloadFragment()
I am using JakeWharton's ViewPagerIndicator on my landscape app but I think I am messing up with the layout. The ViewPagerIndicator does not show up. I just started learning Android Development so any help is appreciated!
My guess is that it has to do with my Linear Layout...should it be relative? Am I covering up the pager indicator? I know that if I move the indicator code above the view pager, I see the indicator but everything else is white. My fragment's layout is a relative layout with height and width matching the parent.
My app is an activity with fragments.
Edit: I set the indicator height to wrap content but now it floats on the top of the screen. How do I make it go down?
My_Activity.java
public class MyActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
CirclePageIndicator circleInd = (CirclePageIndicator) findViewById(R.id.circles);
circleInd.setViewPager(pager);
circleInd.setStrokeColor(0xAA000000);
//circleInd.setCurrentItem(mAdapter.getCount() - 1);
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0: return FirstFragment.newInstance("First");
case 1: return SecondFragment.newInstance("Second");
default: return FirstFragment.newInstance("First");
}
}
#Override
public int getCount() {
return 2;
}
}
}
and the XML
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.frontrowqa.myfirstapplication.MyActivity">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circles"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_gravity="bottom" />
</LinearLayout>
And my fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<EditText
android:id="#+id/PasscodeText"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_width="fill_parent"
android:hint="Passcode"
android:backgroundTint="#android:color/black" />
<EditText
android:id="#+id/ipAddressText"
android:layout_below="#id/PasscodeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="IP Address"
android:backgroundTint="#android:color/black" />
<EditText
android:id="#+id/intercomText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ipAddressText"
android:hint="Intercom Event Number"
android:backgroundTint="#android:color/black"/>
<TextView
android:id="#+id/tvFragFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="26dp"
android:text="TextView" />
</RelativeLayout>
Set your activity xml like this:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="10dp" />
</LinearLayout>
I am currently working on adding tabs into my activity. For the same, I am using the Google SlidingTabLayout and SlidingTabStrip.
My fragment xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" > >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textViewTab" />
</RelativeLayout>
My contents xml looks like this:
<?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"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.musicisfun.allaboutfootball.MainActivity"
tools:showIn="#layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout
android:layout_width="match_parent"
android:id="#+id/tabs"
android:layout_height="wrap_content">
</com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout>
<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="0dp"
/>
</LinearLayout>
</RelativeLayout>
and my code looks like this:
public static class TabFragment extends Fragment{
public static TabFragment getInstance(int position){
TabFragment tabFragment = new TabFragment();
Bundle bundle = new Bundle();
bundle.putInt("site",position);
tabFragment.setArguments(bundle);
return tabFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_tab,container,false);
TextView textViewTab = (TextView) layout.findViewById(R.id.textViewTab);
Bundle bundle = getArguments();
if (bundle!=null){
textViewTab.setText("Current Tab is" + bundle.getInt("site"));
}
return layout;
}
}
My fragment adaptor looks like this:
class TabPagerAdaptor extends FragmentPagerAdapter{
String[] tab_names;
public TabPagerAdaptor(FragmentManager fm) {
super(fm);
tab_names=getResources().getStringArray(R.array.feed_names);
}
#Override
public Fragment getItem(int position) {
TabFragment tabFragment = TabFragment.getInstance(position);
return tabFragment;
}
#Override
public CharSequence getPageTitle(int position) {
return tab_names[position];
}
#Override
public int getCount() {
return 2;
}
}
and this is how i am invoking the tabs:
mViewPager= (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new TabPagerAdaptor(getSupportFragmentManager()));
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setViewPager(mViewPager);
But when I run the code, I can't find the textview being shown even though two tabs are working fine.
You cannot view your textView because the height of the ViewPager is 0dp in your xml -
<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="0dp"
/>
you have to change that to either wrap_content or match_parent-
<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"
/>
Also, Your LinearLayout's orientation is horizontal and the width of SlidingTabLayout is match_parent. SlidingTabLayout is occupying whole screen width leaving no room to display Viewpager.
Your LinearLayout's orientation should be vertical not horizontal
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout
android:layout_width="match_parent"
android:id="#+id/tabs"
android:layout_height="wrap_content">
</com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout>
<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>
I have implemented a TabLayout which has a few tabs & one of the tabs has a table & when a user clicks on a row in the table I need it to get rid of the tab layout, except for the toolbar & open up a new Fragment.
I've tried a few different ways but nothing seems to work, if anyone has any ideas, it'll will be much appreciated!
Thank you!
Cheers!
This is the activity_main.xml
<RelativeLayout
android:id="#+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/tab_layout"/>
</RelativeLayout>
& this is the MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
& this is tab1.xml
<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"
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:background="#F0F0F0" >
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center" >
<TableLayout
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
</TableLayout>
<RelativeLayout
android:id="#+id/loadingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
</RelativeLayout>
</FrameLayout>
& this is Tab1.java
public class TopStories extends Fragment {
A lot of code goes here............
public void createTable() {
TableLayout table = (TableLayout) getActivity().findViewById(R.id.table);
table.setBackgroundColor(Color.rgb(240, 240, 240));
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
table.removeAllViews();
TableRow row = new TableRow(getActivity());
row .setGravity(Gravity.CENTER);
row .setPadding(0, 10, 0, 10);
row .setBackgroundColor(Color.WHITE);
row .setClickable(true);
TableRow.LayoutParams params = new TableRow.LayoutParams();
row .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
This is where I need to start up the new fragment (NewFragment.Java).
}
});
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_topstories, container, false);
}
}
NewFragment.java
public class NewFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.newfragment, container, false);
}
}
newfragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.04" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="18dp"/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_marginTop="87dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</LinearLayout>
I think you need to give the fragment somewhere to replace. Here is the XML from my app, and when I swap fragments, they post to flcontent.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk
xmlns:app="http://schemas.android.com/apk/res
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<!-- The main content view -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
</LinearLayout>
<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/nav_header"/>
and the class code
// Insert the fragment by replacing any existing fragment
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.flContent, fragment);
ft.addToBackStack(null);
ft.commit();
Alright so I think that I figured out a really cheap & a cheeky way to do this & probably not very healthy for the app but it works perfect!
Thank you very much for the help Brian! Appreciate it! Maybe you can do this for the backstack problem of your app as well, don't know if it will work or the right thing that you need to do though.
Cheers!
Added a FrameLayout to my main_activity.xml
<RelativeLayout
android:id="#+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/tab_layout"/>
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</RelativeLayout>
Changed my newfragment.xml to this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fullStoryLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/fullStoryRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.04" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="80dp"/>
<TextView
android:id="#+id/fullStorytitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/fullStory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/image"
android:layout_marginTop="50dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</LinearLayout>
& added this to the onClick event of the table row
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
getActivity().findViewById(R.id.tab_layout).setVisibility(View.GONE);
getActivity().findViewById(R.id.pager).setVisibility(View.GONE);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("New Fragment");
Fragment fragment = null;
Class fragmentClass = NewFragment.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.flContent, fragment);
fragmentTransaction.commit();
}
});
& to go back to the main TabLayout from the NewFragment, I added this at MainActivity.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
if (getSupportActionBar().getTitle() != "Main Activity"){
onBackPressed();
}
else {
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed(){
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setTitle("Main Activity");
findViewById(R.id.fullStoryLinearLayout).setVisibility(View.GONE);
findViewById(R.id.tab_layout).setVisibility(View.VISIBLE);
findViewById(R.id.pager).setVisibility(View.VISIBLE);
}