Appending extra toolbar in fragment view - java

In my application on the dashboard, there is an application bar that looks something like the image below.
As shown in the image, I have a toolbar that currently shows the Navigation drawer.
From the navigation drawer, I can navigate to a fragment A that replaces the TAB area under the toolbar as shown in the image below.
Is there any way I can add two toolbars like this where I can show the navigation drawer icon and back button together inside a fragment?

There is no best way to achieve what you are looking for, considering it goes against the Android Design Guidelines. Although not explicitly stated, the navigation drawer icon and the back button icon are never displayed together.
The theory behind this design is that navigation should be intuitive and predictable, displaying two navigational icons next to each other detracts from an intuitive user interface. The back button should be displayed if there is something to go back to. Though, the need for persistent navigation can be addressed in two different ways. Take Google's Gmail app for example.
By default the NavigationView supports using a swipe from the left edge of the screen as a gesture to open the navigation drawer. In the Gmail app, the navigation drawer icon is show while you are in any one of your inboxes. As soon as a message is selected, the navigation drawer icon is replaced with the back button. Though, you will notice that you can still access the drawer using the gesture stated previously.
On larger screens, the Gmail app supports a miniature navigation drawer. This drawer can remain in view without the need to display to navigational icons. Though this is a little more difficult to implement with the current support library, if you are looking to, this answer may help.
Finally, to answer your question, there is no built-in way to display a back button and a navigation drawer icon together. If you need to do so, the only way would be to use a "work-around", as you currently are.
Though, the only change I would probably make is to use an ImageButton rather than an ImageView. Then you can set the style of the button using style="?android:attr/actionButtonStyle" to mimic the look of an ActionBar button.

Just to update, for the time being, to achieve the above what I have done is.
I added a layout to my fragment file where I draw a custom back button like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark20"
android:orientation="horizontal"
android:padding="15dp"
android:weightSum="3">
<ImageView
android:id="#+id/backButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_weight="0.2"
android:src="#drawable/delete_icon" />
</LinearLayout>
When a person clicks on backImage, I call popBackStack.
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().popBackStack();
}
});
The above code gives me the desired functionality.
Note, for the above to work you need to make sure you are adding your fragmentTransaction to backstack as below
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.home_content_layout,NAME_OFYOUR_FRAGMENT,"nameOfYourFragment");
transaction.addToBackStack(null);
transaction.commit();

Related

Navigation Components: Switch Menu of Navigation Drawer (for logged in users)

I am using the new Navigation Components library. I have a Navigation Drawer in place that works perfectly fine.
If my user logs in I want to change the menu of the Navigation Drawer. Specifically, I want to change the "Login" item to "Logout" from within a fragment.
All the solutions I found online didn't use Navigation Components. So therefore, they recommended something like this:
// Get the navigationView and swap the menu
NavigationView navigationView = view.findViewById(R.id.nav_view);
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer_logged_in);
However, this doesn't seem to work in my case (I cannot get the navigationView from within a fragment). How does one achieve this with Navigation Components?
You can access Activity from Fragment through interface:
Create and interface say LoginSuccessListener
Implements this in your Activity
Inside fragment receive this listener inside onAttach
Use it as you needed to update Navigation Menu

Android Dev: Navigation View vs App Bar

I'm new to Android development. I get that a NavigationView is an AppBar (i think). What makes these two different? I want to create a custom "appbar/toolbar" with a centered logo and menu/settings button in the top right corner (instead of the standard left) which reveals a drawer.
I was going to ditch the built in appbar/toolbar all together and just create my own somehow and include button overlay which displays a drawer.
What would you do? Navigation view, app bar, or custom toolbar from scratch? I don't know what the standard is or what is acceptable. What is the difference between a navigationview and appbar. Thank you.
edit: I'm slowing realizing that an appbar is one feature within a navigation view, among others like a drawer layout, menu items etc... i think.
1. NavigationView
By using NavigationView, we can bind the menu directly with NavigationView. This is the benefit of the NavigationView. No need to create ListView and adapter with navigation drawer. By default we can get selector of item click. With menu we can change the color of icon of selected menu.
For more details :
1.https://developer.android.com/reference/android/support/design/widget/NavigationView.html
http://www.technotalkative.com/part-4-playing-with-navigationview/
2. AppBar
Appbar is for toolbar with scrolling effect. We can easily give the material design effect.
For more details :
1.https://developer.android.com/reference/android/support/design/widget/AppBarLayout.html

