Android: Button animation - java

In my MainActivity I have a FrameLayout container for a Fragment.
One of the Fragments is a normal rectangular Button and the other one is a circular Button (both buttons have the same height).
When I click the Button I want the rectangular Button to transform into circular Button like this:
This is the code I currently have:
Fragment2 fragment = new Fragment2();
setSharedElementReturnTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_trans));
setExitTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.fade));
fragment.setSharedElementEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_trans));
fragment.setEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.fade));
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.button_container, fragment)
.addToBackStack("transaction")
.addSharedElement(view, "button_transition")
.commit();
The problem is when I click the button it transform into an elliptic shape and then to a circular button, but when I click the back button the animation is smooth.
So how to correct it to be like in the example above, or a sample close to that (I only need the button animation).

Related

Acivity1 Fragment2 -> Activity2 (after button click) -> Activity1 Fragment2 navigate from activity to specific fragment

Hello I have an application that implements bottom navigation component.
There is a main activity with 2 fragments (home fragment, note fragment).
I am going from note fragment to another activity called AddNoteActivity.
After I click a button in the AddNoteActivity I want the screen to show the main activity with the note fragment.
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
I understand that this code will launch the main activity but it automatically loads the home fragment because of its set as the app:startDestination in moblie_navigation.xml.
How would I get the main activity to load up the note fragment upon button click in the AddNoteActivity?
Also I have an action bar with up on navigation enabled. When I click the up arrow, I go back to the note fragment like its supposed to.
For go to the fragment just write
on your button click {
finish();
}

I have a Navigation Drawer and want open another isolated fragment

I have a navigation drawer and when i click on one button i want to go to other fragment where i can't access to the navigation drawer and anything else. I just want have a black arrow in the top-left to go back and return to the navigation drawer with all the fragments.
I tried somethings but don't works like i want normally navigator continues accessible and see bot layouts one over the other.
MAIN FRAGMENTS CONTROLLER
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
DrawerLayout drawerLayout=findViewById(R.id.drawer);
NavigationView navigationView=findViewById(R.id.nav_view);
setSupportActionBar(toolbar);
appBarConfiguration= new AppBarConfiguration.Builder(R.id.nav_home,
R.id.nav_user_data,R.id.nav_join_group,
R.id.nav_user_groups,R.id.nav_search_documents)
.setDrawerLayout(drawerLayout).build();
navController = Navigation
.findNavController(this,R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
NavigationUI.setupWithNavController(navigationView,navController);
}
#Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController,appBarConfiguration)
|| super.onSupportNavigateUp();
}
I'm trying with a botton on my ActionBar, when i click it should go to my new fragment (with a generic app back but with one back button)
You simply should specify your action in navigation.xml file like others.
It will change hamburger icon by itself because you synchronize your navigation controller with NavDrawer in this two lines of code:
NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
NavigationUI.setupWithNavController(navigationView,navController);
We need to add one fragment in our navigation.xml (the file where we define our fragments associated with the Fragment and the layout) for the Fragment where we want to go. Add there an action with a unique id and where we want to go in the fragment where we are.
Sorry, i know it's a little bit confused.
NAVIGATION
We go from there
<fragment
android:id="#+id/nav_user_data"
android:name="com.example.androidapplication_reto2.project.activities.navigationfragments.SeeAndModifyUserDataFragment"
android:label="User Data"
tools:layout="#layout/fragment_see_and_modify_user_data"
>
<action
android:id="#+id/action_nav_user_data_to_nav_modify_data"
app:destination="#+id/nav_modify_data" />
</fragment>`
To there
<fragment
android:id="#+id/nav_modify_data"
android:name="com.example.androidapplication_reto2.project.activities.navigationfragments.ModifyUserDataFragment"
android:label="Modify User Data"
tools:layout="#layout/fragment_modify_user_data"
/>
With the action that we defined in the origen fragment.
ORIGEN FRAGMENT
And in the Fragment when you click on one button just happen this.
Navigation.findNavController(getView()).navigate(R.id.action_nav_user_data_to_nav_modify_data);

How to remove fragment from stack programmatically?

In my application, from MainActivity I go to Fragment1 and from Fragment1 I replace Fragment1 with Fragment2. Now I want to remove Fragment1 from stack. How I do this?
When you are added the fragment,just add the TAG for that fragment.
With the use of that TAG you can easily remove your old fragment.
Eg.
FragmentManager fm = getSupportFragmentManager();
fm.replace(R.id.container,new MyFragment(),"TAG_FRAGMENT1").commit();
To remove old fragment use below code,
Fragment oldFragment = fm.findFragmentByTag("TAG_FRAGMENT1");
fm.beginTransaction().remove(oldFragment).commit();
After removing old fragment you can able to add new fragment.

How to set a temporary fragment to my "home screen" Android studio

I have started coding and I really like it, I have been on/off on diffrent project (to learn diffrent things) But now i got a problem, I "app"reciate all the help i can get.
I am using the navigation drawer and got three fragments installed. I can navigate through the navigation drawer and select a fragment and it loads on my screen. But when i start my app the screen is just white and the navigation drawer is on the left and i can pick a fragment. Is it possible to have a fragment on my "home screen" so when I start the app there is a fragment already placed on the screen BUT! when i chosse another fragment in the navigation drawer i would like to have my fragment who loaded at the beginning to disappear. Otherwise both fragments will show on eachother (Text on text). Maybe to have the temporary fragment to "finish" itself somehow. Please look at my imgur image to get my idea.Imgur image press here to view
Here is a generic solution on how to use the FragmentManager. It should give a good idea on how to display a Fragment. Besides that you could include a static Fragment in your layout. It's up to you how to approach it.
So the FragmentManager solution looks like this:
YourActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = AnyFragment.instantiate(this, AnyFragment.class.getName());
fragmentManager.beginTransaction().add(R.id.fragment_placeholder, fragment).addToBackStack(null).commit();
}
The target id is defined in your activity layout.xml
<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">
<FrameLayout
android:id="#+id/fragment_placeholder"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
Later if you want to replace the current Fragment you can use the remove and add method of the FragmentManager (the replace method is kinda buggy).
Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragment_placeholder))
fragmentManager.beginTransaction().remove(currentFragment).add(R.id.fragment_placeholder, yourNewFragment).addToBackStack(null).commit();

Android: Refresh current fragment after language change

So my MainActivity has a navigation drawer with a set of fragments.
On top of the nav bar, I have 2 flags which represent a language. If an user clicks on a language, the app changes the language of the whole app.
Everything is working smoothly (activity refreshes, navigation drawer and all fragments get translated) but the fragment that's open doesn't. This means that the user needs to click on the navigation drawer and select the fragment again to see it translated.
How can I refresh the current fragment the user is in?
Add tag to your fragment when you commit it:
fragmentManager.beginTransaction().replace(R.id.your_id, fragment, "Your_Fragment_TAG").commitAllowingStateLoss();
Then You can detach and attach again your fragment for refresh:
// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();

Categories