Navigation drawer on all activities - android

I have two activities the first one is 'Navigation drawer activity' (MainActivity) and the second activity I have created is a 'Tabbed activity' (OverView). How can I add Navigation drawer menu on Tabbed activity.
Your help and suggestion are very helpful.
Thanks
Rao
activity_main.xml
<?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">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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>
app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true"
tools:context="com.raoburugula.mytable.MainActivity">
<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.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.raoburugula.mytable.MainActivity"
tools:showIn="#layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#android:drawable/sym_def_app_icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio#android.com" />
</LinearLayout>
MainActivity.java
package com.raoburugula.mytable;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton)
findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.overview) {
Intent overviewIntent = new Intent(getApplicationContext(),
OverView.class);
startActivity(overviewIntent);
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Overview activity where I want navigation menu is
activity_over_view.xml
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.raoburugula.mytable.OverView">
<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/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:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
fragment_over_view.xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.raoburugula.mytable.OverView$PlaceholderFragment">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
OverView.Java
package com.raoburugula.mytable;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class OverView extends AppCompatActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_over_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new
SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton)
findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_over_view, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_over_view,
container, false);
TextView textView = (TextView)
rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format,
getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class
below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
}

Right click anywhere in your project tab in left sidebar, choose New -> Activity -> Navigation Drawer Activity
then modify content_main.xml, add the following
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_pager">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/tab_layout"/>
</android.support.v4.view.ViewPager>

You are done well so far except one thing. Instead of using Intent overviewIntent = new Intent(getApplicationContext(), OverView.class); you need to create OverView class as fragment and attache it existing MainActivity. You are done.

Related

Using AndroidStudio emulation On_Click Events within fragments firing when using tab and enter key but not mouse click

I have a problem within my code that seems to me as it is working but only in the given situation where On_Click Events within fragments are firing when using tab and enter key but not mouse click. I am using the emulator through android studio SDK23 for the emulater. When I test it on my phone sdk26 it does not allow the tap to click anything within the fragment. I feel like I may be missing something very simple.
Here is my Activity:
package com.wgu.c196.testproject;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import com.wgu.c196.testproject.database.TermDatabase;
import com.wgu.c196.testproject.fragment.TestFragment;
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener,
TestFragment.OnListFragmentInteractionListener{
private DrawerLayout drawer;
private FloatingActionButton fab;
private TextView fragTitle;
public DrawerLayout getDrawer() {
return drawer;
}
public void setDrawer(DrawerLayout drawer) {
this.drawer = drawer;
}
public TextView getFragTitle() {
return fragTitle;
}
public void setFragTitle(TextView fragTitle) {
this.fragTitle = fragTitle;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Fragment testFragment = new TestFragment();
replaceFragment(testFragment );
fragTitle = findViewById(R.id.fragment_title);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
item.setChecked(true);
drawer.closeDrawers();
fab.show();
switch(item.getItemId()) {
case R.id.nav_terms:
case R.id.nav_courses:
case R.id.nav_assessment:
case R.id.nav_scheduler:
case R.id.nav_alerts:
case R.id.nav_share:
case R.id.nav_send:
default:
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onDestroy(){
TermDatabase.destroyInstance();
super.onDestroy();
}
public void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
// Begin the transaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_main_placeholder, fragment, fragment.toString());
fragmentTransaction.addToBackStack(fragment.toString());
fragmentTransaction.commit();
}
#Override
public void onListFragmentInteraction(View view) {
}
}
My Fragment:
package com.wgu.c196.testproject.fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.wgu.c196.testproject.MainActivity;
import com.wgu.c196.testproject.R;
public class TestFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
private View mView;
public View getView() {
return mView;
}
public void setView(View mView) {
this.mView = mView;
}
ragment (e.g. upon screen orientation changes).
*/
public TestFragment() {
}
// TODO: Customize parameter initialization
#SuppressWarnings("unused")
public static TestFragment newInstance(int columnCount) {
TestFragment fragment = new TestFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_test, container, false);
((MainActivity) mView.getContext()).getFragTitle().setText("Test Button");
Button btn = mView.findViewById(R.id.d_button_id);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Something clicked me in Fragment!", Toast.LENGTH_SHORT).show();
}
});
btn.setOnTouchListener(new View.OnTouchListener() {
#Override public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(v.getContext(), "Something touched me in Fragment!", Toast.LENGTH_SHORT).show();
return false;
}
});
return mView;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
#Override
public void onDestroy(){
super.onDestroy();
}
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(View view);
}
}
My main layout activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/top_margin"
>
<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.AppBarLayout>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/global_top_margin"
android:theme="#style/AppTheme.AppBarOverlay">
<TextView
android:id="#+id/fragment_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textSize="14sp"
android:textStyle="bold" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content_main_placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/main_fragment_top_margin"
android:clickable="true"
android:scrollbars="vertical"
></FrameLayout>
<include layout="#layout/nav_menu_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#drawable/ic_action_add_entity"/>
</android.support.design.widget.CoordinatorLayout>
My fragment layout fragment_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context=".MainActivity"
android:id="#+id/term_list_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:clickable="true">
<Button
android:id="#+id/d_button_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:text="My Button to Click"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:textAppearance="?android:textAppearanceLarge" />
</LinearLayout>
Ultimately I fixed my own problem. Which was started by choosing Navigation Activity when first starting my project.
I removed
<include layout="#layout/nav_menu_main" />
from my activity_main.xml then restructured it whith the xml from nav_menu_main, using DrawerLayout as the Parent and CoordinatorLayout from activity_main.xml as the child.
I consulted this stack Overflow which lead me to my fix.
Drawer Layout Overlapping | Covering complete space in Fragment
My new activity_main.xml
<?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"
app:layout_scrollFlags="scroll|enterAlways"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<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"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/top_margin"
>
<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.AppBarLayout>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/global_top_margin"
android:theme="#style/AppTheme.AppBarOverlay">
<TextView
android:id="#+id/fragment_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textSize="14sp"
android:textStyle="bold" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content_main_placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/main_fragment_top_margin"
android:clickable="true"
android:scrollbars="vertical"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#drawable/ic_action_add_entity"/>
</android.support.design.widget.CoordinatorLayout>
<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>

