My Navigation Drawer closes whenever i click some item on it [duplicate] - android

This question already has answers here:
Android in Navigation Drawer when I click anywhere drawer closes
(2 answers)
Navigation Drawer closes on click
(1 answer)
Closed 3 years ago.
How do I make it so when I click on the menu it does something (in this case write "click")? For some reason it's not working. I followed a youtube video for this , I'm new to android studio. Thank you in advance. Also how do i bypass this "mostly code" error. when I have not more to explain.
DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
Toolbar toolbar;
NavigationView navigationView;
private DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer);
drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open_drawer,R.string.close_drawer);
drawerLayout.addDrawerListener(drawerToggle);
drawerToggle.setDrawerIndicatorEnabled(true);
drawerToggle.syncState();
navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
}
The OnClick part
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId())
{
case R.id.nav_new_purchase:
Toast.makeText(this,"CLICK",Toast.LENGTH_LONG).show();
break;
case R.id.nav_active_purchase:
//TODO
case R.id.nav_old_purchase:
//TODO
}
return false;
}
XML file with the (main_activity.xml)
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/drawer">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/app_toolbar"/>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer_menu"
android:layout_gravity="start">
</com.google.android.material.navigation.NavigationView>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:foreground="#drawable/ic_action_add"
app:backgroundTint="#00FF00"
app:fabCustomSize="200dp"
app:fabSize="auto"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:text="#string/add"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/add_button" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>

You check follow:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
//add this line
Toast.makeText(this,"WORK",Toast.LENGTH_LONG).show();
switch (menuItem.getItemId())
{
case R.id.nav_new_purchase:
Toast.makeText(this,"nav_new_purchase",Toast.LENGTH_LONG).show();
break;
case R.id.nav_active_purchase:
Toast.makeText(this,"nav_new_purchase",Toast.LENGTH_LONG).show();
break;
case R.id.nav_old_purchase:
Toast.makeText(this,"nav_new_purchase",Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(this,"Not found menu id",Toast.LENGTH_LONG).show();
break;
}
return false;
}
If will show "WORK", you code work. Also, There is shown clicked id or "Not found menu id". If will show "Not found menu id", you check wrong menu id.

drawerLayout.closeDrawer(GravityCompat.START) add this code last line click listener

Related

drawer menu not doing anything on Click event

Iam Implementing navigation drawer in my application but its menu not working on click. I dont know whats wrong but on click event is not doing anything or toasting a message.Click anything from drawer menu dont do anything.Firstly other button were also not working but adding them in a seprate layout solved the issue.but drawer menu are still not working
Here is my xml :
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/app_bar_main" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Here in java Iam using it :
public class AuthorMainScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_author_navigation);
viewDeclaration();
drawerLayout.addDrawerListener(actionBarDrawerToggle);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
private void viewDeclaration() {
newSurveyBtn = findViewById(R.id.new_surveys_button);
surveyWithRef = findViewById(R.id.get_survey_button);
surveyResult = findViewById(R.id.analyze_survey);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation_view);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_share:
System.out.println("ss" + "coming");
Toast.makeText(getApplicationContext(), "Share", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_survey_count:
Toast.makeText(getApplicationContext(), "Surveys", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_logout:
Toast.makeText(getApplicationContext(), "logout", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
Solution for this is in the XML file bring navigation view below the include tag and it will work
Change this
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/app_bar_main" />
</LinearLayout>
To this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/app_bar_main" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
You don't initialize actionBarDrawerToggle. You have to do it.
When you are using AndroidX try to use the methods related to it.
Add the host fragment when you want to implement the fragments in that particular activity:
xml:
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<fragment
android:id="#+id/host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/navigation_graph"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/app_bar_main" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
Now use the navigation graph where you need to implement the fragments you are about to use in the activity :
navigation_graph.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/navigation_graph"
app:startDestination="#id/profile">
<fragment
android:id="#+id/profile"
android:name="com.example.jetpacknavigationexample.Fragments.ProfileFragment"
android:label="#string/profile"
tools:layout="#layout/fragment_profile" />
<fragment
android:id="#+id/features"
android:name="com.example.jetpacknavigationexample.Fragments.FeaturesFragment"
android:label="#string/features"
tools:layout="#layout/fragment_features" />
<fragment
android:id="#+id/signout"
android:name="com.example.jetpacknavigationexample.Fragments.ProfileFragment"
android:label="#string/signout"
tools:layout="#layout/fragment_signout" />
</navigation>
In your java class add the implement the Navigation component code :
public class AuthorMainScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
private ActionBarDrawerToggle actionBarDrawerToggle;
//Add this
NavController mNavController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_author_navigation);
//add this
mNavController = Navigation.findNavController(this,R.id.host_fragment);
NavigationUI.setupActionBarWithNavController(this, mNavController, drawerLayout);
NavigationUI.setupWithNavController(navigationView,mNavController);
mNavigationView.setNavigationItemSelectedListener(this);
viewDeclaration();
drawerLayout.addDrawerListener(actionBarDrawerToggle);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
private void viewDeclaration() {
newSurveyBtn = findViewById(R.id.new_surveys_button);
surveyWithRef = findViewById(R.id.get_survey_button);
surveyResult = findViewById(R.id.analyze_survey);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation_view);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_share:
System.out.println("ss" + "coming");
Toast.makeText(getApplicationContext(), "Share",
Toast.LENGTH_SHORT).show();
break;
case R.id.menu_survey_count:
Toast.makeText(getApplicationContext(), "Surveys",
Toast.LENGTH_SHORT).show();
break;
case R.id.menu_logout:
Toast.makeText(getApplicationContext(), "logout",
Toast.LENGTH_SHORT).show();
break;
}
return true;
}
You can test your app now. let me know after you implement this.

