
---- 최종 결과 ----
1. file > new > empty activity > name : Bottom Navigator
2. res > new > android Resource Directory
3. menu > new > menu Resource file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:icon="@drawable/ic_home"
android:title="Home"
/>
<item
android:id="@+id/chatting"
android:title="Chatting"
android:icon="@drawable/ic_chat"
/>
<item
android:id="@+id/favorite"
android:title="Favorite"
android:icon="@drawable/ic_favorite"
/>
<item
android:id="@+id/user"
android:title="User"
android:icon="@drawable/ic_user"
/>
</menu>
4. @drawable/ic ~~ 추가하는 방법 : drawable > new > Vector Asset > clip art 선택 후 원하는 icon 선택 가능
> name, color, size 등 선택 가능 >>>> next > finish > android:icon="@drawable/(지정 name)"
5. MainActivity.java
package com.example.bottomnavigatoin;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.MenuItem;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.btnNav);
fragementRepl(new HomeFragment());
bottomNavigationView.setOnItemReselectedListener(new NavigationBarView.OnItemReselectedListener() {
@Override
public void onNavigationItemReselected(@NonNull MenuItem item) {
switch ( item.getItemId()) {
case R.id.home:
fragementRepl(new HomeFragment());
break;
case R.id.chatting:
fragementRepl(new ChattingFragment());
break;
case R.id.favorite:
fragementRepl(new FavoriteFragment());
break;
case R.id.user:
fragementRepl(new UserFragment());
break;
}
}
});
}
private void fragementRepl(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout,fragment);
fragmentTransaction.commit();
}
}
6. activity_main.xml
<RelativeLayout 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=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:id="@+id/frameLayout"
android:layout_above="@+id/btnNav"
android:layout_alignParentBottom="true"
android:layout_height="match_parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:id="@+id/btnNav"
app:menu="@menu/menu"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
/>
</RelativeLayout>

7. java > com.example.bottomnavigation > new > fragment > fragment(blank)
> HomeFragment , ChattingFragment , FavoriteFragment , UserFragment 동일하게 4개 만들
package com.example.bottomnavigatoin;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeFragment extends Fragment {
public HomeFragment() { // public (해당 Fragment 맞는지 확인, 다르면 바꿔주기)
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
^ ex HomeFragment.java (chatting, favorite, user.java 도 동일한 방식으로 만들기)
, @Override 위에 코드 다 지워주기
8. layout > new > layout resorce file > fragment_chatting, fragment_home, fragment_favorite, fragment_user.xml 만들기
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".HomeFragment"> // 해당 xml 맞는지 확인
<TextView
android:layout_width="match_parent"
android:text="Home Fragment" // 해당 xml 맞는지 확인
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#4CAF50" // 옆에 색상 나온거 네모 누르면 다른색으로 변경 가능
android:gravity="center"
android:layout_gravity="center"
android:layout_height="match_parent"
/>
</FrameLayout>
^ ex fragment_home.xml (chatting, favorite, user.xml 도 동일한 방식으로 만들기)