Unable to Get Navigation Drawer Items to Display Items Correctly

I have got a working Navigation drawer in a blank project, however whenever I try to implement the navigation drawer in to an existing project, it doesn't seem to be working properly.
The navigation drawer displays fine, however, when items inside are selected, it doesn't seem to redirect to the fragment selected.
I have tried using many resources, however, I am still unable to progress.
Thanks in advance.
Here is my code below:
NavigationDrawer Class
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
public class NDrawer extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,
About.OnFragmentInteractionListener, Home.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Fragment fragment;
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment;
if (id == R.id.nav_home) {
fragment = new Home();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.detach(fragment);
ft.attach(fragment);
ft.commit();
} else if (id == R.id.nav_rooms) {
} else if (id == R.id.nav_account) {
} else if (id == R.id.nav_settings) {
} else if (id == R.id.nav_help) {
} else if (id == R.id.nav_about) {
fragment = new About();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.detach(fragment);
ft.attach(fragment);
ft.commit();
} else if (id == R.id.nav_signout) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
About Fragment Class
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
* Activities that contain this fragment must implement the
* {#link About.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {#link About#newInstance} factory method to
* create an instance of this fragment.
*/
public class About extends Fragment {
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public About() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment About.
*/
public static About newInstance(String param1, String param2) {
About fragment = new About();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_about, container, false);
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
activity_main.xml
<?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">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context=".UserActivity">
<TextView
style="#style/Base.TextAppearance.AppCompat.Headline"
android:id="#+id/displayUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/listen_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Listen"
android:onClick="buttonClicked"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/textView"
/>
<TextView
android:id="#+id/status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<TextView
style="#style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/signed_in_header" />
<Button android:id="#+id/btn_settings"
android:text="#string/settings"
android:layout_marginTop="40dp"
android:layout_width="190dp"
android:layout_height="45dp"
android:onClick="buttonClicked" />
<Button android:id="#+id/btn_sign_out"
android:text="#string/sign_out"
android:layout_marginTop="40dp"
android:layout_width="190dp"
android:layout_height="45dp"
android:onClick="buttonClicked" />
<TextView
android:id="#+id/displayUsername2"
android:layout_width="242dp"
android:layout_height="34dp"
android:padding="5dp"
android:layout_margin="5dp"/>
</LinearLayout>
<include
layout="#layout/app_bar_nav"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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_nav"
app:menu="#menu/activity_nav_drawer" />
</android.support.v4.widget.DrawerLayout>
activity_nav_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
+6 <group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home"
android:checked="true"
android:title="Home" />
<item
android:id="#+id/nav_rooms"
android:icon="#drawable/ic_room_black"
android:title="All Rooms" />
<item
android:id="#+id/nav_account"
android:icon="#drawable/ic_account_box_black"
android:title="Account" />
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings_black"
android:title="Settings" />
<item
android:id="#+id/nav_help"
android:icon="#drawable/ic_help_black"
android:title="Help" />
<item
android:id="#+id/nav_about"
android:title="About" />
<item
android:id="#+id/nav_signout"
android:icon="#drawable/ic_input_black"
android:title="Logout" />
</group>
</menu>
content_main.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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".NDrawer"
tools:showIn="#layout/app_bar_nav">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"
android:orientation="horizontal" />
</android.support.constraint.ConstraintLayout>
app_bar_nav.xml
<?xml version="1.0" encoding="utf-8"?>
<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="aviraj.firebaseapp.NDrawer">
<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.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
But in all honesty - have one more try at this and implement the navigation drawer class from scratch from a template class and put our other main clases in method by method to see any possible conflicts.

Switching between fragments with a button in default Navigation Drawer template of Android Studio

I'm using default Navigation Drawer in Android Studio, and I added two fragments for first two button in navigation drawer. Everything works fine and I can switch between these fragments from drawer menu.
But I want to have a simple button in first fragment that clicking on it take me to second fragment. Clicking on such button opens second fragment but it seems to overlap whole screen and disappear the navigation view.
These are my project's codes:
MainActivity.java :
package com.example.arantik.test4;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment=null;
if (id == R.id.nav_camera) {
fragment=new Fragment_1();
} else if (id == R.id.nav_gallery) {
fragment=new Fragment_2();
}
android.support.v4.app.FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainFrame, fragment);
transaction.addToBackStack(null);
transaction.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
activity_main.xml :
<?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">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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>
content_main.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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.arantik.test4.MainActivity"
tools:showIn="#layout/app_bar_main">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainFrame">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Fragment_1.java :
package com.example.arantik.test4;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Fragment_1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_1_layout, container, false);
Button button=(Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment_2 fragment_2=new Fragment_2();
fragmentTransaction.replace(android.R.id.content, fragment_2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
}
fragmen_1_layout.xml :
<?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="wrap_content"
android:id="#+id/textView"
android:text="First Fragment"
android:textSize="20sp"/>
<Button
android:layout_width="100dp"
android:layout_height="60dp"
android:id="#+id/button"
android:text="click"/>
</LinearLayout>
Fragment_2.java :
package com.example.arantik.test4;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment_2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_2_layout, container, false);
return view;
}
}
fragmen_2_layout.xml :
<?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"
android:background="#00ff00">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:text="Second Fragment"
android:textSize="20sp"/>
</LinearLayout>
I've not changed other Java or XML codes.
Add a public method to MainActivity, cast getActivity() to MainActivity in your fragment, and then you can invoke the method. You can also use interface, refer Communicating with Other Fragments

