본문 바로가기
android

안드로이드 스튜디오 bottom navigation & WebView - 첫번째

by 최고집이사 2018. 3. 5.

안드로이드 스튜디오 Bottom Navigation & WebView - 첫번째


거의 한 달 동안 공부해서 완성시킨 Bottom Navigation...

보통은 Bottom Navigation에 대한 강좌는 너무 많아 공부하기 쉬웠으나

거기에 WebView를 포함시키는 방법에 대해서는 전혀없어서 스스로 공부를 해

방금 완성시켰다.

이건 내 자신을 위한 기록이라서 설명은 따로 없다.




Bottom Navigation - 첫번째.


   1. FragmentOne.java


java 마우스 오른쪽 - new - Fragment - FragmentBlink : 

 name : FragmentOne옵션 [inciude~ ]2개 체크해제


import android.content.DialogInterface;

import android.support.v7.app.AlertDialog;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.KeyEvent;

import android.view.View;

import android.view.ViewGroup;

import android.webkit.JsResult;

import android.webkit.WebChromeClient;

import android.webkit.WebSettings;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.webkit.WebViewFragment;

import android.widget.TextView;


import com.google.firebase.messaging.FirebaseMessaging;



public class FragmentOne extends Fragment {



    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        //Bundle arguments = getArguments();

        View view = inflater.inflate(R.layout.fragment_fragment_one, container, false);

        WebView webView = (WebView)view.findViewById(R.id.webView);

        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebViewClient(new WebViewClient());

        webView.loadUrl("http://사이트주소");

        return view;

    }

}


res - layout : fragment_fragment_one.xml


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

<FrameLayout 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=".FragmentOne">


    <!-- TODO: Update blank fragment layout -->

    <WebView

        android:id="@+id/webView"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />


</FrameLayout>




   2. FragmentTwo.java


java 마우스 오른쪽 - new - Fragment - FragmentBlink : 

 name : FragmentTwo 옵션 [inciude~ ]2개 체크해제


import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.webkit.WebView;

import android.webkit.WebViewClient;



public class FragmentTwo extends Fragment {



    public FragmentTwo() {

        // Required empty public constructor

    }



    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        //Bundle arguments = getArguments();

        View view = inflater.inflate(R.layout.fragment_fragment_two, container, false);

        WebView webView = (WebView)view.findViewById(R.id.webView);

        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebViewClient(new WebViewClient());

        webView.loadUrl("http://사이트주소");

        return view;

    }


}


res - layout : fragment_fragment_two.xml

<FrameLayout 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=".FragmentTwo">

    <!-- TODO: Update blank fragment layout -->
    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>



   3. FragmentThree.java


java 마우스 오른쪽 - new - Fragment - FragmentBlink : 

 name : FragmentThree 옵션 [inciude~ ]2개 체크해제


import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.webkit.WebView;

import android.webkit.WebViewClient;



public class FragmentThree extends Fragment {



    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        //Bundle arguments = getArguments();

        View view = inflater.inflate(R.layout.fragment_fragment_three, container, false);

        WebView webView = (WebView)view.findViewById(R.id.webView);

        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebViewClient(new WebViewClient());

        webView.loadUrl("http://사이트주소");

        return view;

    }


}


res - layout : fragment_fragment_three.xml


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

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

    android:layout_width="match_parent"

    android:layout_height="match_parent"

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

    tools:context=".FragmentThree">


    <!-- TODO: Update blank fragment layout -->

    <WebView

        android:id="@+id/webView"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />


</FrameLayout>



   4. res - menu - navigation.xml


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

<menu xmlns:android="http://schemas.android.com/apk/res/android">


    <item

        android:id="@+id/navigation_home"

        android:icon="@drawable/ic_home_black_24dp"

        android:title="메뉴이름1" />


    <item

        android:id="@+id/navigation_dashboard"

        android:icon="@drawable/ic_dashboard_black_24dp"

        android:title="메뉴이름2" />


    <item

        android:id="@+id/navigation_notifications"

        android:icon="@drawable/ic_notifications_black_24dp"

        android:title="메뉴이름3" />


