i have used Bottom navigaion with navigation drawer both with fragments - android

I an Working On a project which uses navigation drawer with bottom navigation view
my navigation drawer is working fine but when i click any options from bottom navigation it gets selected but not shows anything in framelayout Here Is My XML File Please Help Me..
none of fragment of bottom navigation drawer are showing
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:orientation="vertical"
tools:context=".Home_Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize">
<FrameLayout
android:id="#+id/framelayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:soundEffectsEnabled="true"
app:itemIconTint="#color/yellow"
app:itemTextColor="#color/blue"
app:menu="#menu/bottom_nav" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_drawer_header"
app:itemHorizontalPadding="40dp"
app:menu="#menu/navigation_menu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#fecd99"
app:navigationIcon="#drawable/baseline_sort_24px" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
And Here Is My Java File
package com.anshdev.anshapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.appcompat.widget.Toolbar;
import com.anshdev.anshapp.Navigation_drawer.about_us_fragment;
import com.anshdev.anshapp.Navigation_drawer.categories_fragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
public class Home_Activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
Fragment fragment = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
/* <-------------------------Hooks--------------------->*/
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);
/*<-------------------Tool Bar-------------->*/
setSupportActionBar(toolbar);
/*<-------------------NAVIGATION DRAWER MENU-------------->*/
navigationView.bringToFront();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(R.id.home_nav);
//--------------------BOTTOM NAVIGATION-----------------------
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);
bottomNavigationView.setOnNavigationItemSelectedListener(selectedListener);
//default-selected-bottom-nav
Home home = new Home();
FragmentTransaction ft1 = getSupportFragmentManager().beginTransaction();
ft1.replace(R.id.framelayout, home, null);
ft1.commit();
}
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.home_nav:
Intent intent = new Intent(this, Home_Activity.class);
startActivity(intent);
break;
case R.id.categories_nav:
fragment = new categories_fragment();
loadFragment(fragment);
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.about_us_nav:
fragment = new about_us_fragment();
loadFragment(fragment);
drawerLayout.closeDrawer(GravityCompat.START);
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
private void loadFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, fragment).commit();
drawerLayout.closeDrawer(GravityCompat.START);
fragmentTransaction.addToBackStack(null);
}
private BottomNavigationView.OnNavigationItemSelectedListener selectedListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home_bottom_nav:
Home home = new Home();
FragmentTransaction ft1 = getSupportFragmentManager().beginTransaction();
ft1.replace(R.id.framelayout, home, null);
ft1.commit();
return true;
case R.id.menu_bottom_nav:
Menu menu = new Menu();
FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();
ft2.replace(R.id.framelayout, menu, null);
ft2.commit();
return true;
case R.id.fav_nav_bottom:
Favourites Fav = new Favourites();
FragmentTransaction ft3 = getSupportFragmentManager().beginTransaction();
ft3.replace(R.id.framelayout, Fav, null);
ft3.commit();
return true;
case R.id.user_bottom_profile:
User_Profile Userprofile = new User_Profile();
FragmentTransaction ft4 = getSupportFragmentManager().beginTransaction();
ft4.replace(R.id.framelayout, Userprofile, null);
ft4.commit();
return true;
}
return false;
}
};
}
Please Help Me...

Related

Android Naviagtion Items not Displaying Fragments

