How do i put a tabbed layout in my bottom navigation view? - android

Ive tried using view pager code below but code crashes. I essentially need a tabbed activity in 5 of the bottom navigation. Can you also tell me how i would use the tabbed layout code in the fragments. Thankyou
public class client_interface_start extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client_interface_start);
BottomNavigationView bottomNav = findViewById(R.id.client_bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navlistener1);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.client_viewpager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.client_tablayout_listings);
tabs.setupWithViewPager(viewPager);SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.client_viewpager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.client_tablayout_listings);
tabs.setupWithViewPager(viewPager);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_containerclient,
new fragment_client_listings()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navlistener1 =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_clientlistings:
selectedFragment = new fragment_client_listings();
break;
case R.id.nav_clientrequest:
selectedFragment = new fragment_client_request();
break;
case R.id.nav_clientorders:
selectedFragment = new fragment_client_orders();
break;
case R.id.nav_clientprofile:
selectedFragment = new fragment_client_profile();
break;
case R.id.nav_clientnotifications:
selectedFragment = new fragment_client_notifications();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_containerclient,
selectedFragment).commit();
return true;
}
};
}
Here is the XML File, I have 3 tabs in it, listings favorites and appointments. I want to be able to click through them all and show different layout fragments for each TabItem.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout >
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/client_appbar_listings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.google.android.material.tabs.TabLayout
android:id="#+id/client_tablayout_listings"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:id="#+id/client_listings_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Listings" />
<com.google.android.material.tabs.TabItem
android:id="#+id/client_favorites_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Favorites" />
<com.google.android.material.tabs.TabItem
android:id="#+id/client_appointments_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Appointments" />
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="#+id/alerts_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/client_bottom_navigation"
android:layout_marginTop="50dp" >
<Button
android:id="#+id/placement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Placement profile " />
<androidx.viewpager.widget.ViewPager
android:id="#+id/client_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"/>
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Client Listings"/>
</RelativeLayout>

Related

Tabs in TabLayout aren't available for click

I've got simple tabLayout within Fragment file which is CoordinatorLayout.
It works good, when user swipes, different Fragments are displayed.
The problem is, tabs aren't respond for clicking, user cannot select particular tab,it has oportunity to swipe only
Below I launch files containing XML, ViewPager2 adapter, and Fragment.java in which I've implemented mechanism for swiping.
For global, in MainActivity I've implemented toolbar, that's why in Fragment's TabLayout I'm using margin top 50dp.
I also lunch its code.
<androidx.coordinatorlayout.widget.CoordinatorLayout 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/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainFragment">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/Widget.MyApp.TabLayout"
android:layout_marginTop="56dp"
android:layout_below="#+id/toolbar"
app:tabMode="fixed" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end|right">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/minusFab"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_remove_circle_outline_24px"
app:layout_constraintEnd_toEndOf="#id/plusFab"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/plusFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_marginTop="20dp"
android:layout_marginBottom="24dp"
android:src="#drawable/ic_add_circle_outline_24px"
app:layout_constraintBottom_toTopOf="#+id/mainFab"
app:layout_constraintEnd_toEndOf="#+id/mainFab"
app:layout_constraintTop_toBottomOf="#+id/minusFab" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/mainFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_expand_less_24px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I've configured all mechanisms responsible for tabs logic:
public class ViewPagerAdapter extends FragmentStateAdapter {
public ViewPagerAdapter(#NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
#NonNull
#Override
public Fragment createFragment(int position) {
switch (position)
{
case 0: return new FirstFragment();
case 1: return new SecondFragment();
case 2: return new ThirdFragment();
default:
return null;
}
}
#Override
public int getItemCount() {
return 3;
}
}
And implementation in Fragment.Java
public class MainFragment extends Fragment {
public MainFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
ViewPager2 viewPager2 = view.findViewById(R.id.viewPager);
viewPager2.setAdapter(new ViewPagerAdapter(getActivity()));
TabLayout tabLayout = view.findViewById(R.id.tab_layout);
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
#Override
public void onConfigureTab(#NonNull TabLayout.Tab tab, int position) {
switch (position){
case 0: tab.setText("First");
break;
case 1: tab.setText("Second");
break;
case 2: tab.setText("Third");
break;
}
}
}
);
tabLayoutMediator.attach();
// Inflate the layout for this fragment
return view;
}
}
MainActivity Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.FragmentsWithTabs.AppBarOverlay"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/Theme.FragmentsWithTabs.PopupOverlay">
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="#+id/color_mode_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|end"
android:text="Switch here"
android:textAppearance="#style/TextAppearance.AppCompat.Small" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="#+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph"
tools:layout_editor_absoluteX="127dp"
tools:layout_editor_absoluteY="297dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
You have to add listener on tablayout to listen tab select event. Like,
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
//here you can write code to change page of viewpager based on tab id.
// like, viewpager.setCurrentItem(0);
}
});
Finally I've came with an answer. In this situation, ViewPager2 was expanded on all screen hence overlapping clicks on TabLayout :)
So solution was simple, now MainFragment's ViewPager2 code looks like follows:
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="629dp"
android:layout_gravity="bottom"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />

How to program FragmentB inside FragmentA?

I'm making an application as a school project and I'm having trouble in programming a certain FragmentB inside FragmentA. Trouble, more so, I don't know how to even if I had an extensive search done.
FragmentA to MainActivity works properly (or so, because doesn't give out any errors). I used SwitchCase in the MainActivity.java and it works.
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
selectedFragment = HomeScreenFragment.newInstance();
break;
case R.id.navigation_profile:
selectedFragment = ProfileScreenFragment.newInstance();
break;
case R.id.navigation_notifications:
selectedFragment = NotifScreenFragment.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
return true;
}
});
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, HomeScreenFragment.newInstance());
transaction.commit();
}
}
ProfileScreenFragment.java (this is what I want to have a FragmentB)
public class ProfileScreenFragment extends Fragment {
public static ProfileScreenFragment newInstance() {
ProfileScreenFragment fragment = new ProfileScreenFragment ();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_profile_screen2, container, false);
}
}
fragment_profile_screen2.xml (the frame_layout is where I want to put the FragmentB)
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.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginRight="15dp"
android:background="#color/colorPrimary"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:padding="7dp"
app:srcCompat="#drawable/logo_mdpi" />
<TextView
android:id="#+id/textView"
android:layout_width="313dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:fontFamily="sans-serif-black"
android:gravity="center"
android:text="Profile"
android:textColor="#android:color/white"
android:textSize="25sp" />
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="#drawable/icon_more2" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:tabMode="fixed"
app:tabTextColor="#android:color/white">
<android.support.design.widget.TabItem
android:id="#+id/profile_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile" />
<android.support.design.widget.TabItem
android:id="#+id/profile_posts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Posts" />
<android.support.design.widget.TabItem
android:id="#+id/profile_uploads"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Uploads" />
<android.support.design.widget.TabItem
android:id="#+id/profile_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Likes" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#android:drawable/ic_input_add"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:clickable="true" />
</android.support.design.widget.CoordinatorLayout>
How or what code should I be writing for FragmentB be put inside the ProfileScreenFragment.java?
Is it also possible to use SwitchCase in ProfileScreenFragment.java for the Tabs?
Thank you so much. If ever there are clarifications needed in the codes I used, please don't hesitate to ask. I'm still learning the basics of it all so I'm 50/50 on the codes.
Add this snippet in ProfileScreenFragment.
#Override
protected void onViewCreated(View view, Bundle savedInstanceState) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, FragmentB.newInstance());
transaction.commit();
}
For the tab layout, you can add a tab change listener
TabLayout tabLayout = view.findViewById(R.id.tabs);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0: // Handle 1st tab item
break;
case 1: // Handle 2nd tab item
break;
case 2: // Handle 3rd tab item
break;
case 3: // Handle 4th tab item
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});

navigation drawer does not started

