android Android Studio3.5 | beenhere Device Tested | subscriptions Tutorial Video | list_alt Project #1 Part 2

important_devices NewsHub
book Our First Project is NewsHub App. It is a News app, where we use some free news api for showing news. We learn whole app making process by some part. Today we will learn Toolbar and navigation drawer part.
The Android Toolbar has replaced the old action bar. It was introduced in API 21 (Android 5.0 Lollipop). In this tutorial we are going to implement it. Our goal is to implement a toolbar with hamburger icon.
By now most of us are familiar with Navigation Drawers as they’ve been in use by Facebook, Gmail, Google Play Music, Google Play Music and tons of others apps. It’s that sliding panel that comes out onto the screen when you swipe a finger from the left edge of the screen (or sometimes right edge) or tap on the 3 bar button (sometimes called hamburger icon) in the Action Bar.
In this article I’ll go through how to create a navigation drawer (also referred to as sidebar menu, slide-out menu, sliding menu, hamburger menu, basements, etc.) for your own apps using the DrawerLayout.
developer_mode activity_home.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:id="@+id/drawerLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".HomeActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary"> </androidx.appcompat.widget.Toolbar> </RelativeLayout> <LinearLayout android:orientation="vertical" android:layout_width="250dp" android:layout_gravity="start" android:layout_height="match_parent" android:background="@color/background"> <LinearLayout android:background="@color/colorPrimary" android:paddingLeft="8dp" android:paddingRight="10dp" android:elevation="20dp" android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="57dp"> <ImageView android:src="@mipmap/logo" android:layout_width="40dp" android:layout_height="40dp"/> <TextView android:text="@string/app_name" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:textSize="20dp" android:textColor="#ffffff" android:textStyle="bold" android:layout_width="150dp" android:layout_height="wrap_content"/> <TextView android:id="@+id/back" android:clickable="true" android:text=">" android:textSize="30dp" android:textColor="#ffffff" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:id="@+id/Business" android:clickable="true" android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="250dp" android:layout_height="50dp"> <ImageView android:padding="10dp" android:src="@drawable/business" android:layout_width="40dp" android:layout_height="40dp"/> <TextView android:text="Business" android:textStyle="bold" android:textColor="@color/colorPrimary" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:background="@color/colorPrimary" android:layout_width="250dp" android:layout_height="1dp"/> <LinearLayout android:id="@+id/Entertainment" android:clickable="true" android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="250dp" android:layout_height="50dp"> <ImageView android:padding="10dp" android:src="@drawable/mood" android:layout_width="40dp" android:layout_height="40dp"/> <TextView android:text="Entertainment" android:textStyle="bold" android:textColor="@color/colorPrimary" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:background="@color/colorPrimary" android:layout_width="250dp" android:layout_height="1dp"/> <LinearLayout android:id="@+id/General" android:clickable="true" android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="250dp" android:layout_height="50dp"> <ImageView android:padding="10dp" android:src="@drawable/style" android:layout_width="40dp" android:layout_height="40dp"/> <TextView android:text="General" android:textStyle="bold" android:textColor="@color/colorPrimary" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:background="@color/colorPrimary" android:layout_width="250dp" android:layout_height="1dp"/> <LinearLayout android:id="@+id/Health" android:clickable="true" android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="250dp" android:layout_height="50dp"> <ImageView android:padding="10dp" android:src="@drawable/favorite" android:layout_width="40dp" android:layout_height="40dp"/> <TextView android:text="Health" android:textStyle="bold" android:textColor="@color/colorPrimary" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:background="@color/colorPrimary" android:layout_width="250dp" android:layout_height="1dp"/> <LinearLayout android:id="@+id/Science" android:clickable="true" android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="250dp" android:layout_height="50dp"> <ImageView android:padding="10dp" android:src="@drawable/book" android:layout_width="40dp" android:layout_height="40dp"/> <TextView android:text="Science" android:textStyle="bold" android:textColor="@color/colorPrimary" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:background="@color/colorPrimary" android:layout_width="250dp" android:layout_height="1dp"/> <LinearLayout android:id="@+id/Sports" android:clickable="true" android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="250dp" android:layout_height="50dp"> <ImageView android:padding="10dp" android:src="@drawable/toys" android:layout_width="40dp" android:layout_height="40dp"/> <TextView android:text="Sports" android:textStyle="bold" android:textColor="@color/colorPrimary" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:background="@color/colorPrimary" android:layout_width="250dp" android:layout_height="1dp"/> <LinearLayout android:id="@+id/Technology" android:clickable="true" android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="250dp" android:layout_height="50dp"> <ImageView android:padding="10dp" android:src="@drawable/language" android:layout_width="40dp" android:layout_height="40dp"/> <TextView android:text="Technology" android:textStyle="bold" android:textColor="@color/colorPrimary" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:background="@color/colorPrimary" android:layout_width="250dp" android:layout_height="1dp"/> <LinearLayout android:id="@+id/Contact" android:clickable="true" android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="250dp" android:layout_height="50dp"> <ImageView android:padding="10dp" android:src="@drawable/email" android:layout_width="40dp" android:layout_height="40dp"/> <TextView android:text="Contact Us" android:textStyle="bold" android:textColor="@color/colorPrimary" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:background="@color/colorPrimary" android:layout_width="250dp" android:layout_height="1dp"/> <LinearLayout android:id="@+id/Policy" android:clickable="true" android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="250dp" android:layout_height="50dp"> <ImageView android:padding="10dp" android:src="@drawable/chart" android:layout_width="40dp" android:layout_height="40dp"/> <TextView android:text="Privacy Policy" android:textStyle="bold" android:textColor="@color/colorPrimary" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:background="@color/colorPrimary" android:layout_width="250dp" android:layout_height="1dp"/> <LinearLayout android:id="@+id/About" android:clickable="true" android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="250dp" android:layout_height="50dp"> <ImageView android:padding="10dp" android:src="@drawable/info" android:layout_width="40dp" android:layout_height="40dp"/> <TextView android:text="About Us" android:textStyle="bold" android:textColor="@color/colorPrimary" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:background="@color/colorPrimary" android:layout_width="250dp" android:layout_height="1dp"/> </LinearLayout> </androidx.drawerlayout.widget.DrawerLayout>

    xxxxxxxxxx