I have created a navHost which i would like to replace with fragments from the my navGraph and using the onNavigationItemSelected event listener i want the navHost replaced by every navigation item i have selected. But i still dont know why the FragmentTransaction.replace method doesnt get the navHost replaced by the navGraph commited to
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:showIn="#layout/app_bar_main">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation"></fragment>
</androidx.constraintlayout.widget.ConstraintLayout>
mobile_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
app:startDestination="#+id/nav_home">
<fragment
android:id="#+id/nav_home"
android:name="com.example.airtimeflip.ui.fragments.home.HomeFragment"
android:label="#string/menu_home"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/nav_profile"
android:name="com.example.airtimeflip.ui.fragments.profile.ProfileFragment"
android:label="#string/profile"
tools:layout="#layout/fragment_profile" />
<fragment
android:id="#+id/nav_manager"
android:name="com.example.airtimeflip.ui.fragments.manager.AccountManagerFragment"
android:label="#string/menu_slideshow"
tools:layout="#layout/fragment_account_manager" />
<fragment
android:id="#+id/nav_atc"
android:name="com.example.airtimeflip.ui.fragments.atc.AtcFragment"
android:label="#string/menu_gallery"
tools:layout="#layout/fragment_atc" />
<fragment
android:id="#+id/nav_buy_airtime"
android:name="com.example.airtimeflip.ui.fragments.buy_airtime.BuyAirtimeFragment"
android:label="#string/menu_tools"
tools:layout="#layout/fragment_buy_airtime" />
<fragment
android:id="#+id/nav_send_money"
android:name="com.example.airtimeflip.ui.fragments.send_money.SendMoneyFragment"
android:label="#string/menu_share"
tools:layout="#layout/fragment_send_money" />
</navigation>
MainActivity.java
package com.example.airtimeflip;
import android.os.Bundle;
import com.example.airtimeflip.config.GlobalUtilConfig;
import com.example.airtimeflip.ui.fragments.atc.AtcFragment;
import com.example.airtimeflip.ui.fragments.buy_airtime.BuyAirtimeFragment;
import com.example.airtimeflip.ui.fragments.home.HomeFragment;
import android.util.Log;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.example.airtimeflip.ui.fragments.manager.AccountManagerFragment;
import com.example.airtimeflip.ui.fragments.profile.ProfileFragment;
import com.example.airtimeflip.ui.fragments.send_money.SendMoneyFragment;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private static final String TAG = "MainActivity";
private AppBarConfiguration mAppBarConfiguration;
private DrawerLayout drawer;
// private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
//adding event listener to the navigation view
navigationView.setNavigationItemSelectedListener(this);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_profile, R.id.nav_manager,
R.id.nav_atc, R.id.nav_buy_airtime, R.id.nav_send_money)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
}
#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 onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
menuItem.setChecked(true);
try {
if (id == R.id.nav_home){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new HomeFragment());
ft.commit();
} else if (id == R.id.nav_atc){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new AtcFragment());
ft.commit();
} else if (id == R.id.nav_buy_airtime){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new BuyAirtimeFragment());
ft.commit();
}else if (id == R.id.nav_profile){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new ProfileFragment());
ft.commit();
}else if (id == R.id.nav_manager){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new AccountManagerFragment());
ft.commit();
}else if (id == R.id.nav_send_money){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new SendMoneyFragment());
ft.commit();
}
} catch (Exception e) {
//e.printStackTrace();
Log.d(TAG, "onNavigationItemSelected: "+ e.toString());
}
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
Something you are doing wrong here. You are using wrong id.
R.id.nav_home, R.id.nav_atc, R.id.nav_buy_airtime ,R.id.nav_profile, R.id.nav_manager, R.id.nav_send_money. These all are navigation fragment id. But instead you should use menu id.
Also When using Navigation, Navigate to destination using NavController.
Replace this code, this code will also work but the navigation architecture comes with more convenient way of doing the things.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new HomeFragment());
ft.commit();
with this
navController.navigate(R.id.nav_atc);
Also this lines are redundant
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // No need use the same which you have initialize in onCreate
drawerLayout.closeDrawer(GravityCompat.START)
Just use
drawerLayout.closeDrawer(GravityCompat.START)

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

Replacing toolbar title by icon

