I implement a BottomNavigation view for android and I have some fragments to show as BottomNavigation pages. According to the Google Material Design Guide Lines I want to show fragments with cross fade animation.
By touching BottomNavigation's items my ViewpPager change the fragments with default slide animation.
I read some solutions in this and this. but these aren't really fade animation and I couldn't set fading duration.
So is there any way to set an animation on changing ViewPager's tabs ?
Finally, I found my answer.
I changed the ViewPager with a layout to keeps my fragments(a frame layout). Then I added fragments into a fragmentTransaction.
By touching an item on BottomNavigation, current fragment hides and new fragments goes to shown with a fade animation defined in fragmentTransaction.
and this is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
addFragmentsToManager();
}
private void addFragmentsToManager() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
fragmentTransaction.add(R.id.flContent, tripFragment, tripFragment.getClass().getSimpleName());
fragmentTransaction.add(R.id.flContent, notificationFragment, notificationFragment.getClass().getSimpleName());
fragmentTransaction.add(R.id.flContent, searchFragment, searchFragment.getClass().getSimpleName());
fragmentTransaction.add(R.id.flContent, profileFragment, profileFragment.getClass().getSimpleName());
fragmentTransaction.hide(tripFragment);
fragmentTransaction.hide(notificationFragment);
fragmentTransaction.hide(searchFragment);
fragmentTransaction.hide(profileFragment);
fragmentTransaction.commit();
}
private void changeTab(int position) {
Fragment fragment;
switch (position) {
fragment = .....// get framgnet from position
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
fragmentTransaction.hide(prvFragment);
fragmentTransaction.show(fragment).commit();
prvFragment = fragment;
}
There is a problem with adding and hiding fragments.
When the application is idle and the phone goes to sleep mode if you turn back to the application all fragments shown in activity and you see all layouts in one.
Related
I have a navigation view that loads some fragments depending on which is pressed. In one of the fragments, I want to have a button, which when it is pressed, the fragment changes from the one loaded by the navigation view to a different fragment. I have tried using
ProfileEditFragment pef = new ProfileEditFragment();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().remove(pef).replace(R.id.drawer_layout, pef, "Fragment")
.addToBackStack(null).commit();
When I do this, it loads the other fragment on top of the current fragment, rather than replace it, even though I used the replace function. How do I fix this?
Can you try this:
ProfileEditFragment pef = new ProfileEditFragment();
FragmentTransaction transaction =getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.drawer_layout, pef);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
I'm creating a fragment which is supposed to act like a menu. I have successfully inflated it to the activity where I wanted it to be, however I now I realise that I cannot close the fragment. Furthermore I am able to scroll the contents of the activity which the fragment is placed over. How can I edit my code in such a way that the fragment will close after an action on the activity is detected, or one of it's contents is clicked?
I created the fragment by simply adding a fragment via new -> Fragment -> Fragment(Blank). I have not touched any of the code and have initialized the fragment like so in a on click:
findViewById(R.id.Menu).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_left);
MenuFragment menuFragment = new MenuFragment();
fragmentTransaction.replace(android.R.id.content, menuFragment);
fragmentTransaction.commit();
}
});
This is what it looks like, ignore the horrendous design.
I have two fragments in an activity.
When i go from fragment A to fragment B, custom animation have been added. So that the view of fragment B slides in from the right on entry and then slides back out from right on exit.
But the fragment B can been seen to be entering from behind the view of fragment A. So when Fragment A is replaced by Fragment B, the view of Fragment B animates from right to left but it comes in from behind the view of Fragment A and not
in front of Fragment A.
I am able to notice this since half of Fragment A is transparent and Fragment B sliding in can be seen only from that transparent half indicating that it is sliding in from behind Fragment A and not from the front.
Not entirely sure why this is happening. The respective code has been provided below.
Could someone please help?
private void navigateToFragment(#NonNull Fragment fragment, boolean addToBackStack, int enterAnim, int exitAnim, int popEnterAnim, int popExitAnim) {
FragmentManager fragmentManager = getSupportFragmentManager();
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(enterAnim, exitAnim, popEnterAnim, popExitAnim);
transaction.replace(R.id.member_address_root, fragment, fragment.getClass().getSimpleName());
if (addToBackStack){
transaction.addToBackStack(fragment.getTag());
}
fragmentManager.executePendingTransactions();
transaction.commit();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_member_address);
if (savedInstanceState == null){
if (getIntent() != null){
Fragment fragment = FragmentA.newInstance();
navigateToFragment(fragment, false, R.anim.no_animation, R.anim.no_animation, R.anim.no_animation, R.anim.no_animation);
}
}
}
#Override
public void onFragmentBRequested() {
FragmentB fragment = new FragmentB();
navigateToFragment(fragment, true, R.anim.enter_from_right, R.anim.no_animation, R.anim.no_animation, R.anim.exit_from_right);
}
onFragmentBRequested() is called from FragmentA based on a button click.
Ok, So I experimented a bit with what you are trying to do. Basically you want that current fragment shouldn't move/animate from it's place & the new fragment should animate only. I couldn't achieve with transaction.replace() with 'R.anim.no_animation' as the entry animation happens behind the current fragment.
so instead of replacing new fragment, adding it seems more practical.
so using transaction.add() instead of transaction.replace() works as expected.
I am really new to android development. I have created a simple main activity and add an icon on top left. Clicking it I can show a blank fragment.on my screen replacing a layout which is loaded in onCreate method. Now clicking another icon I want to hide that fragment and load that layout again. How to do that?? any helps??
Below is my code
//part of oncreate where my layout is loaded
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// part of code when icon clicked and fragment is loaded
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BlankFragment frag = new BlankFragment();
fragmentTransaction.replace(R.id.content_main, frag);
fragmentTransaction.commit();
//another nearby icon clicked
//now i want to replace this fragment from content_main layout
//what code to add??
If I got your question right, then this is the right answer I guess.
//keep track of all fragments you add by tagging
fragmentTransacaction.add(R.id.content, new FragA(), "first");
//and when removeing
Fragment f = getFragmentManager().findFragmentByTag("first");
if(f!=null) fragmentTransac.remove(f);
fragmentTransac.commit();
I got this from here
You can toggle visibilty by below code.
public void toggleVisibility(Fragment fragment){
FragmentTransaction transaction = getFragmentManager().beginTransaction();
if (fragment.isHidden()) {
transaction.show(fragment);
} else {
transaction.hide(fragment);
}
transaction.commit();
}
I've followed instructions from dmanargias answer here: Android Fragments and animation
The animations themselves work, however the initial animation when adding a fragment is doing something strange. The initial fragment appears to be replaced with the new fragment before the animation is started.
e.g. One would expect an animation of
A <- B (B sliding from right to cover A)
However as soon as the action starts A instantly becomes B and you get an animation of
B <- B.
When popping the stack you get a correct animation of A -> B (B sliding away revealing A)
This is the code that adds a fragment:
CategoryFragment newFragment = CategoryFragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
fragmentTransaction.replace(R.id.fragment, newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Any ideas why this would happen and if there's a way to fix it?
Try this:
final CategoryFragment newFragment = CategoryFragment.newInstance();
final View container = findViewById(R.id.fragment);
container.postDelayed(new Runnable() {
#Override
public void run() {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
transaction.replace(container.getId(), newFragment).commit();
currentFragment = cardFragment;
}
}, 0);
I was having exactly the same problem, but with different animations.
Check that android:shareInterpolator is not set to true in your xml animations.