How to program FragmentB inside FragmentA? - android

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

Related

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

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>

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" />

Bottom Navigation and Fragment Tab Layout

I have a tab layout with 3 Fragments, I also have a BottomNavigation. So should I place BottomNavigation in an Activity for 3 Fragments or place a bottom navigation in each Fragment? Because I have a ListView in each Fragment so I think each Fragment should have one BottomNavigation for click event.
Just one BottomNavigation for all fragment.
You can easily use this library and then in YOUR_ACTIVITY_LAYOUT set it like this:
<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">
<FrameLayout
android:layout_above="#id/bottom_navigation"
android:name="com.ali.digikalaapp.Fragment_pager_one"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.ss.bottomnavigation.BottomNavigation
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary">
<com.ss.bottomnavigation.TabItem
android:id="#+id/tab_home"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:tab_text="Home"
app:tab_icon="#drawable/ic_home_white_24dp"
/>
<com.ss.bottomnavigation.TabItem
android:id="#+id/tab_images"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:tab_text="Images"
app:tab_icon="#drawable/ic_image_black_24dp"
/>
<com.ss.bottomnavigation.TabItem
android:id="#+id/tab_camera"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:tab_text="Camera"
app:tab_icon="#drawable/ic_camera_white_24dp"
/>
</com.ss.bottomnavigation.BottomNavigation>
</RelativeLayout>
and if you have 3 Fragment , replace fragment like this in YOUR_ACTIVITY:
public class YOUR_ACTIVITY extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_ACTIVITY_LAYOUT);
BottomNavigation bottomNavigation = (BottomNavigation) findViewById(R.id.bottom_navigation);
bottomNavigation.setOnSelectedItemChangeListener(new OnSelectedItemChangeListener() {
#Override
public void onSelectedItemChanged(int itemId) {
switch (itemId) {
case R.id.tab_home:
Fragment_Home home = new Fragment_Home();
FragmentManager fragment = getSupportFragmentManager();
FragmentTransaction transaction = fragment.beginTransaction();
transaction.replace(R.id.frameLayout, home);
break;
case R.id.tab_images:
Fragment_Image image = new Fragment_Image();
FragmentManager fragment = getSupportFragmentManager();
FragmentTransaction transaction = fragment.beginTransaction();
transaction.replace(R.id.frameLayout, image);
break;
case R.id.tab_camera:
Fragment_Camera camera = new Fragment_Camera();
FragmentManager fragment = getSupportFragmentManager();
FragmentTransaction transaction = fragment.beginTransaction();
transaction.replace(R.id.frameLayout, camera);
break;
}
transaction.commit();
}
}
}
and here is one Fragment example :
public class Fragment_Home extends Fragment {
public Fragment_Home() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
and here is fragment_home 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"
tools:context=".Fragment.Fragment_Home">
<TextView
android:text="Blank Fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>

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 with Overlay

I'm having overlay in my application, I did a search and I found some solutions that apparently were simple, but did not work for me. Probably I have a little confusion because I don't understand why it's not working.
So, in my activity_main.xml file I have a FrameLayout element 'container_body'. In my MainActivity I'm set the content of this xml. In this activity I have a drawer-list with some fragments.
When I click in any fragment I'm seeing overlay.
In MainActivity I'm calling the displayView that is responsible to make the fragments transitions, I'm replacing the container_body element for the content of fragment, but this doesn't working.
Any suggestions, please?
[Images] The first screen
The fragment screen
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new PostView();
title = getString(R.string.title_section1);
break;
case 1:
fragment = new RegionView();
title = getString(R.string.title_section2);
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:contentInsetStart="72dp" />
<FrameLayout
android:id="#+id/container_body"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.25"
android:layout_gravity="top|left|bottom|right">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/tile_bg"
android:orientation="vertical" >
<ListView
android:id="#+id/list_view_messages"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#null"
android:divider="#null"
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true">
</ListView>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/llMsgCompose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal"
android:weightSum="3" >
<EditText
android:id="#+id/inputMsg"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#color/bg_msg_input"
android:textColor="#color/text_msg_input"
android:paddingLeft="6dp"
android:paddingRight="6dp"/>
<Button
android:id="#+id/btnSend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/bg_btn_join"
android:textColor="#color/white"
android:text="#string/btn_send" />
</LinearLayout>
</FrameLayout>
<com.heinrichreimersoftware.materialdrawer.DrawerView
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
Fragment
public class PostView extends Fragment implements CommonPresenter {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_post, null);
return (view);
}

Categories

Resources