I have my navigation drawer and i need to open it but i can't>>
Here is my drawer_layout.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="#layout/activity_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#android:color/white"
app:menu="#menu/drawer_view" />
and this is my toolbar in activity_display.xml:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/myToolBar"
android:background="#color/top_bar_backgroung">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dots_vertical"
android:textSize="40sp"
android:layout_marginRight="10dp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/openDrawer"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:src="#drawable/logo"
android:scaleType="centerInside"/>
</android.support.v7.widget.Toolbar>
and here is how i open my drawer :
private DrawerLayout mDrawer;
private Toolbar toolbar;
private NavigationView nvDrawer;
toolbar = (Toolbar) findViewById(R.id.myToolBar);
setSupportActionBar(toolbar);
// Find our drawer view
View viewDrawer = getLayoutInflater().inflate(R.layout.drawer_layout, null);
mDrawer = (DrawerLayout) viewDrawer.findViewById(R.id.drawer_layout);
nvDrawer = (NavigationView) viewDrawer.findViewById(R.id.nvView);
setupDrawerContent(nvDrawer);
//gridView = (GridView) findViewById(R.id.displayGridView);
drawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "onClick: " );
mDrawer.openDrawer(GravityCompat.END);
}
});
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the fragment to show based on nav item clicked
android.support.v4.app.Fragment fragment1 = null;
Class fragmentClass = null;
switch(menuItem.getItemId()) {
case R.id.nav_home_fragment:
Toast.makeText(this, "home", Toast.LENGTH_SHORT).show();
//fragmentClass = FirstFragment.class;
break;
case R.id.nav_personal_fragment:
//fragmentClass = SecondFragment.class;
break;
case R.id.nav_avilableCareer_fragment:
//fragmentClass = ThirdFragment.class;
break;
case R.id.nav_declareCareer_fragment:
//fragmentClass = FourdFragment.class;
break;
default:
//fragmentClass = FirstFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment1).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
mDrawer.closeDrawers();
}
i don't know why i can't start my navigation draw..
i can't see it on screen.
write end instead of right in xml file
android:layout_gravity="right"
to
android:layout_gravity="end"
or alternate solution
use
mDrawer.openDrawer(GravityCompat.RIGHT);
instead of
mDrawer.openDrawer(GravityCompat.END);

Android - TabLayout unclear behavior