how to create the click event in sidebar navigation

I have created the sidebar navigation in my app. Now I want to create a click event on that.
I have searched for it and I got the documentation but I'm unable to understand the same.
so please anyone can suggest a better source for it will be helpful. tutorial on youtube will be helpful.
My navigation_menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/nav_account"
android:icon="#mipmap/ic_person_black_24dp"
android:title="My Account"/>
<item android:id="#+id/nav_setting"
android:icon="#mipmap/ic_settings_black_24dp"
android:title="Settings"/>
<item android:id="#+id/nav_logout"
android:icon="#mipmap/ic_logout_black_24dp"
android:title="Logout"/>
</menu>
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"
tools:context="com.example.lenovo.jdstudio.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/navigation_action"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Main Layout"
android:textAlignment="center"
android:textSize="24dp" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/navigation_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Simply implement NavigationView.OnNavigationItemSelectedListener interface and then override the method onNavigationItemSelected(MenuItem menuItem) like the following:
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// declaring the NavigationView
navigationView = (NavigationView) findViewById(R.id.navigationView);
// assigning the listener to the NavigationView
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
switch (menuItem.getItemId()) {
case R.id.nav_account:
// do you click actions for the first selection
break;
case R.id.nav_setting:
// do you click actions for the second selection
break;
case R.id.nav_logout:
// do you click actions for the third selection
break;
}
return true;
}
}
And for sure don't forget to give id to the NavigationView in your xml-layout:
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/navigation_menu">
</android.support.design.widget.NavigationView>

navigation drawer does not started

I have my navigation drawer and i need to open it but i can't>>
Here is my drawer_layout.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="#layout/activity_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#android:color/white"
app:menu="#menu/drawer_view" />
and this is my toolbar in activity_display.xml:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/myToolBar"
android:background="#color/top_bar_backgroung">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dots_vertical"
android:textSize="40sp"
android:layout_marginRight="10dp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/openDrawer"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:src="#drawable/logo"
android:scaleType="centerInside"/>
</android.support.v7.widget.Toolbar>
and here is how i open my drawer :
private DrawerLayout mDrawer;
private Toolbar toolbar;
private NavigationView nvDrawer;
toolbar = (Toolbar) findViewById(R.id.myToolBar);
setSupportActionBar(toolbar);
// Find our drawer view
View viewDrawer = getLayoutInflater().inflate(R.layout.drawer_layout, null);
mDrawer = (DrawerLayout) viewDrawer.findViewById(R.id.drawer_layout);
nvDrawer = (NavigationView) viewDrawer.findViewById(R.id.nvView);
setupDrawerContent(nvDrawer);
//gridView = (GridView) findViewById(R.id.displayGridView);
drawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "onClick: " );
mDrawer.openDrawer(GravityCompat.END);
}
});
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
android.support.v4.app.Fragment fragment1 = null;
Class fragmentClass = null;
switch(menuItem.getItemId()) {
case R.id.nav_home_fragment:
Toast.makeText(this, "home", Toast.LENGTH_SHORT).show();
//fragmentClass = FirstFragment.class;
break;
case R.id.nav_personal_fragment:
//fragmentClass = SecondFragment.class;
break;
case R.id.nav_avilableCareer_fragment:
//fragmentClass = ThirdFragment.class;
break;
case R.id.nav_declareCareer_fragment:
//fragmentClass = FourdFragment.class;
break;
default:
//fragmentClass = FirstFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment1).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
mDrawer.closeDrawers();
}
i don't know why i can't start my navigation draw..
i can't see it on screen.
write end instead of right in xml file
android:layout_gravity="right"
to
android:layout_gravity="end"
or alternate solution
use
mDrawer.openDrawer(GravityCompat.RIGHT);
instead of
mDrawer.openDrawer(GravityCompat.END);

How to attach fragment to Navigationview

