Fragments are not getting replaced in the main_activity() - java

I am trying to make navigation drawer using fragments using the reference link : https://guides.codepath.com/android/Fragment-Navigation-Drawer
But the problem is when i tap on any of the item in navigation drawer it does not replace the corresponding fragment to the main activity layout.
Main activity snippet:
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the fragment to show based on nav item clicked
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_first_fragment:
//fragmentClass = FirstFragment.class;
fragment=new FirstFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.flContent, fragment).commit();
break;
case R.id.nav_second_fragment:
//fragmentClass = SecondFragment.class;
fragment=new SecondFragment();
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.flContent, Fragment.instantiate(MainActivity.this, "com.example.FirstFragment"));
tx.commit();
break;
default:
fragmentClass = FirstFragment.class;
}
activity_main layout:
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<LinearLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
/>
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/nav_header"
android:layout_gravity="start"
android:background="#android:color/white"
app:menu="#menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>
fragment one layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment 1"
android:layout_gravity="center"
android:gravity="center"
android:background="#FF5722"/>
</LinearLayout>
frament one java:
public class FirstFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.home, container,false);
if(container==null){
Log.d("First","Null");
return view;
}Log.d("First"," Not Null");
return null;
}
}

use this in switch
switch(menuItem.getItemId()) {
case R.id.nav_first_fragment:
getSupportFragmentManager().beginTransaction().replace(R.id.container, new FirstFragment()).commit();
break;
case R.id.nav_second_fragment:
getSupportFragmentManager().beginTransaction().replace(R.id.container, new SecondFragment()).commit();
break;
}
and make sure you give the correct IDs to the switch. And "container" is the ID for the layout of the activity_main.xml
just give the ID to main Layout like
android:id="#+id/container"

Related

Android Fragment not replacing UI from Activity

I am currently building an android application that uses a navigation drawer. When the user selects an item from the drawer, a fragment is created that corresponds to the item selected. For example, if the user selects the contacts item from the navigation drawer, the UI should change from the dashboard, to the contacts layout.
I am successfully creating fragments, however the Fragment retains the calenderView from my dashboard.
Here is the dashboard layout:
Here is the Dashboard layout
Here is the layout once the user selects timer from the navigation drawer
enter image description here
The problem is that the calenderView remains when the fragment is inflated. Ideally it would just be the "Timer Frag" Button that is visible in this fragment.
Here is my mainActivity code:
//Fragments will be inflated through this method.
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment = null;
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_patient) {
fragment = new Patient_Fragment();
// Handle the camera action
} else if (id == R.id.nav_timer) {
fragment = new Timer_Fragment();
} else if (id == R.id.nav_reminders) {
} else if (id == R.id.nav_contacts) {
} else if (id == R.id.nav_account) {
} else if (id == R.id.nav_about) {
}
if(fragment !=null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.dashboardArea, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Here is my timer Fragment:
public class Timer_Fragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_timer, null);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
Here is the fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/timerFrag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Timer Frag"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.181" />
This is the dashboard XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:id="#+id/dashboardArea"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.a517086.carefree.MainActivity"
tools:showIn="#layout/app_bar_main">
<CalendarView
android:id="#+id/calendarView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
The problem is you are replacing the R.id.dashboardArea (which is in your CalendarView fragment layout), but you need to replace the View in your MainActivity which will hold the fragment. I have called it content_container for this example.
if(fragment !=null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.content_container, fragment);
ft.commit();
}
Example:
I use a DrawerLayout, but you can use just any layout. This is my MainActivity 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"
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"
tools:openDrawer="start">
<RelativeLayout
android:id="#+id/content_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_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>
When I want to swap out my fragments I just get the fragment object I want to be visible and replace the container--In this case "content_container".
You should put your CalendarView in another xml fragment related to the calendar fragment like you did with the timer fragment like this :
fragment_calendar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<CalendarView
android:id="#+id/calendarView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
CalendarFragment.java
public class CalendarFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_calendar, null);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
Your dashboard xml file should be empty like this :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:id="#+id/dashboardArea"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.a517086.carefree.MainActivity"
tools:showIn="#layout/app_bar_main">
</FrameLayout>
Then, in your main activity initialize the calendar fragment just like that :
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(R.id.dashboardArea, CalendarFragment);
ft.commit();

Android : fragment is not replaced

