๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/Smart_Fan

[Smart_Fan] 05 - Android Studio : Navigation Drawer Menu ๊ตฌํ˜„ 01

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2020. 7. 27.

 

 

 

[Smart_Fan] 04 - Arduino : ๋ฒ„ํŠผ์œผ๋กœ DC๋ชจํ„ฐ ์ „์› ๊ป๋‹ค ๋„๊ธฐ

[Smart_Fan] 03 - Arduino : DC๋ชจํ„ฐ ์ž‘๋™ ์‹œํ‚ค๊ธฐ [Smart_Fan] 02 - Oven์œผ๋กœ ์•ฑ UI ๊ตฌ์ƒ ๋ฐ ์•„๋‘์ด๋…ธ ๊ตฌ์ž… [Smart_Fan] 01 - ์ฃผ์ œ ์„ ์ • ์ฃผ์ œ ์„ ์ • [2020-06-10] ์นœ๊ตฌ์™€ ํ•จ๊ป˜ ์–ด๋– ํ•œ ์ฃผ์ œ๋ฅผ ๊ฐ€์ง€๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœ..

onelight-stay.tistory.com

 

Navigation Drawer Menu ๊ตฌํ˜„

[2020-07-12]

 

์šฐ๋ฆฌ๋Š” ์•„๋‘์ด๋…ธ์˜ DC ๋ชจํ„ฐ ๋ฌธ์ œ๋กœ ์ธํ•ด

๋จผ์ € ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค๋ฅผ ์ด์šฉํ•ด ๊ธฐ๋ณธ์ ์ธ ํ‹€์„ ๋งŒ๋“ค๊ธฐ๋กœ ํ•˜์˜€๊ณ ,

๋จผ์ € ๋ฉ”๋‰ด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ขŒ์ธก์—์„œ ๋ฉ”๋‰ด๊ฐ€ ์Šฌ๋ผ์ด๋“œ๋กœ ๋‚˜์™”๋‹ค ๋“ค์–ด๊ฐ”๋‹ค ํ•  ์ˆ˜ ์žˆ๋Š”

์Šฌ๋ผ์ด๋“œ ๋ฉ”๋‰ด๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

 

#01. ๋ฉ”๋‰ด๋ฒ„ํŠผ ํด๋ฆญ ์‹œ Navigation Drawer Menu๊ฐ€ ๋‚˜์˜ค๋„๋ก ์„ค์ •

 

  • build.gradle(:app)
//Material Design
implementation 'com.google.android.material:material:1.2.0-alpha02'

//Rounded Image View
implementation 'com.makeramen:roundedimageview:2.3.0'

//Navigation Component
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.0-rc03'
implementation 'androidx.navigation:navigation-ui-ktx:2.2.0-rc03'

๊ฐ€์žฅ ๋จผ์ € ํ•ด์•ผํ•  ์ผ์€ dependencies { }์— ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๊ณ  SYNC๋ฅผ ํ•ด์ฃผ๋Š” ์ผ์ด๋‹ค.

 

ํ”Œ๋žซํผ๊ณผ ๊ธฐ๊ธฐ ์ „๋ฐ˜์˜ ์‹œ๊ฐ์ ์ธ ์š”์†Œ๋ฅผ ๋””์ž์ธํ•˜๊ธฐ ์œ„ํ•œ Material Design,

์ด๋ฏธ์ง€์˜ ๋ชจ์„œ๋ฆฌ๋ฅผ ๋‘ฅ๊ธ€๊ฒŒ ์„ค์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ๋Š” Rounded Image View,

ํŽ˜์ด์ง€๊ฐ„์˜ ์ด๋™์„ ์‰ฝ๊ณ  ์•ˆ์ •์ ์ผ ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” Navigation Component๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์—ˆ๋‹ค.

 

 

  • styles.xml
 <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

style.xml์—์„œ style์˜ parent๋ฅผ "Theme.AppCompat.Light.DarkActionBar"์—์„œ

"Theme.MaterialComponents.Light.NoActionBar"๋กœ ๋ณ€๊ฒฝํ•ด ์ฃผ์—ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ๋˜๋ฉด DarkActionBar์—์„œ NoActionBar๋กœ ๋ณ€๊ฒฝ๋˜์–ด์„œ

