MainActivity에 BottomNavigation 및 Fragment에 Intent
중간중간에 쓸데없는 코드는 다른 기능 구현하려다가 빼고 뭐 어쩌다보니웅앵
걍 인생 대충삽시다
Gradle Scripts의 build.gradle에 추가
implementation 'com.google.android.material:material:1.3.0'
res - menu 생성 후 거기에 bottom item
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/tab1"
app:showAsAction="ifRoom"
android:enabled="true"
android:icon="@mipmap/baseline_queue_music_black_36"
android:textAllCaps="false"
android:title="Book">
</item>
<item
android:id="@+id/tab2"
app:showAsAction="ifRoom"
android:enabled="true"
android:textAllCaps="false"
android:icon="@mipmap/baseline_library_music_black_36"
android:title="Drama">
</item>
<item
android:id="@+id/tab3"
app:showAsAction="ifRoom"
android:enabled="true"
android:textAllCaps="false"
android:icon="@mipmap/baseline_local_florist_black_36"
android:title="etc.">
</item>
</menu>
activity_main.xml
Fragment 표시할 FrameLayout
com.google.android.material.bottomnavigation.BottomNavigationView 여기에 아까 만든 menu의 bottom
<androidx.constraintlayout.widget.ConstraintLayout
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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:itemBackground="@color/maincolorbg"
app:itemTextColor="@color/maintext"
app:menu="@menu/menu_buttom" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment.xml 각 3개씩 생성 / Fragment.java 각 3개씩 생성
(xml은 여기에 기록 생략)
Fragment1.java의 일부
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.fragment.app.Fragment;
public class Fragment1 extends Fragment {
View view;
Context context;
OnTabItemSelectedListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment1, container, false);
initUI(rootView);
return rootView;
}
private void initUI(ViewGroup rootView) {
Button button1 = rootView.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://audioclip.naver.com/search/all?keyword=신범식"));
startActivity(myIntent);
}
});
Button button2 = rootView.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.storytel.com/kr/ko/search-%EC%8B%A0%EB%B2%94%EC%8B%9D"));
startActivity(myIntent);
}
});
.
.
.
.
MainActivity.java
나는 여기에 bottom외에 상단 ActionBar에 아이콘도 두개 넣음
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements OnTabItemSelectedListener {
Fragment1 fragment1;
Fragment2 fragment2;
Fragment3 fragment3;
BottomNavigationView bottomNavigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment1).commit();
BottomNavigationView bottomNavigation = findViewById(R.id.bottom_navigation);
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.tab1:
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment1).commit();
return true;
case R.id.tab2:
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment2).commit();
return true;
case R.id.tab3:
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment3).commit();
return true;
}
return false;
}
});
}
@Override
public void OnTabItemSelected(int position) {
if (position == 0) {
bottomNavigation.setSelectedItemId(R.id.tab1);
} else if (position == 1) {
bottomNavigation.setSelectedItemId(R.id.tab2);
} else if (position == 2) {
bottomNavigation.setSelectedItemId(R.id.tab3);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_button, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int curId = item.getItemId();
switch (curId) {
case R.id.jamjari:
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://m.youtube.com/channel/UCd6aTixvIfHOfeJZQuES5nw/featured"));
startActivity(myIntent);
break;
case R.id.setting:
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivityForResult(intent, 101);
break;
}
return super.onOptionsItemSelected(item);
}
}
너무 대충 했나
'리빙 포인트 > Android Studio' 카테고리의 다른 글
[Android Studio] AlertDialog (0) | 2021.01.19 |
---|---|
[Android Studio] button color (0) | 2021.01.19 |