</menu>




   5. res - layout - activity_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:id="@+id/container"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#ffffff"

    tools:context="패키지명.MainActivity">


    <FrameLayout

        android:id="@+id/fram"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_marginBottom="48dp"

        android:background="#FFFFFF"

        android:text="@string/title_home"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintLeft_toLeftOf="parent"

        app:layout_constraintTop_toTopOf="parent" />



    <android.support.design.widget.BottomNavigationView

        android:id="@+id/navigation"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="#F05F40"

        app:layout_anchor="@+id/container"

        app:layout_anchorGravity="bottom"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintLeft_toLeftOf="parent"

        app:layout_constraintRight_toRightOf="parent"

        app:menu="@menu/navigation" />


</android.support.constraint.ConstraintLayout>

android:background="#F05F40" : 바 바탕 색상.

app:itemIconTint="color code" : 바 아이콘 색상.

app:itemTextColor="color code" : 바 아이콘 설명 색상.



   6. MainActivity


import android.os.Bundle;

import android.support.annotation.NonNull;

import android.support.design.widget.BottomNavigationView;

import android.support.v4.app.FragmentTransaction;

import android.support.v7.app.AppCompatActivity;

import android.view.KeyEvent;

import android.view.MenuItem;

import android.widget.TextView;


public class MainActivity extends AppCompatActivity {


    private TextView mTextMessage;


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener

            = new BottomNavigationView.OnNavigationItemSelectedListener() {


        @Override

        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {

                case R.id.navigation_home:

                    setTitle("Fragment Title One");

                    FragmentOne fragment = new FragmentOne();

                    FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();

                    fragmentTransaction1.replace(R.id.fram, fragment, "FragmentName");

                    fragmentTransaction1.commit();

                    return true;

                case R.id.navigation_dashboard:

                    setTitle("Fragment Title Two");

                    FragmentTwo fragment2 = new FragmentTwo();

                    FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();

                    fragmentTransaction2.replace(R.id.fram, fragment2, "FragmentName");

                    fragmentTransaction2.commit();

                    return true;

                case R.id.navigation_notifications:

                    setTitle("Fragment Title Three");

                    FragmentThree fragment3 = new FragmentThree();

                    FragmentTransaction fragmentTransaction3 = getSupportFragmentManager().beginTransaction();

                    fragmentTransaction3.replace(R.id.fram, fragment3, "FragmentName");

                    fragmentTransaction3.commit();

                    return true;

            }

            return false;

        }

    };


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        backPressCloseHandler = new BackPressCloseHandler(this); //뒤로 한 번 더 누르면 종료


        mTextMessage = (TextView) findViewById(R.id.message);

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);

        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);


        setTitle("Fragment Title One");

        FragmentOne fragment = new FragmentOne();

        FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();

        fragmentTransaction1.replace(R.id.fram, fragment, "FragmentName");

        fragmentTransaction1.commit();

    }



    //아래는 뒤로 한 번 더 누르면 종료

    private BackPressCloseHandler backPressCloseHandler;

        

        @Override

        public void onBackPressed() {

            //super.onBackPressed();

            backPressCloseHandler.onBackPressed();

        }


}

★ 뒤로는 사용 안 할시 제거 (// 두 부분) 사용시 BackPressCloseHandler.java 추가해야 하는데

자세한 사항은 http://coder-json.tistory.com/4 사이트 방문.



   7. build.gradle(Module: app)


dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.1.0'

    implementation 'com.android.support:design:26.1.0'

    implementation 'com.android.support.constraint:constraint-layout:1.0.2'

    implementation 'com.android.support:support-vector-drawable:26.1.0'

    implementation 'com.android.support:support-v4:26.1.0'

    testImplementation 'junit:junit:4.12'

    androidTestImplementation 'com.android.support.test:runner:1.0.1'

    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    compile 'com.google.firebase:firebase-core:11.8.0'

    compile 'com.google.firebase:firebase-messaging:11.8.0'

    compile 'com.squareup.okhttp3:okhttp:3.2.0'

}

apply plugin: 'com.google.gms.google-services'




이상이고 다음엔 FCM 메세지와 인트로 추가 분 기록.

오늘도 좋은 하루되세요.