I am new to android and I am going to design this kind of layout that is complex to me. Here's the code of main_activity.xml file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/home_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/menu_drawer"/>
</android.support.v4.widget.DrawerLayout>
Basically I have in my main activity a Navigation drawer and 3 tabs. Here's a picture:
Everything works as I want and I'm happy.
When I click on a navigation drawer item I start a new intent to open a new activity and it's good but the drawer disappears of course (becase the new activity does NOT have the drawer). I want to keep the drawer in the screen always.
To keep the drawer on my screen I thought that I could have placed the TabLayout of the above XML as a fragment; in this way the drawer would still be there. But how can I do this? How can I place the TabLayout in a fragment so that I can replace this fragment with TabLayout or other fragments?
I think that I could put a FrameLayout instead of TabLayout but I do not know if it's good idea and how would it work. Any help?
There are similar questions like this but they haven't helped me because I'm still stuck here
More. If you click on results, the ThisSeason activity is launched and it has some fragments inside (inclusing ResultsFragments). That works, but there is no navigation drawe anymore and I have to press the back button!
I'll give you a general idea for implementing this.
Make a base activity for your application.
Then make fragment for each activity including the screen which has tabs. So, if you have 5 items in nav view then total of 6 fragments will be created.
Add the nav view and related functions in the main activity xml and java file. This will basically add navigation view to the base activity and since you're using fragments, all the pages(fragments) will have the navigation view.
Additional Note: You can make multiple subclasses to handle specific functions, like one for handling actionMode, navigation view and updating the title in the toolbar. You get the point.
Also, all the features which are common to all the fragments should be added in the base activity only instead of repeating the same code everywhere.
Related
I recently implemented a navigation drawer into the main screen of my app. For some reason a small grey (or transparent black) bar is being rendered on top of it.
Screenshot:
Layout code:
<android.support.design.widget.CoordinatorLayout
... >
<LinearLayout
... >
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout">
<fragment
... />
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/navigation"
android:layout_gravity="start">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list_nav"/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
... />
</android.support.design.widget.CoordinatorLayout>
The Java portion is just your generic list-filling stuff. Let me know if you need more code.
How can I remove this weird bar?
For me, it worked to add
app:insetForeground="#color/transparent"
to the NavigationView's XML definition.
NavigationView is a very specialized ViewGroup that builds its own internal structure from provided resources. It's not meant to be used as a regular ViewGroup. That is, it's not meant to have child Views added directly to it, either in layout XML, or in code.
This is not immediately apparent, though, since no fatal error will occur if you do add children to it manually. It is further muddled by the fact that Android Studio's recent Navigation Drawer templates use NavigationView as the default drawer View in the DrawerLayout, with no indication that it is not mandatory that the drawer be a NavigationView. A drawer can be virtually any kind of View or ViewGroup.
In this case, the shadow is apparently coming from something internal to the NavigationView. However, since it's not being used for any of its specialized features, the NavigationView can be removed completely, and the ListView can act as the drawer on its own.
Simply remove the <NavigationView> element, and any code associated with it. To setup the ListView as the drawer, set its layout_gravity attribute to start, and set its layout_width to an exact value; e.g., 240dp.
i add in my activity_main.xml line:
<com.google.android.material.navigation.NavigationView
...
app:insetForeground="#android:color/transparent" />
and it works fine!
I'm using android navigation drawer menu. I want to show navigation drawer in my all activities class. If i want to use it what i need to do actually. If anyone give tips it'll very helpful.
Make a BaseActivity Activity, which will be extended by every other Activity.
The layout of the BaseActivity will be a DrawerLayout, that contains a FrameLayout and the Navigation Drawer. It will look something like this (In this case, the Navigation Drawer is a RecyclerView) -
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/base_frame"/>
<!-- Side navigation drawer UI -->
<android.support.v7.widget.RecyclerView
android:id="#+id/nav_drawer_rv"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
Make the FrameLayout a protected class field, and in every Activity that extends the BaseActivity, inflate the layout as such -
getLayoutInflater().inflate(R.layout.your_activity, baseFrameLayout);
Make the BaseActivity handle all interactions with the Navigation Drawer (selecting items, switching Activities, etc...).
I am new to Android and I want to know how to best develop my app.
My app is going to be only on tablets and I have a navigation menu on the header (not side menu like a NavDrawer but a navigation menu on header like in websites).
The question is if it's better to make a header.xml file that contains all the navigation buttons and include it in every activity, and then in order to handle click events make it inside some base activity that every activity will inherit from.
Or it's better to make the header as a fragment and handle the click events inside the fragment itself.
Thanks.
Current practice is to use a Toolbar with either a navigation tab with a spinner or Tabs/Icons for each of your views. If you don't want to build this yourself you can use the TabbedActivity template when you set up your project.
The way it works is it's a Toolbar(new version of the ActionBar) included on the top of an Activity that has a fragment for the rest of the view. You can use a ViewPager or a FragmentManager to change the content of the fragment as the user makes his/her selection.
Here is the main activity of an app I built like this recently:
<RelativeLayout
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"
tools:context=".MainActivity"
android:background="#dfdfdf">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:layout_below="#id/tool_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/tabs"
/>
</RelativeLayout>
and the toolbar looks something like this:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:elevation="6dp"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>
my primary goal is to have a navigation drawer with a Scrolling Activity while implementing material design so it would look something like a combination of these two:
Navigation Drawer Sample Image (Credits to Google)
Scrolling Activity Sample Image (Credits to Google)
As of right now I am doing this by associating a Navigation Drawer Layout's xml to my main activity and then including a Scrolling Layout's xml inside of Navigation Drawer Layout's content xml.
It's working after implementing many small changes in xmls of both the layouts but I don't feel like it's the optimal way to do so as it results in using more than 7 xmls at once for only one activity.
Does anybody know of a better way to do so?
You have to include scrolling layout into navigation layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="#layout/activity_scrolling"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
I'm looking for best practices when it comes to the following pretty common setup:
What I'm currently doing is:
My Main Activity has a navigation drawer and a Toolbar.
When you click on one of the list items inside the drawer a fragment is loaded under the toolbar. Some fragments have their own tabs inside using viewpager ect.
I would like to make use of the material functionalities like hiding the toolbar while scrolling ect. It is no problem to get it done.
But there is a problem when only some of your fragments would like to make use of the scrolling toolbar. Using a normal Scrollview does solve the problem if you can add an additional margin at the bottom, but once you have a keyboard it will mess up as the scroll is wrong. Using a nestedscrollview works but does scroll the toolbar.
So how can I avoid the scrolling toolbar when my activity holds the toolbar and some fragments that scroll should make use of it and some not?
Is the architectural design wrong? Another thing I thought is to have the different toolbars inside of each fragment... but they have to share the same navigation drawer so you have to create and add the ToggleButton each time? Is it the right approach ? Should I go that path? I'm not convinced and would like to have the opinion of somebody more experienced then me. What's the best solution in this case.
Appreciate your help!
Cheers
EDIT:
Main Activity
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container_first"
>
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:id="#+id/owner_main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/appBar"
android:orientation="horizontal"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/addicon"
app:fabSize="normal"
app:layout_anchor="#id/owner_main_container"
app:layout_anchorGravity="bottom|right|end"
app:layout_behavior="xxx.Classes.Misc.ScrollAwareFABBehavior"
/>
</android.support.design.widget.CoordinatorLayout>
<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawerhost" />
I am also Using navigation drawer and tool bar please import project from this link
And try to understand how to use navigation drawer and tool bar using fragment