1
Hello,
I have prior knowledge of Java but I’ve just started programming for mobile on Android Studio, someone would tell me if there’s any way to use Appbar + Toolbar (for the sake of design itself, the Bottomnavigation i found it very ugly and little customizable), like floating menu, along with Fragments?
I’ve thought of some ways, and I was using setOnClickListener but I can’t find a way to alternate the Fragment. I’ve also tried using the Navigationui but it was unsuccessful, probably I’m failing to notice something. I’ll leave here my Mainactivity.java and my activiy_main.xml to set a better example of what I would like to do.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:backgroundTint="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:liftOnScroll="true">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbarMain"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/shadow_bottombar_main"
android:theme="?attr/actionBarTheme">
<TextView
android:id="@+id/mainNavigation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mainTitle"
android:fontFamily="@font/gilroy_bold"
android:textSize="20sp"
android:layout_marginStart="30dp"
android:textColor="#AA000000"/>
<TextView
android:id="@+id/popularNavigation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/popularsTitle"
android:fontFamily="@font/gilroy_bold"
android:textSize="20sp"
android:layout_marginStart="30dp"
android:textColor="#AA000000"/>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
Mainactivity.java
public class MainActivity extends AppCompatActivity {
private AppBarLayout bottomBar;
private Toolbar toolbarMain;
private TextView mainNavigation;
private TextView popularNavigation;
private NavController navController;
private MainFragment mainFragment;
private PopularsFragment popularsFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomBar = findViewById(R.id.nav_view);
toolbarMain = findViewById(R.id.toolbarMain);
mainNavigation = findViewById(R.id.mainNavigation);
popularNavigation = findViewById(R.id.popularNavigation);
mainFragment = new MainFragment();
popularsFragment = new PopularsFragment();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupWithNavController(toolbarMain, navController);
onOptionsItemSelected();
}
public void eventOnClickMenuBottom() {
mainNavigation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
navController = Navigation.findNavController(mainFragment.getActivity(), R.id.nav_host_fragment);
}
});
popularNavigation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
navController = Navigation.findNavController(popularsFragment.getActivity(), R.id.nav_host_fragment);
}
});
}
}
My idea was to do the onClick of each Textview change the Activity of navController, but it did not happen.
Anyway, thanks for the attention who read and I appreciate any help.
what you think of using tablayout and viewpager?
– Murillo Comino
I was warned about Viewpager, I was able to implement it for my code and it works. My problem right now is that I need to make buttons inside the fragments change what is appearing in Viewpager, then I’m cracking my head with it now. But yes, your tip was right, the best way to do what I wanted was with Viewpager. I thank you!
– Davi Massini
You can try to implement a Listener in the Viewpager Adapter, so the Fragments communicate with it, anything put what you managed to do in updating your response, and tomorrow I try to help you more.
– Murillo Comino
Have a look at that answer, please! https://stackoverflow.com/questions/36600229/go-to-next-page-in-viewpager/36601047#36601047
– Thiago da Silva
Thanks to both (I would mark both, but I just realized that is not allowed), the answers helped a lot. Yesterday I implemented this method that Thiago sent now and it really works well. I managed to do what you need, now is to go to other parts of the project and have new doubts... hahahahahahha
– Davi Massini