1

    <?xml version="1.0" encoding="utf-8"?>

2

    <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"

3

        xmlns:app="http://schemas.android.com/apk/res-auto"

4

        xmlns:tools="http://schemas.android.com/tools"

5

        android:id="@+id/drawerLayout"

6

        android:layout_width="match_parent"

7

        android:layout_height="match_parent"

8

        android:fitsSystemWindows="true"

9

        tools:context=".HomeActivity">

10

    ​

11

        <RelativeLayout

12

            android:layout_width="match_parent"

13

            android:layout_height="match_parent">

14

    ​

15

            <androidx.appcompat.widget.Toolbar

16

                android:id="@+id/toolbar"

17

                android:layout_width="match_parent"

18

                android:layout_height="wrap_content"

19

                android:background="@color/colorPrimary">

20

            </androidx.appcompat.widget.Toolbar>

21

    ​

22

    ​

23

        </RelativeLayout>

24

    ​

25

        <LinearLayout

26

            android:orientation="vertical"

developer_mode HomeActivity.java
package com.friendvilla.newshub; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.drawerlayout.widget.DrawerLayout; import android.os.Bundle; import androidx.appcompat.widget.Toolbar; public class HomeActivity extends AppCompatActivity { private DrawerLayout drawerLayout; private Toolbar toolbar; private ActionBarDrawerToggle actionBarDrawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); actionBarDrawerToggle = new ActionBarDrawerToggle(HomeActivity.this,drawerLayout,toolbar,R.string.app_name,R.string.app_name); drawerLayout.addDrawerListener(actionBarDrawerToggle); actionBarDrawerToggle.syncState(); } }

    xxxxxxxxxx

1

    package com.friendvilla.newshub;

2

    ​

3

    import androidx.appcompat.app.ActionBarDrawerToggle;

4

    import androidx.appcompat.app.AppCompatActivity;

5

    import androidx.drawerlayout.widget.DrawerLayout;

6

    ​

7

    import android.os.Bundle;

8

    import androidx.appcompat.widget.Toolbar;

9

    ​

10

    public class HomeActivity extends AppCompatActivity {

11

        private DrawerLayout drawerLayout;

12

        private Toolbar toolbar;

13

        private ActionBarDrawerToggle actionBarDrawerToggle;

14

    ​

15

        @Override

16

        protected void onCreate(Bundle savedInstanceState) {

17

            super.onCreate(savedInstanceState);

18

            setContentView(R.layout.activity_home);

19

    ​

20

            drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

21

            toolbar = (Toolbar) findViewById(R.id.toolbar);

22

            setSupportActionBar(toolbar);

23

            actionBarDrawerToggle = new ActionBarDrawerToggle(HomeActivity.this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);

24

            drawerLayout.addDrawerListener(actionBarDrawerToggle);

25

            actionBarDrawerToggle.syncState();

26

        }

dns Continue to Downloadget_app