Hide toolbar and tablayout in fragments - android

I've been developing some application all went fine, everytime I left the main fragment the toolbar and tablayout would also go, as soon as I was coming back to the main fragment Toolbar and tablayout where waiting for me there.
Recently, I've tested my app on newer android 7.0 and the result is that toolbar and tablayout don't go anywhere, they just stay at the top of any fragment.
I've tried this approach:
View view2 = getActivity().findViewById(R.id.tabs);
View view = getActivity().findViewById(R.id.toolbar);
view2.setVisibility(View.GONE);
view.setVisibility(View.GONE);
Using this method it removes tablayout and toolbar from the top, however when I go back using the back button, it also removes these two from the main fragment.
I've tried using the onResume on my main fragment, but as far as I understand unless the parent mainactivity reloads, no result will be shown in onResume.
My question is, how can I hide toolbar and tablayout on specific nested fragments without hiding it on my main fragment. I've looked through the other questions,everyone talks about the need of hiding them in course of scrolling.
Any help will be much appreciated.
P.s. this is how I set toolbar and tablayout on my mainactivity:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); // Setting/replace toolbar as the ActionBar
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.addTab(tabLayout.newTab().setText(getResources().getText(R.string.tab0)));
tabLayout.addTab(tabLayout.newTab().setText(getResources().getText(R.string.tab1)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
Edit: these images will illustrate better the issue i'm facing
Main fragment
second fragment
Main fragment again
When I go to nested fragment (Second fragment) I remove the toolbar, but when I press back to return to the Main fragment it's also removed in there.
What I want is to hide it on my nested fragments and leave it on the main fragment.

well my guess from your statement seems like you are trying to load. fragment in an activity and want not to see the toolbar and also the tab bars! well the approach you are using is right! using visibility GONE is the way to do it I think the same but the issue is the placement of the code! here is how you should do it!
in your container activity where you are trying to have the fragment loaded after doing
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); // Setting/replace toolbar as the ActionBar
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.addTab(tabLayout.newTab().setText(getResources().getText(R.string.tab0)));
tabLayout.addTab(tabLayout.newTab().setText(getResources().getText(R.string.tab1)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
here is what you have to do! simply where your fragmentTransaction Commits the fragment into the container after that commit you can do it like this
FragmentManager mFragManager = getSupportFragmentManager();
FragmentTransaction mFragTranscation = mFragManager.startTransaction();
mFragTranscation.add(R.id.containerId,yourFragObject());
mFragTransaction.commit();
//after that you can do the visibility GONE MAGIC
toolbar.setVisibility(View.GONE);
tabLayout.setVisibility(View.GONE);
You were trying to hide the Activity's design part from within the fragment if you want that to happen the next way to do it is using the ONACTIVITYCREATED() power but that also has to have a way to access the Views of Container Activity in the fragment which can be done in this method.
but above written code will help if you want the activity's header design gone on opening of the fragment! happy coding
for further if you want anything to come back which is hidden you can have the OnBackPressed function ! doing it for you! for example if you want to see that when main fragment comes in action and the TOOLBAR etc should be back you should Override the OnBackPressendMethod and there you can check which fragment is currently getting loaded back from stack and if its the "Required One" then just use VISIBLITY(VIEW.VISIBLE) for the ones you want to be back on screen!
override in MainFragment
#Override
public void onBackPressed() {
//here you can call the things back if the frag required is loaded
}
//try this way to do the add fragment and adding its name in to stack
private void replaceFragment (Fragment fragment){
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped){ //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();
}
}
that way you can do it for more check this thread here STACK_THREAD

Related

Android: Hide viewPager tabs when launching a fragment from one of the viewPager tab fragment

I have a viewPager in my app which has several tabs. In some of the tabs, on clicking an item a new fragment is shown. I want this fragment to cover the tabs. Doing this is possible but the approaches don't look good to me.
1.) one way is that I add the newly created fragment to the activity using getSupportFragmentManager(). This solves the problem but doesn't look like a good idea as it will create problems when using back button etc.
2.) Other way is to hide the tabs manually using Visibilty.GONE but problem with this approach is that this hiding of the tabs is visible, I mean the animation could be seen and looks bad.
Is there a better approach to do this problem?
This is my code. "sub_fragment_container" is present in the activity xml, so I get an error java.lang.IllegalArgumentException: No view found for id 0x7f0e00ff (com.my.app:id/sub_fragment_container) for fragment DetailFragment{
FragmentTransaction fragmentTransaction = getChildFragmentManager()
.beginTransaction();
Fragment profileFragment = new DetailFragment();
profileFragment.setArguments(bundle);
fragmentTransaction
.add(R.id.sub_fragment_container, profileFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Use android:animateLayoutChanges="true"
Or, to fine tune it further, here's a tested method that I've personally used in my app Newslet to solve the exact same problem.
Wrap your Toolbar and TabLayout within an AppBarLayout. This should be easy if you're aware of the Design Support Library. If not, this should get you started.
Add this piece of code in your onCreate():
LayoutTransition layoutTransition = new LayoutTransition();
layoutTransition.setDuration(200);
layoutTransition.setStartDelay(LayoutTransition.CHANGE_APPEARING, 0);
layoutTransition.setStartDelay(LayoutTransition.APPEARING, 0);
layoutTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
layoutTransition.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 0);
appBarLayout.setLayoutTransition(layoutTransition);
After this, when you remove the Tabs or add it back, the layout change will animate smoothly.
Hope this helped you!

Hiding Actionbar before fragment transaction (Android)

My application contains an activity with several fragments. The activity displays the ActionBar by default. However, there are several fragments, displayed in order, where the ActionBar should not show. For these fragments, I hide the ActionBar in the onCreate code with getActivity().getActionBar().hide(); . However, each time one of these fragments are loaded, the ActionBar flashes on the screen momentarily before disappearing.
How can I make the actionbar disappear before the fragments are displayed on the screen? For reference, below is the code I use for transactions between the fragments:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.commitAllowingStateLoss();
I would suggest two ways to solve this.
Reconsider your activity-fragment relation, should it be split into different activities, i.e. some activities with action bar, some without action bar.
Use toolbar, and remove all default actionbar, assign the toolbar to your fragments which need it instead of the activity. Be reminded that you are not using setSupportActionBar() as this is not part of the activity layout.

Fragments aren't replacing properly in FragmentPagerAdapter

I'm going to develop an android app to use Fragments with a ViewPager using the FragmentPagerAdapter. I'm trying to replace fragments onClick of a button inside a fragment but when I click on that button, the current layout goes up and next layout still comes on that position i.e.2 layouts come on a single fragment and I don't want it.
I'm using these steps to replace but not getting changed properly,
FragmentTransaction transaction = getActivity()
.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.select_program_root, newFrag);
transaction.addToBackStack(null);
transaction.commit();
Even I'm using TabHost with ViewPager inside a horizontal ScrollView. Everything is fine in my app. I'm only stuck on replacing of fragments onClick of buttons which are located inside fragments.
Thanks in advance.
Finally, I solved it by implementing few lines of code.
Create this method into your FragmentActivity:
public void setCurrentItem(int item, boolean smoothScroll) {
mViewPager.setCurrentItem(item, smoothScroll);
}
And call it onClick of a Fragment:
((MainActivity) getActivity()).setCurrentItem(3, true);