Hello all I have an activity with three fragments.When the app launches it directly displays my activity with activity Name.and on clicking to fragments inside navigation drawer the toolbar title changes.Now I just want to replace the Toolbar title just of Activity with icon. for this I used setlogo.However I wish to make the icon visible just at the first time when the app launches.I mean when I select fragment I should be able to replace the icon by fragment name.
this is my code of activity.Please help
package com.example.user.educationhunt;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.user.educationhunt.fragment.NearMe;
import com.example.user.educationhunt.fragment.Register;
import com.example.user.educationhunt.fragment.Search;
import com.example.user.educationhunt.fragment.Settings;
import com.example.user.educationhunt.pojos.feedbackData;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class EduHunt extends AppCompatActivity {
private DrawerLayout mDrawer;
private Toolbar toolbar;
private NavigationView nvDrawer;
private ActionBarDrawerToggle drawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edu_hunt);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setLogo(R.mipmap.edutext);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
nvDrawer = (NavigationView) findViewById(R.id.nvView);
setupDrawerContent(nvDrawer);
// Find our drawer view
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = setupDrawerToggle();
// Tie DrawerLayout events to the ActionBarToggle
mDrawer.addDrawerListener(drawerToggle);
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.flContent, new Search());
tx.commit();
}
private ActionBarDrawerToggle setupDrawerToggle() {
return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
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.search:
fragmentClass = Search.class;
break;
case R.id.settings:
fragmentClass = Settings.class;
break;
case R.id.register:
fragmentClass = Register.class;
break;
case R.id.nearme:
fragmentClass = NearMe.class;
break;
default:
fragmentClass = Search.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawer.closeDrawers();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()){
case R.id.our_team:
final Dialog dialog=new Dialog(this);
dialog.setContentView(R.layout.activity_our_team);
dialog.show();
return true;
case R.id.feedback:
startActivity(new Intent(EduHunt.this,SendFeedback.class));
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
drawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.education_hunt, menu);
return true;
}
}
this is my toolbar.xml
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="?attr/colorPrimaryDark">
</android.support.v7.widget.Toolbar>
Well, you need to customize your toolbar: as below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="56dp"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
app:theme="#style/ToolbarSearchView"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="horizontal"
android:id="#+id/ll_navi"
android:background="#color/colorPrimary"
android:gravity="center_vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="46dp"
android:paddingLeft="5dp"
....
/>
<TextView
android:layout_width="match_parent"
android:layout_height="46dp"
android:paddingLeft="5dp"
..../>
</LinearLayout>
</android.support.v7.widget.Toolbar>
And after that you just need to handle the visibility of image and text as per your requirement.
Edit
Include the toolbar layout in your activity's layout as:
<LinearLayout 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:gravity="center_horizontal"
android:orientation="vertical"
android:background="#drawable/bg">
<include
android:id="#+id/tool_bar"
layout="#layout/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
........
</LineraLayout>
and then in your Activity access toolbar as:
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView txtTitle = (TextView) findViewById(R.id.toolbar_title);
Try this.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.yourIcon);
toolbar.setTitle("");
Change it here:
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;
toolbar.setNavigationIcon(R.drawable.yourIcon);
switch (menuItem.getItemId()) {
case R.id.search:
fragmentClass = Search.class;
//Change it here
toolbar.setNavigationIcon(R.drawable.cutomIcon);
break;
case R.id.settings:
//Change it here
toolbar.setNavigationIcon(R.drawable.cutomIcon);
fragmentClass = Settings.class;
break;
case R.id.register:
//Change it here
toolbar.setNavigationIcon(R.drawable.cutomIcon);
fragmentClass = Register.class;
break;
case R.id.nearme:
//Change it here
toolbar.setNavigationIcon(R.drawable.cutomIcon);
fragmentClass = NearMe.class;
break;
default:
//Change it here
toolbar.setNavigationIcon(R.drawable.cutomIcon);
fragmentClass = Search.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawer.closeDrawers();
}
Try this,
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.search:
toolbar.setTitle("search");
fragmentClass = Search.class;
break;
case R.id.settings:
toolbar.setTitle("settings");
fragmentClass = Settings.class;
break;
case R.id.register:
fragmentClass = Register.class;
break;
case R.id.nearme:
fragmentClass = NearMe.class;
break;
default:
fragmentClass = Search.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawer.closeDrawers();
toolbar.setLogo(null);
}

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.

Fragments get not replaced during OnClick event in navigation drawer