์ƒ๋‹จ์˜ ์•ก์…˜๋ฐ”๊ฐ€ ์‚ฌ๋ผ์ง€๊ฒŒ ๋˜๋Š”๋ฐ

์ถ”ํ›„์— ํˆด๋ฐ”๋ฅผ ์ œ์ž‘ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.

 

 

  • colors.xml, strings.xml, ์ด๋ฏธ์ง€ ์ถ”๊ฐ€
ํŒŒ์ผ ์ด๋ฆ„ ์„ค๋ช…
back.png ์Šฌ๋ผ์ด๋“œ ๋ฉ”๋‰ด์˜ ์ƒ๋‹จ ๋’ท๋ฐฐ๊ฒฝ
fan.png ์Šฌ๋ผ์ด๋“œ ๋ฉ”๋‰ด์˜ ์ƒ๋‹จ ๋กœ๊ณ 
ic_addremote.xml ๋ฆฌ๋ชจ์ปจ ์ถ”๊ฐ€ ์•„์ด์ฝ˜
ic_explain.xml ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค๋ช… ์•„์ด์ฝ˜
ic_home.xml ํ™ˆ ์•„์ด์ฝ˜
ic__menu.xml ์ƒ๋‹จ ํˆด๋ฐ”์— ์œ„์น˜ํ•  ๋ฉ”๋‰ด ์•„์ด์ฝ˜
ic_remote.xml ๋ฆฌ๋ชจ์ปจ ์•„์ด์ฝ˜
ic_settings.xml ํ™˜๊ฒฝ์„ค์ • ์•„์ด์ฝ˜

์ด์™ธ์—๋„ ํ™”๋ฉด์˜ ๋””์ž์ธ์„ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ๋“ค๋„ ๋”ฐ๋กœ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ๋‹ค.

 

 

  • menu/navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_home"
        android:title="@string/home" />

    <item android:title="@string/remote">
        <menu>
            <item
                android:id="@+id/nav_remote"
                android:icon="@drawable/ic_remote"
                android:title="@string/remote_control"/>
            <item
                android:id="@+id/nav_addremote"
                android:icon="@drawable/ic_addremote"
                android:title="@string/add_remote_control"/>
        </menu>
    </item>

    <item
        android:id="@+id/nav_explain"
        android:icon="@drawable/ic_explain"
        android:title="@string/explain" />

    <item
        android:id="@+id/nav_settings"
        android:icon="@drawable/ic_settings"
        android:title="@string/settings" />
</menu>

๋ณธ๊ฒฉ์ ์œผ๋กœ ๋จผ์ € ๋ฉ”๋‰ด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ขŒ์ธก์—์„œ ์Šฌ๋ผ์ด๋”ฉํ•˜๋ฉด์„œ ๋‚˜์˜ค๋Š”

menu์˜ ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

 

 

  • layout/layout_navigation_header.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="10dp"
    android:background="@drawable/back">

    <com.makeramen.roundedimageview.RoundedImageView
        android:id="@+id/fan"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="centerCrop"
        android:src="@drawable/fan"
        android:layout_marginTop="35dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:riv_oval="false"/>

    <View
        android:id="@+id/viewSupporter"
        android:layout_width="1dp"
        android:layout_height="1dp"
        app:layout_constraintBottom_toBottomOf="@id/fan"
        app:layout_constraintEnd_toEndOf="@id/fan"
        app:layout_constraintStart_toStartOf="@id/fan"
        app:layout_constraintTop_toTopOf="@id/fan"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="45dp"
        android:text="@string/smart_fan"
        android:textColor="@color/colorPrimaryText"
        android:textSize="28sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@id/viewSupporter"
        app:layout_constraintStart_toEndOf="@id/fan"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="16dp"
        android:text="๋˜‘๋˜‘ํ•œ ์„ ํ’๊ธฐ Smart Fan"
        android:textColor="@color/colorPrimaryText"
        android:textSize="12sp"
        android:textStyle="bold"
        app:layout_constraintStart_toEndOf="@id/fan"
        app:layout_constraintTop_toBottomOf="@id/viewSupporter" />