No view found for id 0x7f0c0071

I really tried to find something here that solves this problem but I didn't find anything...
MainActivity.java:
package de.thejakekols.brunoflo.teamgennewsql;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
SQLHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
db = new SQLHelper(this);
addItemsFromDb();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.addUserMenu) {
// AddUser addStudent = new AddUser();
// FragmentManager manager = getSupportFragmentManager();
// manager.beginTransaction().replace(R.id.content_main, addStudent).addToBackStack(null).commit();
getSupportFragmentManager().beginTransaction().add(R.id.content_main, new AddClass()).commit();
return true;
}else if(id == R.id.addClassMenu){
AddClass addClass = new AddClass();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content_main, addClass).addToBackStack(null).commit();
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void addItemsFromDb() {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
final Menu menu = navigationView.getMenu();
ArrayList<String> klassen = db.getAllKlassen();
for (int i = 0; i < klassen.size(); i++) {
menu.add(klassen.get(i));
}
// refreshing navigation drawer adapter
for (int i = 0, count = navigationView.getChildCount(); i < count; i++) {
final View child = navigationView.getChildAt(i);
if (child != null && child instanceof ListView) {
final ListView menuView = (ListView) child;
final HeaderViewListAdapter adapter = (HeaderViewListAdapter) menuView.getAdapter();
final BaseAdapter wrapped = (BaseAdapter) adapter.getWrappedAdapter();
wrapped.notifyDataSetChanged();
}
}
}
}
And my activity_main.xml
<?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">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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>
And my app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true"
tools:context="de.thejakekols.brunoflo.teamgennewsql.MainActivity">
<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.AppBarLayout>
<include layout="#layout/content_main"
android:id="#+id/include" />
</android.support.design.widget.CoordinatorLayout>
And my content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="de.thejakekols.brunoflo.teamgennewsql.MainActivity"
tools:showIn="#layout/app_bar_main">
</RelativeLayout>
And i'll only provide my AddUser.java, because the AddClass.java is nearly the same:
package de.thejakekols.brunoflo.teamgennewsql;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
/**
* A simple {#link Fragment} subclass.
*/
public class AddUser extends Fragment {
public AddUser() {
// Required empty public constructor
}
EditText vorname, nachname;
Button addstudent;
Spinner spinner;
SQLHelper db;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_add_user, container, false);
vorname = (EditText) view.findViewById(R.id.vorname);
nachname = (EditText) view.findViewById(R.id.nachname);
addstudent = (Button) view.findViewById(R.id.addStudent);
spinner = (Spinner) view.findViewById(R.id.spinner);
db = new SQLHelper(getActivity());
ArrayList<String> spinnerArray = db.getAllKlassen();
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
addstudent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vorname.setText("");
nachname.setText("");
if(db.addStudent(spinner.getSelectedItem().toString(), vorname.getText().toString(), nachname.getText().toString()) == true){
Toast.makeText(getContext(), "Der Benutzer " + vorname.getText().toString() + " " + nachname.getText().toString() + " wurde erfolgreich der Klasse " + spinner.getSelectedItem().toString() + " hinzugefügt", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getContext(), "Da ist was schief gelaufen!", Toast.LENGTH_LONG).show();
}
}
});
return view;
}
}
my fragment_adduser.xml:
<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="de.thejakekols.brunoflo.teamgennewsql.AddUser"
android:id="#+id/addStudentFragment">
<!-- TODO: Update blank fragment layout -->
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/vorname"
android:hint="Vorname" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="#+id/vorname"
android:layout_alignParentStart="true"
android:id="#+id/nachname"
android:hint="Nachname" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/nachname"
android:layout_alignParentStart="true"
android:id="#+id/spinner" />
<Button
android:text="Schüler hinzufügen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner"
android:layout_alignParentEnd="true"
android:id="#+id/addStudent" />
</RelativeLayout>
I also tried it with a fresh fragment but it didn't worked aswell :(
Logcat Log:
12-1012:16:12.4904371-4371/de.thejakekols.brunoflo.teamgennewsqlE/FragmentManager:Noviewfoundforid0x7f0c0071(de.thejakekols.brunoflo.teamgennewsql:id/content_main)forfragmentAddClass{560ed4#0id=0x7f0c0071}
12-1012:16:12.4904371-4371/de.thejakekols.brunoflo.teamgennewsqlE/FragmentManager:Activitystate:
12-1012:16:12.4914371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:LocalFragmentActivity3469381State:
12-1012:16:12.4914371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mCreated=truemResumed=truemStopped=falsemReallyStopped=false
12-1012:16:12.4914371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mLoadersStarted=true
12-1012:16:12.4914371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:ActiveFragmentsin921187d:
12-1012:16:12.4914371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:#0:AddClass{560ed4#0id=0x7f0c0071}
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mFragmentId=#7f0c0071mContainerId=#7f0c0071mTag=null
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mState=1mIndex=0mWho=android:fragment:0mBackStackNesting=1
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mAdded=truemRemoving=falsemFromLayout=falsemInLayout=false
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mHidden=falsemDetached=falsemMenuVisible=truemHasMenu=false
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mRetainInstance=falsemRetaining=falsemUserVisibleHint=true
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mFragmentManager=FragmentManager{921187dinHostCallbacks{63bb372}}
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mHost=android.support.v4.app.FragmentActivity$HostCallbacks#63bb372
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:AddedFragments:
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:#0:AddClass{560ed4#0id=0x7f0c0071}
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:BackStackIndices:
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:#0:BackStackEntry{44453c3#0}
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:FragmentManagermiscstate:
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mHost=android.support.v4.app.FragmentActivity$HostCallbacks#63bb372
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mContainer=android.support.v4.app.FragmentActivity$HostCallbacks#63bb372
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mCurState=5mStateSaved=falsemDestroyed=false
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:ViewHierarchy:
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:com.android.internal.policy.PhoneWindow$DecorView{e6f4440V.E........0,0-1080,1920}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.widget.LinearLayout{5abbf79V.E........0,0-1080,1794}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.view.ViewStub{f0d94beG.E........0,0-0,0#10203a9android:id/action_mode_bar_stub}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.widget.FrameLayout{50e691fV.E........0,0-1080,1794}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.FitWindowsLinearLayout{824646cV.E........0,0-1080,1794#7f0c0059app:id/action_bar_root}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.ViewStubCompat{59fda35G.E........0,0-0,0#7f0c005aapp:id/action_mode_bar_stub}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.ContentFrameLayout{38a3ecaV.E........0,0-1080,1794#1020002android:id/content}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v4.widget.DrawerLayout{b3a783bVFED....F..0,0-1080,1794#7f0c006dapp:id/drawer_layout}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.design.widget.CoordinatorLayout{1295b58V.ED.......0,0-1080,1794}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.design.widget.AppBarLayout{7c6f7baV.E........0,63-1080,210}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.Toolbar{680e4b1V.E........0,0-1080,147#7f0c006fapp:id/toolbar}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.AppCompatTextView{c757d96V.ED.......189,38-596,109}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.AppCompatImageButton{24e5d17VFED..C....0,0-147,147}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.ActionMenuView{284d504V.E........975,0-1080,147}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.ActionMenuPresenter$OverflowMenuButton{8e1aedVFED..C....0,10-105,136}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.widget.RelativeLayout{30e566bV.E........0,210-1080,1794#7f0c0070app:id/include}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.design.widget.NavigationView{903dd22I.E........-735,0-0,1794#7f0c006eapp:id/nav_view}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.design.internal.NavigationMenuView{e51b3b3VFED.V.....0,0-735,1794#7f0c0077app:id/design_navigation_view}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.widget.LinearLayout{cfc3d70V.E........0,0-735,441#7f0c0076app:id/navigation_header_container}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.widget.LinearLayout{6be78e9V.E........0,0-735,420}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.AppCompatImageView{526a96eV.ED.......42,66-168,234#7f0c0084app:id/imageView}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.AppCompatTextView{229d80fV.ED.......42,234-693,327}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.AppCompatTextView{63fc09cV.ED.......42,327-519,378#7f0c0085app:id/textView}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.view.View{a7cbaa5V.ED.......0,1794-1080,1920#1020030android:id/navigationBarBackground}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/AndroidRuntime:ShuttingdownVM
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlE/AndroidRuntime:FATALEXCEPTION:main
Process:de.thejakekols.brunoflo.teamgennewsql,PID:4371
java.lang.IllegalArgumentException:Noviewfoundforid0x7f0c0071(de.thejakekols.brunoflo.teamgennewsql:id/content_main)forfragmentAddClass{560ed4#0id=0x7f0c0071}
atandroid.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102)
atandroid.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
atandroid.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
atandroid.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
atandroid.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
atandroid.os.Handler.handleCallback(Handler.java:739)
atandroid.os.Handler.dispatchMessage(Handler.java:95)
atandroid.os.Looper.loop(Looper.java:148)
atandroid.app.ActivityThread.main(ActivityThread.java:5417)
atjava.lang.reflect.Method.invoke(NativeMethod)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-1012:16:12.4941555-1962/system_processW/ActivityManager:Forcefinishingactivityde.thejakekols.brunoflo.teamgennewsql/.MainActivity
12-1012:16:12.4981214-1616/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x333implycreationofhostcolorbuffer
[12-1012:16:12.5011555:1962D/]
HostConnection::get()NewHostConnectionestablished0x7fc54b5b7360,tid1962
12-1012:16:12.5111255-1621/?D/AudioFlinger:mixer(0xf44c0000)throttleend:throttletime(55)
12-1012:16:12.5131214-1214/?E/EGL_emulation:tid1214:eglCreateSyncKHR(1660):error0x3004(EGL_BAD_ATTRIBUTE)
12-1012:16:12.5771555-1962/system_processD/gralloc_ranchu:gralloc_unregister_buffer:exitingHostConnection(isbuffer-handlingthread)
12-1012:16:12.6711555-1606/system_processI/OpenGLRenderer:InitializedEGL,version1.4
12-1012:16:12.6881214-1617/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x900implycreationofhostcolorbuffer
12-1012:16:12.6941555-1606/system_processE/EGL_emulation:tid1606:eglSurfaceAttrib(1165):error0x3009(EGL_BAD_MATCH)
12-1012:16:12.6941555-1606/system_processW/OpenGLRenderer:FailedtosetEGL_SWAP_BEHAVIORonsurface0x7fc54b53fac0,error=EGL_BAD_MATCH
12-1012:16:12.6961214-1329/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x900implycreationofhostcolorbuffer
12-1012:16:12.6981214-1329/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x900implycreationofhostcolorbuffer
12-1012:16:13.0841555-1569/system_processW/ActivityManager:ActivitypausetimeoutforActivityRecord{32c16b2u0de.thejakekols.brunoflo.teamgennewsql/.MainActivityt338f}
12-1012:16:13.1171214-1579/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x900implycreationofhostcolorbuffer
12-1012:16:13.1251950-2080/com.android.launcher3E/EGL_emulation:tid2080:eglSurfaceAttrib(1165):error0x3009(EGL_BAD_MATCH)
12-1012:16:13.1251950-2080/com.android.launcher3W/OpenGLRenderer:FailedtosetEGL_SWAP_BEHAVIORonsurface0x7fc5574928c0,error=EGL_BAD_MATCH
12-1012:16:13.1261214-1329/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x900implycreationofhostcolorbuffer
12-1012:16:13.1311214-1329/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x900implycreationofhostcolorbuffer
12-1012:16:13.6661950-2080/com.android.launcher3W/OpenGLRenderer:IncorrectlycalledbuildLayeronView:ShortcutAndWidgetContainer,destroyinglayer...
12-1012:16:13.6661950-2080/com.android.launcher3W/OpenGLRenderer:IncorrectlycalledbuildLayeronView:ShortcutAndWidgetContainer,destroyinglayer...
12-1012:16:14.8624371-4371/de.thejakekols.brunoflo.teamgennewsqlI/Process:Sendingsignal.PID:4371SIG:9
12-1012:16:14.8781555-1606/system_processE/Surface:getSlotFromBufferLocked:unknownbuffer:0x7fc548597ce0
12-1012:16:14.8871555-1708/system_processE/JavaBinder:!!!FAILEDBINDERTRANSACTION!!!(parcelsize=104)
12-1012:16:14.8871555-1708/system_processW/InputMethodManagerService:GotRemoteExceptionsendingsetActive(false)notificationtopid4371uid10041
12-1012:16:14.8891555-1708/system_processE/JavaBinder:!!!FAILEDBINDERTRANSACTION!!!(parcelsize=104)
12-1012:16:14.9081255-1621/?D/AudioFlinger:mixer(0xf44c0000)throttleend:throttletime(11)
12-1012:16:14.9221555-1625/system_processD/GraphicsStats:Buffercount:3
12-1012:16:14.9221555-1917/system_processI/WindowState:WINDEATH:Window{7880644u0de.thejakekols.brunoflo.teamgennewsql/de.thejakekols.brunoflo.teamgennewsql.MainActivity}
12-1012:16:14.9221555-1917/system_processW/WindowManager:Force-removingchildwinWindow{662c9bau0PopupWindow:81e55c2}fromcontainerWindow{7880644u0de.thejakekols.brunoflo.teamgennewsql/de.thejakekols.brunoflo.teamgennewsql.MainActivity}
12-1012:16:14.9261555-1625/system_processW/WindowManager:Failedlookingupwindow
java.lang.IllegalArgumentException:Requestedwindowandroid.os.BinderProxy#408fae5doesnotexist
atcom.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:8733)
atcom.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:8724)
atcom.android.server.wm.WindowState$DeathRecipient.binderDied(WindowState.java:1209)
atandroid.os.BinderProxy.sendDeathNotice(Binder.java:558)
12-1012:16:14.9261555-1625/system_processI/WindowState:WINDEATH:null
I'm new into Android :)
The ID specified on an <include> tag overrides the ID of the root View in the inflated layout. This means that the RelativeLayout you're using to hold your Fragment's View does not have ID content_main after inflation. It has ID include, so the FragmentManager can't find the ViewGroup for the ID specified in the FragmentTransaction.
You can see this in your hierarchy listing:
android.widget.RelativeLayout{30e566b V.E..... ... 0,210-1080,1794 #7f0c0070 app:id/include}
Simply remove android:id="#+id/include" from the <include> tag in the app_bar_main layout.
I thing you have done mistake in the code
final View view = inflater.inflate(R.layout.fragment_add_user, container, false);
Please check the inflated layout is correct.

Navigation Drawer doesn't show fragments

I've created a new Android Studio Project and my MainActivity is a Navigation Drawer Activity.
So, I can't show up fragments. I've read many post on internet and here too.
Explaining:
I open navigation drawer, select menu "Podcast".
PodcastsFragment should be shown, but it still showing activity.
MainActivity code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private Tracker mTracker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// [START shared_tracker]
// Obtain the shared Tracker instance.
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
// [END shared_tracker]
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
protected void onResume() {
Log.i("TESTE", "Setting screen name: " + "teste");
mTracker.setScreenName("Main " + "teste");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
super.onResume();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
FragmentTransaction ft = null;
if (id == R.id.nav_podcasts) {
Log.d("Clicked:", "nav_podcasts");
fragment = new PodcastsFragment();
} else if (id == R.id.nav_feed) {
//fragment = new FeedFragment();
Log.d("Clicked:", "nav_feed");
//fragment = new FeedFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FeedFragment ff = new FeedFragment();
fragmentTransaction.add(R.id.container_body, ff);
fragmentTransaction.commit();
} else if (id == R.id.nav_downloads) {
//fragment = new DownloadsFragment();
Log.d("Clicked:", "nav_downloads");
} else if (id == R.id.nav_add) {
//fragment = new OPMLFragment();
Log.d("Clicked:", "nav_add");
} else if (id == R.id.nav_settings) {
//fragment = new SettingsFragment();
Log.d("Clicked:", "nav_settings");
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container_body, fragment).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
PodcastsFragment code:
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PodcastsFragment extends Fragment {
public PodcastsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//return inflater.inflate(R.layout.fragment_podcasts, container, false);
View rootView = inflater.inflate(R.layout.fragment_podcasts, container, false);
// Inflate the layout for this fragment
return rootView;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
}
app_bar_main code:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true"
tools:context="com.test.test.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
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.AppBarLayout>
<include layout="#layout/content_main"/>
<!--<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />-->
</android.support.design.widget.CoordinatorLayout>
activity_main code:
<?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">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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>
content_main code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main"
tools:context="com.test.test.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="teste"/>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</RelativeLayout>
Make the drawer's list in Your activity and add setOnItemClickListener().When the user selects an item in the drawer's list, the system calls onItemClick() on the OnItemClickListener given to setOnItemClickListener().
What you do in the onItemClick() method depends on how you've implemented your app structure. In the following example, selecting each item in the list inserts a different Fragment into the main content view (the FrameLayout element identified by the R.id.content_frame ID):
for more delail please read these articles youtube and github example
Latest - If you use Navigation Architecture Component, then you don't have to manually connect your fragments to your drawer MenuItem. The system will automatically tie you MenuItem to specific fragment If the id of the MenuItem in your menu/xyz_menu.xml matches the id of the destination in navigation/xyz_nav.xml, the NavController can then navigate to that destination.
Follow the correct steps mentioned in the official documentation : https://developer.android.com/guide/navigation/navigation-ui#Tie-navdrawer
Note :
They have a sample app linked to the same article which you can refer.
To get this is effect, do not implement onNavigationItemSelected otherwise it will override the system let you handle the navigation.

Categories

Resources