I created a menu with 5 items in a navigation drawer, the 5 items represent 5 different fragments, each time an item gets clicked it should show the related fragment.
Another click on a different item, fragment should be replaced and so on.
I created 5 seperate classes for the fragments.
When i start the app and click an item the very first time, the related fragment gets shown but after i click another item in the menu nothing happens (the fragment gets not replaced), i also get no errors and the app does not crash.
I don`t know where the problem is hopefully somebody can help, here is my code:
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private MyAdapter myAdapter;
public ListView mainMenuList;
public Fragment_Android fAndroid;
public Fragment_Latest_Releases fLatest;
public Fragment_My_Applications fMyapps;
public Fragment_Platforms fPlatforms;
public Fragment_Settings fSettings;
public android.support.v4.app.FragmentManager fragmanager;
public android.support.v4.app.FragmentTransaction fragtrans;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mainMenuList = (ListView) findViewById(R.id.testList);
myAdapter = new MyAdapter(this);
mainMenuList.setAdapter(myAdapter);
toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp((R.id.fragment_navigation_drawer), (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
fAndroid = new Fragment_Android();
fLatest = new Fragment_Latest_Releases();
fMyapps = new Fragment_My_Applications();
fPlatforms = new Fragment_Platforms();
fSettings = new Fragment_Settings();
mainMenuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0: {
Toast.makeText(getApplicationContext(), "Android has been clicked.", Toast.LENGTH_SHORT).show();
fragmanager = getSupportFragmentManager();
fragtrans = fragmanager.beginTransaction();
fragtrans.replace(R.id.fragment_container, fAndroid);
fragtrans.commit();
drawer.closeDrawers();
break;
}
case 1: {
Toast.makeText(getApplicationContext(), "Latest Releases has been clicked.", Toast.LENGTH_SHORT).show();
fragmanager = getSupportFragmentManager();
fragtrans = fragmanager.beginTransaction();
fragtrans.replace(R.id.fragment_container, fLatest);
fragtrans.commit();
drawer.closeDrawers();
break;
}
case 2: {
Toast.makeText(getApplicationContext(), "Platforms has been clicked.", Toast.LENGTH_SHORT).show();
fragmanager = getSupportFragmentManager();
fragtrans = fragmanager.beginTransaction();
fragtrans.replace(R.id.fragment_container, fPlatforms);
fragtrans.commit();
drawer.closeDrawers();
break;
}
case 3: {
Toast.makeText(getApplicationContext(), "My Applications has been clicked.", Toast.LENGTH_SHORT).show();
fragmanager = getSupportFragmentManager();
fragtrans = fragmanager.beginTransaction();
fragtrans.replace(R.id.fragment_container, fMyapps);
fragtrans.commit();
drawer.closeDrawers();
break;
}
case 4: {
Toast.makeText(getApplicationContext(), "Settings has been clicked.", Toast.LENGTH_SHORT).show();
fragmanager = getSupportFragmentManager();
fragtrans = fragmanager.beginTransaction();
fragtrans.replace(R.id.fragment_container, fSettings);
fragtrans.commit();
drawer.closeDrawers();
break;
}
}
}
});
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#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) {
Toast.makeText(this, "Just hit settings.." + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Example of 1 class (in this case Android):
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment_Android extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragandroid = inflater.inflate(R.layout.layout_android, null);
return fragandroid;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
Example of 1 fragment xml (Android fragment):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
My main activity xml:
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="android.kadas.org.teka.MainActivity">
<include
android:id="#+id/my_toolbar"
layout="#layout/app_bar" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="android.kadas.org.teka.NavigationDrawerFragment"
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>
</LinearLayout>
In the click listener you are actually replacing the entire layout of the activity with the desired fragment.
In your main activity's xml add the following layout, inside the RelativeLayout:
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Change your
fragtrans.replace(R.id.drawer_layout, fragmentX);
statements to
fragtrans.replace(R.id.container, fragmentX);
Finally solved due to the big help of Alexandru Rosianu.
The code has been amended with the correct one.

Categories

Resources