I've created two tab layout via TabLayout & ViewPager, but when I transition between the tabs, it seems that I'm always viewing the layout of TAB1 instead of transitioning to TAB2. I can see this as TAB1 has a Spinner in it's layout, while TAB2 does not.
AS you can see I've added Log.w entry in onTabSelected but I never see the output, which strengths the assumption that I'm not really transitioning between tabs, the layout never changes.
What is incorrect in my configuration?
Here is the code:
Main activity onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
dbM = DBManager.getInstance(context);
TaskHashList.Initialize();
itemListAllTasks = dbM.getAllTasks();
TaskHashList.addTaskList(itemListAllTasks);
itemListWaitingTasks = dbM.getSortedTasks(Sorting.fromInteger(Sorting.WAITING.ordinal()));
TaskHashList.addTaskList(itemListWaitingTasks);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("All Tasks"));
tabLayout.addTab(tabLayout.newTab().setText("Waiting"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PageAdapter adapter = new PageAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
Log.w("changed tab", "");
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
adapter.notifyDataSetChanged();
}
My PageAdapter
public class PageAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PageAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
Fragment fragment;
switch (position) {
case 0:
fragment = new AllTasksTabFragment();
case 1:
fragment = new WaitingTasksTabFragment();
default:
fragment = new AllTasksTabFragment();
}
Bundle bundle = new Bundle();
bundle.putInt("position",position);
fragment.setArguments(bundle);
return fragment;
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
TAB1 layout - All Tasks
<?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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="#+id/Main2ActivitylinearLayout3">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:paddingRight="10dp"
android:text=""
android:id="#+id/allt_tab_totalTask"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:id="#+id/Main2ActivitylinearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingRight="8dp"
android:text="Sort:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/sort_array"
android:id="#+id/all_tasks_sortSpinner"
android:gravity="center"
android:textAlignment="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:id="#+id/Main2ActivitylinearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="No Tasks to Display"
android:layout_marginTop="60dp"
android:layout_marginLeft="60dp"
android:id="#+id/alltab_emptylist"
android:gravity="center"
android:layout_centerHorizontal="true"
android:textStyle="bold" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/alltasks_listView"
android:layout_centerHorizontal="true" />
</LinearLayout>
TAB2 layout - Waiting Tasks
<?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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="#+id/Main2ActivitylinearLayout1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:paddingRight="10dp"
android:text=""
android:id="#+id/waitt_tab_totalTask"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:id="#+id/Main2ActivitylinearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="No Tasks to Display"
android:gravity="center"
android:layout_marginTop="60dp"
android:layout_marginLeft="60dp"
android:id="#+id/wait_tabemptylist"
android:layout_centerHorizontal="true"
android:textStyle="bold" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/waitingtasks_listView"
android:layout_centerHorizontal="true" />
</LinearLayout>
Your switch case doesn't have breaks.
#Override
public Fragment getItem(int position) {
Fragment fragment;
switch (position) {
case 0:
fragment = new AllTasksTabFragment();
break; // Add this
case 1:
fragment = new WaitingTasksTabFragment();
break; // Add this
default:
fragment = new AllTasksTabFragment();
}
Bundle bundle = new Bundle();
bundle.putInt("position",position);
fragment.setArguments(bundle);
return fragment;
}
But i cannot assume why your Log.w doesn't work.
Maybe you're setting Log Filter to show errors only...

Android Fragments Not Being replaced

I am new to fragments and running into some trouble with my navigation bar fragments. I have set up the navigation bar fine, when I select a item from the navigation the layout is inflated fine but the old layout doesnt disappear and is left sitting either ontop or underneath then new layout.
The new layout should have the toolbar and navigation drawer fragment but not the tabs and their layout.
Can anyone explain where I have goen wrong?
Main Activity Creating Tabbed layout & Nav itemSelected
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
drawerFragment = (FragmentDrawer) getFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerFragment.setDrawerListener(this);
}
private void setupTabIcons() {
int[] tabIcons = {
R.drawable.ic_tab_list,
R.drawable.ic_tab_shopping,
R.drawable.ic_tab_add,
R.drawable.ic_tab_search
};
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new PossibleRecipes(), "ONE");
adapter.addFrag(new ShoppingList(), "TWO");
adapter.addFrag(new AddRecipe(), "THREE");
adapter.addFrag(new SearchRecipes(), "FOUR");
viewPager.setAdapter(adapter);
}
#Override
public void onDrawerItemSelected(View view, int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
//fragment = new HomeFragment();
//title = getString(R.string.title_home);
//break;
case 1:
fragment = new FavouritesFragment();
title = getString(R.string.title_favourites);
break;
case 2:
fragment = new HelpFragment();
title = getString(R.string.title_help);
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
Main Activty.XML
<android.support.v4.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout2">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</android.support.design.widget.CoordinatorLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.example.rory.pocketchef.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
One of the Navigation drawer XMLs (HELP.XML)
<RelativeLayout 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="info.androidhive.materialdesign.activity.HomeFragment">
<TextView
android:id="#+id/label"
android:layout_alignParentTop="true"
android:layout_marginTop="100dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="45dp"
android:text="HOME"
android:textStyle="bold"/>
<TextView
android:layout_below="#id/label"
android:layout_centerInParent="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12dp"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="Edit fragment_home.xml to change the appearance" />
</RelativeLayout>
In OnDrawerItemSelected you set fragment = new FavouritesFragment(); or fragment = new HelpFragment();. So below that you should probably do;
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment); //Here was the error. container is a FrameLayout
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
Note that this line fragmentTransaction.replace(R.id.drawer_layout, new FavouritesFragment()); was edited.
Also, note that container_body has a height of 0dp and layout_weight doesn't work in FrameLayout which the CoordinatorLayout extends... The ViewPager is set to fill_parent as well.
I don't understand the purpose of container_body in your UI... What is it supposed to do? Where should it appear?

Categories

Resources