add toolbar menu only at specific fragments - java

So I have this app and in the toolbar, there is a drop-down menu and I am looking for a solution on how to show only specific items at specific fragments.
I know that I have to make a java class and link it to the menu, but how do I find out what fragment I am on?

You can move the implementation of setting the toolbar for your fragments in the activity that launches these fragments. In that case, you may have the toolbar in your activity and the activity should have a fragment container where you will put your fragment. Let us consider a layout like the following for your activity.
<RelativeLayout>
<!-- Your toolbar here in the activity -->
<ToolBar>
android:id="+#id/toolbar"
...
</ToolBar>
<!-- Your fragment container here -->
<FrameLayout>
android:id="+#id/fragment_container"
...
android:layout_below="#id/toolbar"
</FrameLayout>
</RelativeLayout>
Now, you have the ToolBar here in your activity and hence, you can set the toolbar when you are loading the fragments in the fragment container. Your activity might have the following functions.
public void launchFrag1() {
// replace the fragment 1 in the fragment container
loadToolbarForFrag1();
}
public void launchFrag2() {
// replace the fragment 2 in the fragment container
loadToolbarForFrag2();
}
Now call the specific function of your activity to load the fragment and the toolbar dynamically as well.
If you are trying to call the method from the fragment, you can always call those methods declared in your activity like the following.
((YourActivity) getActivity()).launchFrag2();
I hope you get the idea.
Please note that this is just a basic implementation of how you can get your dynamic toolbar working in your app. However, your implementation might vary based on the use case that you have.
I hope that helps!

I have an ImageView in the toolbar which I hide as follows:
// Show image in fragment 1
ImageView img = getActivity().findViewById(R.id.toolbarMenu);
img.setVisibility(View.VISIBLE);
// hide image in fragment 2
ImageView img = getActivity().findViewById(R.id.toolbarMenu);
img.setVisibility(View.GONE);
Try these codes in onCreateView after inflating your layout

Related

how Set OptionsMenu when certain fragment is shown

In my main Activity, I have a fragmentContainerView in which I am changing the fragments by using bottomNavigationbar which has 2 navigation Items (home & profile).
Now I want to show OptionsMenu the "profile" fragment is shown and then again remove it when the "home" fragment is shown.
How can I do it? or is there an alternative?
Create a public method in the activity to change set the option menu and call it from the fragment like below.
HomeActivity homeActivity=(HomeActivity) getActivity();
assert homeActivity!=null;
homeActivity.yourPublicMethod();

Android Studio getSupportFragment.beginTransaction().replace causing crashes when navigating to another fragment(Java)

I am trying to implement a side-navigation bar (hamburger menu). I want to be able to select a new fragment to view from this menu, and then click buttons on that fragment to direct to another fragment (not in the hamburger menu list).
I was following a guide (https://www.youtube.com/watch?v=bjYstsO1PgI), but it doesn't show the second step of redirecting to the other fragment.
I keep getting a crash whenever I click a button that redirects to the other fragment, if it has been loaded by the hamburger menu.
The line I use is to render from the hamburger menu is:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new FirstFragment()).commit();
This is called by the following event listener:
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
and I get this error whenever I press a button to go to the 'other' fragment.
Navigation action/destination com.example.testApp:id/action_FirstFragment_to_submitScreen cannot be found from the current destination Destination(com.example.testApp:id/submitScreen) label=fragment_submit_screen class=com.example.renamedTestApp.SubmitScreen
This error is not present when I use
<include layout="#layout/content_main"/>
to load the first fragment on app startup (the one from the hamburger menu).
Any tips? This question may be confusing so let me know if I should tidy up my explanation.
I believe the error may be in the fragment not being fully loaded perhaps? If that is the case, what would be a better line to use to switch on the fly.
provide some code
try this
FragmentManager fragmnt= getActivity().getSupportFragmentManager();
FragmentTransaction tr= fragmnt.beginTransaction();
tr.replace(R.id.YOUR,new Yourfragment());
//or tr.replace(R.id.content_frame, new Yourfragment());
tr.commit();

Create a Custom Toolbar programmatically on Android

I have an app with some behavior. As an upgrade I'm going to add a Toolbar to the app. I know, I can do it putting de XML on each activity. But I want to create a Layout XML for the action Bar and inflate on each activity on the OnCreate method.
This way I think I can add the bar whenever I need it without touching the XML,and maybe make the code more efficient:
Assumptions:
I have a XML with a layout with tag "< Toolbar >"
The app has the noActionBar theme
This is an extract of OnCreate()
android.support.v7.widget.Toolbar barra =(android.support.v7.widget.Toolbar) getLayoutInflater().inflate(R.layout.barra_herramientas,null);
LinearLayout layout_actividad = (LinearLayout) findViewById(R.id.contenedor_principal);
layout_actividad.addView(barra);

Custom layout for my toolbar with buttons . Should I create a JavaClass for it?

I have an android-studio app with custom toolbar. I use a layout for my toolbar with buttons and I call it with <'include> in every activity. I used android:onClick in the xml for the buttons.
The problem is, that I am not sure how I should connect my buttons (located in my toolbar layout) since I have no Java Class for it. If I create a Java Class I should extend it and I normally use Activity/AppCompatActivity, but my toolbar layout is not activity and the app crashes when I click the buttons in the emulator.
So how should I make the buttons work?
You have different ways to do it. I can show quickly 2 ways in pseudo code :
Solution 1 :
Layout activity :
<Coordinator>
<AppBarLayout>
<include layout="#layout/custom_toolbar"/>
</AppBarLayout>
</Coordinator>
Layout Toolbar :
<Toolbar id="#+id/toolbar">
<TextView id="#+id/tv_toolbar_title"/>
</Toolbar>
Activity :
onCreate() {
toolbar = (Toolbar)findViewById(R.id.toolbar);
tvTitle = (TextView) findViewById(R.id.tv_toolbar_title);
setToolbar(toolbar);
//handle click on view if you want
}
Solution 2 :
Layout activity
<Coordinator>
<AppBarLayout>
<CustomToolbar id="#+id/customToolbar""/>
</AppBarLayout>
</Coordinator>
Custom Toolbar Class => CustomView
CustomToolbar extends Toolbar {
//find your views
}
Layout custom toolbar :
<merge>
<TextView id="#+id/tv_toolbar_title"/>
//other views needed
</merge>
In your activity you will have the customtoolbar :
onCreate() {
toolbar = (CustomToolbar)findViewById(R.id.toolbar);
}

Unable to start a fragment from a fragment

Hello everyone i have a view pager named "viewpager" and two fragments named "Add_Create" and "Add_Create_2".I am using Tab layout to show different tabs.Now in one of the tabs there is "Add_Create" fragment.In Add_Create fragment there is a button and on that button click i want to show Add_Create_2 fragment.Below is my code that i have tried
Add_Create_2 add= new Add_Create_2();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.viewpager,add);
transaction.addToBackStack(null);
transaction.commit();
Now this code is present in first fragment named "Add_Create".
The problem could be at the viewPager. What kind of element is this? If it is a ViewPager, you cannot load it a Fragment. You should to create on it maybe a RelativeLayout, and it will be your fragment container.
Instead of using getFragmentManager(), use getSupportFragmentManager(). Since you're calling it from a fragment, maybe getActivity().getSupportFragmentManager()

Categories

Resources