What I want to do is that when I click a button ( BtnA or BtnB) , the fragment just below buttons is replaced by corresponding fragment (FragmentA or FragmentB).
this is my code in MainActivity.java
#Override
public void onClick(View v) {
Fragment replacableFragment = new Fragment();
switch (v.getId()){
case R.id.button1:
{
replacableFragment = new Fragment_One();
Log.d("OnClick","button1");
break;
}
case R.id.button2:
{
replacableFragment = new Fragment_Two();
Log.d("OnClick","button2");
break;
}
}
FragmentManager manager= getFragmentManager();
Log.d("OnClick","1");
FragmentTransaction transaction = manager.beginTransaction();
Log.d("OnClick","2");
transaction.add(R.id.fragmentPlace, replacableFragment);
//transaction.addToBackStack(null);
Log.d("OnClick","3");
transaction.commit();
}
and this is my code of Fragment_One.java
public class Fragment_One extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one,container,false);
}
}
Fragment_Two.java is the same with 'inflater.inflater(R.layout.fragment_two,container,false);'
When I click any button, logcat shows everything exactly. But Fragment is not replaced at all.
I think my code has no problem to function it.
Can anyone know about this?
xml files for my project are below:
activity_main.xml
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FRAGMENT ONE"
android:id="#+id/button1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FRAGMENT TWO"
android:id="#+id/button2"/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragmentPlace"
android:name="lkm.com.practical5_3.Fragment_One"/>
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#android:color/holo_orange_dark"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="THIS IS FRAGMENT ONE"
android:id="#+id/text1"/>
</LinearLayout>
fragment_two.xml
The same one as 'fragment_one.xml'
use this
transaction.replace(R.id.fragmentPlace, replacableFragment).commit();
I want to anser my question!
The problem was the container!
At activiy_main.xml, the container of the fragment ais defined as 'fragment ~/'.
I fix it as 'FrameLayout ~/' and it was succesfful!
Thank you!

Error in inflating map fragment

I have a navigation bar and I am working with fragment, menu1fragment is a map, when I tried to use addtobackstack to save my data I have a problem in inflating any solution
MainActivity
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new menu1_fragment();
title = getString(R.string.title_section1);
break;
case 1:
fragment = new menu2_fragment();
title = getString(R.string.title_section2);
break;
case 2:
fragment = new menu4_myfleet();
title = getString(R.string.title_section3);
break;
case 3:
fragment = new menufragment_contact();
title = getString(R.string.title_section4);
break;
case 4:
fragment = new menu5_Emergency();
title = getString(R.string.title_section5);
break;
case 5:
logoutUser();
break;
default:
break;
}
if (fragment != null) {
String backStateName = fragment.getClass().getName();
FragmentManager fragmentManager = getSupportFragmentManager();
boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped){ //fragment not in back stack, create it.
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.addToBackStack(backStateName);
fragmentTransaction.commit();
}
/* FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();*/
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
This is menu1_fragment that inflate the map:
rootview = inflater.inflate(R.layout.activity_map,container,false);
/*get map fragment and puts it in the container */
FragmentManager fm = getChildFragmentManager();
Fragment mapFragment = fm.findFragmentById(R.id.map);
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.container_body, mapFragment, "myMap")
.commitAllowingStateLoss();
}
//create the fragment that contain your vehicle
mFragment4Container = (FrameLayout) rootview.findViewById(R.id.fragment4Container);
mFragment4Container.setVisibility(View.GONE);
android.support.v4.app.FragmentTransaction trans = getActivity().getSupportFragmentManager().beginTransaction();
trans.add(mFragment4Container.getId(), new menu3_fragment());
trans.commit();
//create a fragmen for marker profile
mFragment4Containeruser = (FrameLayout) rootview.findViewById(R.id.fragment4Containeruser);
mFragment4Containeruser.setVisibility(View.GONE);
android.support.v4.app.FragmentTransaction transprofile = getActivity().getSupportFragmentManager().beginTransaction();
transprofile.add(mFragment4Containeruser.getId(), new user_info());
transprofile.commit();
This is the error that appears:
android.view.InflateException:Binary XML file line #7: Error inflating class fragment at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719) at
android.view.LayoutInflater.rInflate(LayoutInflater.java:761) at
android.view.LayoutInflater.inflate(LayoutInflater.java:498) at
android.view.LayoutInflater.inflate(LayoutInflater.java:398) at
com.example.fcb.insurance.menu1_fragment.onCreateView(menu1_fragment.java:120)
Menu1_fragment `
<fragment 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"
android:id="#+id/map"
tools:context="com.example.fcb.insurance.map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
<FrameLayout
android:id="#+id/fragment4Container"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#E2E2E2"
android:layout_gravity="bottom"
/>
<FrameLayout
android:id="#+id/fragment4Containeruser"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#E2E2E2"
android:layout_gravity="bottom"
/>
<com.rey.material.widget.Button
style="#style/Material.Drawable.Ripple.Wave.Light"
android:layout_width="256dp"
android:layout_height="wrap_content"
android:text="call expert"
android:id="#+id/buttonclose"
android:layout_marginBottom="100dp"
android:layout_gravity="center_horizontal|bottom"
app:rd_enable="true"/>
<com.rey.material.widget.Button
style="#style/Material.Drawable.Ripple.Wave.Light"
android:layout_width="256dp"
android:layout_height="wrap_content"
android:text="call Tow Truck"
android:id="#+id/button_towtruck"
android:layout_marginBottom="50dp"
android:layout_gravity="center_horizontal|bottom"
app:rd_enable="true"/>
</FrameLayout>
`
Main Activity.xml
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fcb.insurance.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.example.fcb.insurance.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer"
/>
</android.support.v4.widget.DrawerLayout>
Since you are adding the SupportMapFragment using XML in the Menu1_fragment.xml like that
android:name="com.google.android.gms.maps.SupportMapFragment"
then you should remove this code from Menu1_fragment.java
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.container_body, mapFragment, "myMap")
.commitAllowingStateLoss();
}
or vice versa.
Edit #1
I also noticed that you are inflating another layout for your menu1_fragment
rootview = inflater.inflate(R.layout.activity_map,container,false);
It should be R.layout.Menu1_fragment instead if R.layout.activity_map
remove android:name from xml declaration, and replace it with:
class="com.google.android.gms.maps.MapFragment"