I am using NaivgationView in my app, when i select item from drawer,the view of my fragment which i set is not displaying. i also added toast it shows while i select that item can any one help me with this ?
public class MainActivity extends AppCompatActivity
{
private DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
FragmentManager fragmentManager = getFragmentManager();
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.home:
Toast.makeText(getApplicationContext(),"Inbox Selected", Toast.LENGTH_SHORT).show();
return true;
// For rest of the options we just show a toast on click
case R.id.be_the_donor:
Toast.makeText(getApplicationContext(),"Stared Selected",Toast.LENGTH_SHORT).show();
RegisterDonor frag = new RegisterDonor();
// update the main content by replacing fragments
fragmentManager.beginTransaction()
.replace(R.id.frame_container, frag)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit();
return true;
case R.id.search_donor:
Toast.makeText(getApplicationContext(),"Send Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.tools:
Toast.makeText(getApplicationContext(),"Drafts Selected",Toast.LENGTH_SHORT).show();
return true;
default:
Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show();
return true;
}
}
});
// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open, R.string.navigation_drawer_close){
#Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.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);
}
}
Fragment
public class RegisterDonor extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.register_donor, container, false);
return rootView;
}
}
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
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<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.dayshift.bloodfinder.MainActivity"
tools:showIn="#layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
register_donor
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:id="#+id/usernameWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/register_donor_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Enter Name"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/mobileWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/register_donor_mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Enter Mobile"/>
</android.support.design.widget.TextInputLayout>
<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
android:id="#+id/register_donor_bloodgroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Material Design Spinner"
android:textColorHint="#05ab9a"
app:met_floatingLabel="normal" />
<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
android:id="#+id/register_donor_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Material Design Spinner"
android:textColorHint="#05ab9a"
app:met_floatingLabel="normal" />
<AutoCompleteTextView
android:id="#+id/register_donor_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="">
<requestFocus />
</AutoCompleteTextView>
</LinearLayout>
Your app bar occupies the entire screen.
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Change the app bar to wrap content on the height
In activity_main_drawer write your item with id and title after that in mainactivity in method onNavigationItemSelected for your id declare your fragment and commit
You are telling that the Toasts are showing up but not the fragment so the problem might be the FragmentManager
check you import of fragmentManager which should be
import android.support.v4.app.FragmentManager;
and should not be
import android.app.FragmentManager;
And your fragment transaction should be like this
FragmentManager fragmentManager = getSupportFragmentManager();
RegisterDonor frag = new RegisterDonor();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, frag , "")
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit();
hope this will help you!
Try this way i guess it works fine :
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
int id = menuItem.getItemId();
switch (id) {
case R.id.be_the_donor:
Fragment registerDonor = new RegisterDonor();
fragmentTransaction.replace(R.id.frame_container, registerDonor);
fragmentTransaction.commit();
drawerLayout.closeDrawers();
break;

Edit toolbar according to the fragment displaying

n my main activity I have a navigation drawer and by clicking it I m loading different fragments like Home , Cart , Settings etc.. Initially in my main activity I got the toolbar with spinner and button to navigation drawer as well. But for other fragments I need to customize tool bar as I need.
Here is how my activity_main.xml implemented :
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/navigation_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="#bool/fitsSystemWindows">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="#dimen/status_bar_kitkat_height"
android:background="?colorPrimary" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="#dimen/status_bar_lollipop_height"
android:background="?colorPrimaryDark" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/status_bar_margin_top">
<FrameLayout
android:id="#+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?actionBarSize" />
<!--include different toolbars into the activity dont know this is correct implementation... If I missed any of them It gives me a null pointer exception in MainActivity..-->
<include
android:id="#+id/toolbar_cart"
layout="#layout/toolbar_cart" />
<include
android:id="#+id/toolbar_home"
layout="#layout/toolbar_home" />
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="#bool/fitsSystemWindows"
app:headerLayout="#layout/navigation_drawer_header"
app:menu="#menu/navigation_drawer_menu"
app:theme="#style/NavigationViewTheme" />
</android.support.v4.widget.DrawerLayout>
Then I implemented a method to customize toolbar according to the fragment loading (inside main activity) :
public void customizeToolBar(int position) {
switch (position) {
case 0:
toolbar = (Toolbar) findViewById(R.id.toolbar_home);
setSupportActionBar(toolbar);
subCategories_spinner = (Spinner) findViewById(R.id.spinner_subCategories);
actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
break;
case 1:
toolbar = (Toolbar) findViewById(R.id.toolbar_cart);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setTitle("Check Out");
actionBar.setDisplayHomeAsUpEnabled(true);
break;
....
Then I call that method inside the NavigationItemSelectedListener like below :
private void setupNavigationDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.item_navigation_drawer_home:
menuItem.setChecked(true);
customizeToolBar(0);
setFragment(0);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.item_navigation_drawer_cart:
menuItem.setChecked(true);
customizeToolBar(1);
setFragment(1);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
...
But this is not working. Every time displays the same toolbar. I m new to android. Any suggestion to make this work would be grateful. Thank you.
There are many solutions to this issue.
If you include different toolbars you can add set android:visibility=""
Ex:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#2196F3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
</android.support.v7.widget.Toolbar>
And set toolbar Visibility when you change fragment:
toolbar.setVisibility(View.VISIBLE);
Or you can edit toolbar in the fragment:
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
Get toolbar from Main Activity and edit.

Categories

Resources