Create Drawer in Android

 //--------------------------------------------Code Detail and Working------------------------------------------------

1 Step Drawer create

//============================java Code Start ===============================

//menu create folder



//Menu create file follow step 


//write name menu create




//item add code

<item
   
android:id="@+id/action_fr"
   
android:title="Action"
   
android:icon="@drawable/home"/>
<item
   
android:id="@+id/action_histry"
   
android:title="Histry"/>


//====================================End==================================

//--------------------------------------------Code Detail and Working---------------------------------------------------------

2 Step XML call Drawer create theme show icon navigation

//============================java Code Start =========================================

<?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=".drwer"
   
android:id="@+id/drawer_layout"
   
android:orientation="vertical">

<!--frameLayout show Containner -->
   
<LinearLayout
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
android:orientation="vertical">

        <FrameLayout
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:id="@+id/frame_layout"/>
    </LinearLayout>
   
<!--end frameLayout show Containner -->
    <!--Drawer navigation show  -->
   
<com.google.android.material.navigation.NavigationView
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
android:layout_gravity="start"
       
android:id="@+id/nav_view"
       
app:menu="@menu/drawer_manu"/>
   
<!--end Drawer navigation show  -->

</androidx.drawerlayout.widget.DrawerLayout>

=========================================End==========================================

//--------------------------------------------Code Detail and Working---------------------------------------------------------

3 step String add

//============================java Code Start =========================================

<string name="navigation_drawer_open">open drawer</string>
<string
name="navigation_drawer_close">close drawer</string>






=========================================end======================================

 

//--------------------------------------------Code Detail and Working---------------------------------------------------------

4 step drawer layout instance to toggle the menu icon to open

//============================java Code Start =========================================

// class inner create reference

ActivityDrwerBinding Binding;

// drawer layout instance to toggle the menu icon to open
        // drawer and back button to close drawer
        
toggle= new ActionBarDrawerToggle(this,Binding.drawerLayout,
               
R.string.navigation_drawer_open,R.string.navigation_drawer_close);
       
// pass the Open and Close toggle for the drawer layout listener
        // to toggle the button
       
Binding.drawerLayout.addDrawerListener(toggle);
       
toggle.syncState();
// to make the Navigation drawer icon always appear on the action bar
    
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

=========================================end======================================

//--------------------------------------------Code Detail and Working---------------------------------------------------------

5 Step Menu Option selected show

//============================java Code Start =========================================

 // override the onOptionsItemSelected()

    // function to implement

    // the item click listener callback

    // to open and close the navigation

    // drawer when the icon is clicked

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
   
if (toggle.onOptionsItemSelected(item)) {
       
return true;
   
}
   
return super.onOptionsItemSelected(item);
}

=========================================End==========================================

//--------------------------------------------Code Detail and Working---------------------------------------------------------

6 Step Nevigation click item

//============================java Code Start =========================================

Binding.navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
   
@Override
   
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

       
int id = item.getItemId();
        if
(id == R.id.action_fr) {
            FragmentTransaction ft =
                    getSupportFragmentManager().beginTransaction()
;
           
HomeFragment m= new HomeFragment();
           
ft.replace(R.id.frame_layout, m);
           
ft.commit();
         
//  Toast.makeText(drwer.this, "Setting", Toast.LENGTH_LONG).show();


       
}
       
//This is for closing the drawer after acting on it
       
Binding.drawerLayout.closeDrawer(GravityCompat.START);
      
// OnBackPressed();
      
return true;
   
}
})
;

=========================================End==========================================

//--------------------------------------------Code Detail and Working---------------------------------------------------------

Optional  Nevigation Header setting

//============================java Code Start =========================================

View headerView = binding.drawerNavigation.getHeaderView(0);

        DrawerHeaderBinding dbinding  = DrawerHeaderBinding.bind(headerView);

        dbinding.textViewEmail.setText("Hello");

=========================================End==========================================

 

Optional code Nevigation icon Open and close function

//============================java Code Start =========================================

public  void OnBackPressed(){
   
if(Binding.drawerLayout.isDrawerOpen(GravityCompat.START)){
       
Binding.drawerLayout.closeDrawer(GravityCompat.START);
   
}else {
       
super.onBackPressed();
   
}
}

=========================================End==========================================

//--------------------------------------------Code Detail and Working---------------------------------------------------------

Drawer Navigation calling without navigation bar show in theme changing

//============================java Code Start =========================================

toggle = new ActionBarDrawerToggle(this,

                binding.drawerLayout,

                binding.drawerToolbar,

                R.string.drawer_opened,

                R.string.drawer_closed);

        toggle.setDrawerIndicatorEnabled(true);

        toggle.syncState();

=========================================End==========================================

//--------------------------------------------Code Detail and Working---------------------------------------------------------

 Fragment calling  Binding

//============================java Code Start =========================================

FragmentHome2Binding Binding;
   
@Override
   
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            
Bundle savedInstanceState) {

       
Binding = FragmentHome2Binding.inflate(inflater, container, false);
       
Binding.ActionTxt.setText("muzamil");
        return
Binding.getRoot();
       
// Inflate the layout for this fragment
      //  return inflater.inflate(R.layout.fragment_home2, container, false);
   
}

=========================================End==========================================


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.