Android: How to completely remove ActionBar

How can I completely remove the ActionBar from Certain Fragments. I want to remove it not just to hide it. I have Actionbar Tab Navigation. From that i added a new Fragmnt which didn't need actionBar. So i need to remove it. I can't hide it because when i pressed back button and moved to Tabs section, i need to show Action bar again.
This is how i am replacing Fragments.
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.frame_container, fragment)
.addToBackStack(null).commit();
The action bar is not part of your fragment. It's part of your activity.
You can create a new Activity with a theme Theme.Holo.Light.NoActionBar and open this Activity with :
getActivity().startActivity(new Intent(getActivity(), SecondActivity.class));
And inside your second activity you can display the fragment you want.
I recommend you this post to understand more the differences between Fragment and Activity: https://stackoverflow.com/a/10515807/3112836
If you're not using actionbar.hide() just because you have to show Action bar in other fragment, than I think you should give it a try by using :
actionbar.hide() inside onAttach method of your fragment and actionBar.show() inside your onDestroy() method.

Hiding actionBar tabs with navigation mode adversely affecting fragment backstack

I've got an activity with an actionbar, and actionbar tabs. When I choose an item from the contents of the tabs, I'm trying to replace the current fragment with a new one, add the transaction to the back stack, and hide the tabs.
I hide the tabs by changing the action bar navigation mode to standard.
The problem is that when I press the back button, I just get a blank view with the action bar (in standard mode).. The fragment transaction doesn't appear to be reversed.
If I don't hide the tabs by changing the navigation mode to standard, the transaction reversal works fine..
I've tried overriding the back press to change the navigation mode back to tabs, but it doesn't work.
Could someone tell me how they would achieve this?
Here is the code where the tabs get hidden and the fragment transaction takes place:
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Fragment albumListFragment = new AlbumListFragment();
albumListFragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, albumListFragment);
ft.addToBackStack(null);
// Commit the transaction
ft.commit();
For clarity: I would expect that pressing back after this transaction has been committed, the tabs would come back into view with the previous fragment. At the moment after pressing back it's not showing the tabs OR the fragment.
Not sure if this question is still relevant or not, but I seem to be having good luck hiding the tabs after the fragment transition.
From my ListFragment, I do a standard fragment transition very similar to how you're doing it in your code.
In my detail fragment, however, I hide the action bar in the onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
}
and show it again in my onPause method:
public void onPause() {
super.onPause();
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}

Categories

Resources