With a DrawerLayout, how can I close the app on pressing Back no matter which screen is active?

I'm using a DrawerLayout for the navigation of my app:
Taking this menu for the example:
I'd like the Home button to launch the HomeActivity and so on. But clearing the activity stack, that is: If I press People, then Photos, then Locations and then the back button, the app should close. I'm trying intent flags when launching the activities but I always get the same behaviour: People->Photos->Location->Back goes to Photos instead closing the app
How can I achieve that?
As per you said that "If I press People, then Photos, then Locations and then the back button, the app should close".
For achieve that you should user Fragment for Home,People,Location and Photos etc.
and when you press back button of Device you can clear Fragment back stack and finish activity.
I think this code may help you. When you press Back button of a device it will close the application.
In MainActivity.java,
public void SelectItem(int possition)
{
//Using Fragment here
Fragment fragment = null;
Bundle args = new Bundle();
switch (possition)
{
//Can use Fragment for Home,People,Location and Photos etc. and when you press back button of Device you can clear Fragment back stack and finish activity.
case 0:
fragment=new FragmentNewsFeed();
break;
case 1:
fragment = new FragmentMessages();
break;
default:
break;
}
fragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
listView_drawer.setItemChecked(possition, true);
setTitle(dataList.get(possition).getItemName());
drawerLayout.closeDrawer(listView_drawer);
}
In FragmentNewsFeed class,
public class FragmentNewsFeed extends Fragment
{
public FragmentNewsFeed()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_news_feeds, container,false);
return view;
}
}
Whereas you can find content frame in a layout/activity_main.xml,
<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/content_frame"
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="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#ffff"/>
</android.support.v4.widget.DrawerLayout>
In R.layout.fragment_news_feeds,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear_Parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#008B8B"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear_1"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="0.30"
android:background="#2F4F4F"
android:orientation="vertical" >
<ListView
android:id="#+id/listView_OwnItems"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</LinearLayout>

Android Drawer Layout is Displaying under fragment content

I am adding a drawer layout to my Activity which uses fragments. I've implemented this just as described here per the android documentation. And the drawer layout Adapter and View all work in that they show up, but the drawer extends "under" the fragment content. Here is a picture of what is happening:
Does anyone know why this might be? My code for the activity's xml, and the activity add fragment is below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_list); // for drawer navigation
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(android.R.id.content,
new AlarmListFragment());
fragmentTransaction.commit();
setUpDrawer(fragmentManager);
}
private void setUpDrawer(FragmentManager fm) {
String[] drawerItems = getResources().getStringArray(
R.array.drawer_array);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ListView drawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.item_nav_drawer, drawerItems));
// Set the list's click listener
drawerList.setOnItemClickListener(new
DrawerItemClickListener(drawer,drawerList,drawerItems,fm));
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<ListView
android:id="#+id/alarm_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
Do I have to set a z-index somewhere? I can provide any further info needed I'd just really like to figure this out!
I don't what produces this issue , but to solve it u have to add the fragment to the content of your FrameLayout which has the id = content_frame.
So edit this line
fragmentTransaction.replace(android.R.id.content,
new AlarmListFragment());
to be
fragmentTransaction.replace(R.id.content_frame,
new AlarmListFragment());
You can move the drawer layout below your Fragment container in your XML. this will render the DrawerLayout in the forward view of the app. just be sure not to cover the fragment container with the parent container of the drawer so that your fragment will be accessible when you need it.
<RelativeLayout
android:id="#+id/my_parent_container"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity"
xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
android:id="#+id/fragment_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"></FrameLayout>
<android.support.v4.widget.DrawerLayout
android:id="#+id/my_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id=#+id/drawer_pane"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="end" />
<ListView xmlns="http://schemas.android.com/apk/res/android"
android:layout_width="280dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:id="#+id/menu_list"
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

Categories