How to add two fragments together in a frameLayout in runtime - android

I am developing an andorid application with navigation drawer and I have a layout for contentview like this:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="0dp"
android:divider="#android:color/transparent"
android:background="#color/default_color"/>
</android.support.v4.widget.DrawerLayout>
So I can add and replace fragment on navigation drawer item click. Everything okay till now.
But in one case, on clicking an item on navigation drawer I need to show two fragments instead of one ( just like a listFragment and details fragment ). How can I do it? Should I add two fragments into the framelayout then? But how? or should I change the contentView with different drawerLayout ? If yes, how can I do this?

You may just use nested fragments. Create another fragment that will dynamically create both your fragments and add them to its layout.
For more information read this: http://developer.android.com/about/versions/android-4.2.html#NestedFragments

Related

how to set 2 layout on 1 drawer layout?

I have one drawer layout contains one fragment like below:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/fl_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray_light_background_fragment" />
<!-- Drawer view -->
<fragment
android:id="#+id/navigation_drawer"
android:name="ui.fragment.drawer.NavigationDrawerFragment"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="#layout/fragment_navigation_drawer_in_company_feed" />
</android.support.v4.widget.DrawerLayout>
I follow this https://developer.android.com/training/implementing-navigation/nav-drawer.html and can show drawer layout. But my drawer layout has just one view (layout)
I would like drawer layout can show 2 (or many) views (layouts) at many the different contexts. All views (layouts)is left side.
Example: At Activity A: drawer layout show view (layout) A.
At Activity B: drawer layout show view (layout) B.
Someone can help me. I look forward to your advice. Thanks you!

Android Layout fragments

I don't know how to create a footer whereby when the footer is pulled up, options for a music player come up. I am using a swipe tabs layout.
The UI element that you're looking for is the DrawerLayout. All you have to do is make it the top level element of your UI hierarchy, add your views to it, and then select one view to show up when the user slides the finger from the edge of the window.
There's an article in the developer's site about how to create this (here) and this is one of their examples:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
Alternatively you could use a SlidingDrawer to achieve the same result, but note that the class has been deprecated in recent versions of Android.
Consider using - ViewDragHelper:
Doc located here.
Here's similar question someone else asked earlier.

BaseActivity to implement Navigation Drawer and Toolbar in every activity

I created a base activity and base layout to extend those to get same nav drawer and toolbar in all my activities. However, I want to add sliding tab layout in one of my activity. Should I create a new layout for this? Or is there a way I can add this sliding tab layout to my extended base activity dynamically?
activity_base.xml
<include android:id="#+id/app_bar" layout="#layout/app_bar"></include>
<android.support.v4.widget.DrawerLayout
android:layout_below="#+id/app_bar"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#color/itembackground"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>
You can implement the sliding tab in the Fragment which you are loading in the FrameLayout. The sliding tab can be achieved using a ViewPager and the ActionBar.TabListener interface. The ViewPager can hold the tab Fragments in a FragmentStateAdapter and access them using getChildFragmentManager(). This way, there is no need to create a separate layout for the tabbed screen, just extend from BaseActivity and use a Fragment with tabs.
For more on this, see the Creating Swipe Views with Tabs example.

custom layout in navigation drawer

I am new to android, I follow this tutorial to create Navigation Drawer here
I make the navigation drawer successfully but I dont know how to change framelayout to my custom layout with button, textview....My app just show menu and blank framelayout, how can I solve this ?
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="asdsadsad" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/purple_dark"
android:choiceMode="singleChoice"
android:divider="#android:color/darker_gray"
android:dividerHeight="0.1dp"
android:listSelector="#drawable/ic_drawer"/>
</android.support.v4.widget.DrawerLayout>
you can use a RelativeLayout on LinearLayout instead of FrameLayout and you can build your whole layout inside that
If you need to add another layout you need to gie it to your closeDrawerand drawerOpen.
Please refer my answer here

Navigation drawer like Google+

How can I create a navigation drawer like Google+ where there is a header for user profile info and a listview below?
I have the following code in my Activity:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.br.app.MainActivity">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.br.app.NavigationDrawerFragment" />
</android.support.v4.widget.DrawerLayout>
Currently, the NavigationDrawerFragment contains just a listview, but I tried to add a RelativeLayout as a container with an inner RelativeLayout to create the profile area and the listview below, but the app is crashing.
I've read the tutorial from but as I could understand, my header would be an item of the listview, and it won't be fixed on top (without scrolling).
How can I create this custom navigation drawer like Google+?
Actually, there are two version of navigation drawer used by google.
the first is the old navigation drawer BELOW the action bar. It is used in Google Play Store and Google Books for example.
HERE you can find a library that implements it.
The second is the new navigation drawer that is showed in Material Design guidelines where the drawer is over the action bar.
HERE you can find a library that implements it.
Actually these two libraries are in developing. But they are almost usable at this time.
You can easily customize the android Navigation drawer once you know how its implemented. here is a nice tutorial where you can set it up.
This will be the structure of your mainXML:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
</android.support.v4.widget.DrawerLayout>

Categories

Resources