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);
Related
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();
Current layout
The above is the app layout in Android app that I am working on. There is an activity, which has a BottomNavigationView. The navigation is managed using:
this.getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
Now, on this fragment, I have a TabLayout and a ViewPager as you can see in the image. The ViewPager listens to the TabLayout selector and it can be changed by either sliding the ViewPager or clicking on the tab. Works fine..
Once a tab is selected, I am loading another Fragment that has a RecyclerView on it. The contents in the RecyclerView is coming from a URL so its being updated on Post() as below:
CompletableFuture.runAsync(this::prepare);
recyclerView.post(() ->
{
adapter.list = newList;
adapter.notifyDataSetChanged();
progress.setVisibility(View.INVISIBLE);
});
Now the issue that I am facing:
Everything works fine for the first time when the Menu 1 in the layout image is clicked. The recycler view is able to call the service and load the content. Now if I switch the navigation to Menu 2 or any other and try to come back to Menu 1, nothing progresses from the ViewPager / TabLayout. Up to the TabLayout I can see Logs, and after that nothing. Basically setting the currentItem to the TabLayout is called but its not calling the create of the next Fragment and hence its not loading the RecyclerView again. The ViewPager is using FragmentStatePagerAdapter.
TabViewerAdapter extends FragmentStatePagerAdapter
The adapter is initialised using ChildFragmentAdapter as below:
TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 0, tabs.getTabCount());
I know behaviour "0" is deprecated so tried with "1" as well as below:
TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 1, tabs.getTabCount());
Tried invalidating views, notifying adapter and many other.. No use.
I also noticed that when I switch app (while its running by tapping the phone square button to go to another app) and come back (resume) then its loading the recycler view again.
This means, I am missing something and tried the onResume method. But didn't work.
Am I doing something stupid or against design patterns or conventions? No idea what to do..
Please help, thank you.
I resolved this issue by doing the things below:
Used a SharedViewModel to handle the list
Used ListAdapter for RecyclerView
All good now.
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
I'm using a custom Toolbar widget in my app, I found that the label attribute in Manifest file doesn't work. Any suggestion except using "setTitle()" method in Activity/Fragment? Thank you.
<activity
android:name=".CartActivity"
android:label="#string/cart_activity_label"
android:parentActivityName=".CatalogActivity" />
I wish the title can be shown by using attributes in Manifest file.
use a textview inside the Toolbar widget or if you are using support library, use setsupportActionbar(toolbar); in your activity.
getActionBar().setTitle(R.string.logout);
or better,
getSupportActionBar().setTitle(R.string.logout);`
in onCreate method
Ok...I just found out that I forgot to link the View (Toolbar) object to the View id. Follwing is the complete setup for using a Toolbar as a default ActionBar:
private Toolbar toolbar;
toolbar = findViewById(R.id.toolbar); //I missed this
setSupportActionBar(toolbar);
Toolbar title explain:
You change the default system style to ".NoActionBar", the default ActionBar disappears.
You add a custom Toolbar to the Layout, an "ActionBar" without title appears.
There is no title on the Toolbar even though you've already set the label attribute in Manifest file, because the Toolbar is not recognized as default ActionBar by the system, that's why we need to use "setSupportActionbar()" method to make it work like a normal ActionBar.
Don't forget to link the View of the Toolbar.
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);
}