</androidx.constraintlayout.widget.ConstraintLayout>

navigation_menu.xml์—์„œ ๋งŒ๋“  ํ•ญ๋ชฉ๋“ค ์œ„์—

๋ฐฐ๋„ˆ์ฒ˜๋Ÿผ ํ•ด๋‹น ์•ฑ์˜ ๋Œ€๋žต์ ์ธ ์„ค๋ช…๊ณผ ์ด๋ฏธ์ง€๊ฐ€ ๋“ค์–ด๊ฐ„ ํ—ค๋”๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋‹ค.

 

 

  • layout/activity_main.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"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="@color/colorPrimary"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingStart="15dp"
            android:paddingEnd="15dp"
            app:layout_constraintTop_toTopOf="parent">

            <ImageView
                android:id="@+id/imageMenu"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:contentDescription="@string/app_name"
                android:src="@drawable/ic_menu"
                android:tint="@color/colorWhite"/>

            <TextView
                android:id="@+id/textTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="@string/app_name"
                android:textColor="@color/colorWhite"
                android:textSize="18sp"
                android:textStyle="bold" />

        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:headerLayout="@layout/layout_navigation_header"
        app:menu="@menu/navigation_menu"
        android:layout_gravity="start"/>

</androidx.drawerlayout.widget.DrawerLayout>

activity_main.xml์—์„œ๋Š” ํ™ˆ ํ™”๋ฉด์—์„œ ์ƒ๋‹จ์— ์žˆ๋Š” ํˆด๋ฐ”๋ฅผ ์ƒ์„ฑํ•˜์˜€๊ณ ,

ํˆด๋ฐ”์˜ ์ขŒ์ธก์— ์žˆ๋Š” ๋ฉ”๋‰ด ๋ฒ„ํŠผ์„ ์ƒ์„ฑํ•ด

ํด๋ฆญ ์‹œ ๋„ค๋น„๊ฒŒ์ด์…˜ ๋ฉ”๋‰ด์— ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑํ•˜์˜€๋‹ค.

 

 

  • MainActivity.java
package com.example.smartfan;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import android.os.Bundle;
import android.view.View;

import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        findViewById(R.id.imageMenu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

		// start์— ์ง€์ •๋œ Drawer ์—ด๊ธฐ
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });

        NavigationView navigationView = findViewById(R.id.navigationView);
        navigationView.setItemIconTintList(null);
    }
}

javaํŒŒ์ผ์„ ํ†ตํ•ด์„œ ํˆด๋ฐ”์— ์žˆ๋Š” ๋ฉ”๋‰ด ๋ชจ์–‘ ์ด๋ฏธ์ง€๋ฅผ ํด๋ฆญํ•  ๊ฒฝ์šฐ์—

์ขŒ์ธก์— ์œ„์น˜ํ•œ ๋ฉ”๋‰ด๊ฐ€ ์—ด๋ฆด ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด ์ฃผ์—ˆ๋‹ค.

 

 

์• ๋ฎฌ๋ ˆ์ดํ„ฐ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๋ฉด ๋ฉ”๋‰ด ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด

์ขŒ์ธก์˜ ๋ฉ”๋‰ด๊ฐ€ ์—ด๋ฆฌ๊ณ  ๋‹ซํžˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 


 

 

๋‚ด์šฉ์ด ๊ธธ์–ด์ ธ์„œ ์ผ๋‹จ์€ ๋ฉ”๋‰ด๋ฅผ ๋งŒ๋“ค์–ด์ง„ ๊ฒƒ๊นŒ์ง€๋งŒ ๋ณด์—ฌ์ฃผ์—ˆ์ง€๋งŒ

๋‹ค์Œ์—๋Š” ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๊ฐ ํŽ˜์ด์ง€๋กœ ๋„˜๊ธฐ๋„๋ก ํ•˜๋Š” ๊ฒƒ๋„ ์„ค์ •ํ•ด ์ค˜ ๋ณด๋„๋ก ํ•˜์ž.

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€