activity_dashboard.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" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/draw_layout" android:layout_gravity="center" android:background="#F8F0E3"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <androidx.appcompat.widget.Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorAccent" android:elevation="4dp" android:gravity="center" android:theme="@style/MyMaterialTheme.Base" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:titleTextColor="#FFFFFF"> <TextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/ip_track" android:textColor="@color/whiteColor" android:textSize="18sp" /> </androidx.appcompat.widget.Toolbar> <LinearLayout android:id="@+id/logOutLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/welcomeText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="20dp" /> <Button android:id="@+id/btnLogout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="#aaa" android:text="@string/logout" android:textColor="@android:color/white" /> </LinearLayout> <LinearLayout android:id="@+id/shareLinkLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/logOutLayout" android:background="@color/colorAccent" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/urLink" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> <Button android:id="@+id/shareLink" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/shareLinkLayout" android:background="@color/colorAccent" android:orientation="vertical"> <ListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="10dp" android:layout_marginTop="5dp" android:background="@android:color/transparent" android:cacheColorHint="@android:color/transparent" android:divider="#CCCCCC" android:dividerHeight="1dp" android:paddingLeft="2dp"> </ListView> </LinearLayout> </RelativeLayout> <com.google.android.material.navigation.NavigationView android:layout_width="wrap_content" android:id="@+id/nav_view" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/nav_header_main" app:menu="@menu/navigation_menu" /> </androidx.drawerlayout.widget.DrawerLayout>
Now add nav_header_main.xml in layout folder
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:background="@drawable/side_nav_bar" android:gravity="center" android:orientation="vertical" 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" android:src="@drawable/ic_star" /> <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>
Also add menu navigation_menu.xml in menu folder
<?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:ignore="HardcodedText"> <group android:id="@+id/group_home" android:checkableBehavior="single"> <item android:id="@+id/nav_home" android:title="Home" android:gravity="bottom|right" android:icon="@drawable/ic_home"/> </group> <item android:id="@+id/action_searchList" android:title="Scan Nearby Devices" android:gravity="right" android:icon="@drawable/ic_bluetooth"/> <item android:id="@+id/action_pairedList" android:title="Connect to a Paired Device" android:gravity="right" android:icon="@drawable/ic_pair_bluetooth"/> <item android:title="Communicate"> <menu> <item android:id="@+id/action_share" android:icon="@drawable/ic_share" android:orderInCategory="1" android:title="Share this app" /> <item android:id="@+id/action_about" android:icon="@drawable/digiline_logo" android:orderInCategory="2" android:title="About Us" /> </menu> </item> <group android:id="@+id/group_exit" android:checkableBehavior="single"> <item android:id="@+id/nav_exit" android:title="Exit" android:icon="@drawable/ic_exit_button"/> </group> </menu>
Â
public class DashboardActivity extends AppCompatActivity implements View.OnClickListener , NavigationView.OnNavigationItemSelectedListener{ public DrawerLayout drawerLayout; public ActionBarDrawerToggle actionBarDrawerToggle; private NavigationView mNavigationView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dashboard); //=========================Adding Toolbar in android layout======================================= Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(myToolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); //========================Toolbar End============================================================ Drawable drawable = myToolbar.getOverflowIcon(); if (drawable != null) { drawable = DrawableCompat.wrap(drawable); DrawableCompat.setTint(drawable.mutate(), getResources().getColor(R.color.whiteColor)); myToolbar.setOverflowIcon(drawable); } // ******************** Changing the color of three dot or overflow button ***************************** /*************************************************************************************** * Navigation Drawer Layout * ***************************************************************************************/ // drawer layout instance to toggle the menu icon to open // drawer and back button to close drawer drawerLayout = findViewById(R.id.draw_layout); actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close) { /** * Called when a drawer has settled in a completely closed state. */ public void onDrawerClosed(View view) { super.onDrawerClosed(view); // Do whatever you want here } /** * Called when a drawer has settled in a completely open state. */ public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); // Do whatever you want here // initItemData(); } public void onDrawerStateChanged(int i) { } public void onDrawerSlide(View view, float v) { } }; // pass the Open and Close toggle for the drawer layout listener // to toggle the button drawerLayout.addDrawerListener(actionBarDrawerToggle); actionBarDrawerToggle.syncState(); // to change humburger icon color actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.whiteColor)); mNavigationView = findViewById(R.id.nav_view); mNavigationView.setNavigationItemSelectedListener(this); // After giving this Navigation menu Item Icon becomes colorful mNavigationView.setItemIconTintList(null); // <-- HERE add this code for icon color // to make the Navigation drawer icon always appear on the action bar getSupportActionBar().setDisplayHomeAsUpEnabled(true); /*************************************************************************************** } @Override public boolean onOptionsItemSelected(final android.view.MenuItem item) { // Yes this is necessary for drawalayout hamburger icon click on //navigation().getOptionsMenuInflater(this).closeSearchView(); // The action bar home/up action should open or close the drawer. // ActionBarDrawerToggle will take care of this behavior. actionBarDrawerToggle.onOptionsItemSelected(item); return super.onOptionsItemSelected(item); }
@Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.nav_exit: exitApplication(); break; case R.id.action_share: shareApp(); break; case R.id.action_about: Intent intent = new Intent(this, AboutActivity.class); startActivity(intent); break; case R.id.action_searchList: ScanDevicesList(); break; case R.id.action_pairedList: pairedDevicesList(); break; } //close navigation drawer drawerLayout.closeDrawer(GravityCompat.START); return true; } }
Comments