Navigation Drawer app behaivour when open fragments

Im working with a Android app that uses Navigation Drawer. This app use the template provided with Eclipse ADT (when you select the navigation drawer template).
I dont understand the behaivour of the navigation drawer very well.
My main requeriment is make a "search" option, I have on the Navigation Drawer a EditText to get the query string from the user. I want that I press search button and open my SearchFragment getting the search query.
I know that I can make this:
Bundle args = new Bundle();
args.putString("searchQuery",searchQuery);
fragment.setArguments(args);
And this for getting:
getArguments().getString("searchQuery");
But I have the next:
MainActivity
NavigationDrawerFragment
SearchFragment
OtherFragments.. (I think this arent relevant in the question)
I dont understand where I can make this steps.
More data:
I have in the navigation drawer class the EditText with the setOnEditorActionListener for the search press.
Any info that I can add I will be add. Sorry my english
Thanks for the help
Navigation drawer is basically a component that show 2 pane in the way that one is the main pane (usually fragment) that is used to show content and the other one is usually a listview that is just used to choose which fragment to show in main pane
so if you want to switch between 2 different layouts you have to create 2 different fragments and then you can add or replace these fragments based on listview's selected item.
You may refer to below link for complete code
http://developer.android.com/training/implementing-navigation/nav-drawer.html
Hope it helps!

Handler for ActionBar MenuButton click

Even after a long search on this subject I came to no result. So I want to ask here.
In my Android application, I try to set a onClick listener for the menu button in the upper right corner (on the actionbar). When the button is pressed, the NavigationDrawer should open. (I have seen this in an app. Unfortunately, I do not know how this app is called.) I do not want to open the menu.
I have already tried:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:onClick="showdrawer">
<item />
</menu>
with
public void showdrawer(View v) {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.openDrawer(Gravity.LEFT);
}
...but nothing happens when I press the button.
Is there a way to implement this?
I would be very grateful for help.
In my Android application, I try to set a onClick listener for the menu button in the upper right corner (on the actionbar).
First, there is not necessarily a "menu button in the upper right corner (on the actionbar)" on all devices, when using the stock action bar implementation. It will depend upon API level of the device and whether the device has an off-screen MENU key.
Second, that is for the overflow. Please leave it alone.
Third, you have no direct access to that button anyway.
When the button is pressed, the NavigationDrawer should open
The navigation drawer is usually opened by tapping on the left side of the action bar, on the app icon, particularly when it has the "mini-hamburger" on the left edge. That is handled for you via ActionBarDrawerToggle, if you are using DrawerLayout for the navigation drawer.
It is possible to have a right-hand drawer as well, though I seem to recall Google advocating that more for contextual operations. They do not cover this in the written design guidelines, but I seem to recall seeing it on an Android Design in Action video. For that, you might use your own regular action item in the action bar as a trigger, if the right-hand drawer is for activity-level contextual operations.

Drawer Layout initially showed on the View

Usually the Drawer Layout is hidden on the view until the onTouch or onSwipe in is triggered. I'd like to make the drawer layout initially displayed. I also checked out the SlidingLayer Library but
It is more complicated that the new drawer layout that Android has released. In the image below, the blue lines represents the initial drawer that needs interaction which also triggers the opening and closing of the drawer.
My last resort on this it to have a button, on the view that will handle the interaction, but as much as possible I want to have a normal view on drawer layout.
I have seen this on the documentation, not sure what it does:
setDrawerShadow(Drawable shadowDrawable, int gravity)
Set a simple drawable used for the left or right shadow.

Categories

Resources