I have a HomeActivity which initiates the ActionBarDrawerToggle as follows:
private fun initDrawer() {
val toolbar = binding.fragmentHomeContainerView.findViewById<MaterialToolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
drawerToggle = ActionBarDrawerToggle(
this,
binding.drawer,
toolbar,
R.string.open_drawer,
R.string.close_drawer
)
binding.drawer.addDrawerListener(drawerToggle)
drawerToggle.syncState()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
binding.navigationView.setNavigationItemSelectedListener {
handleNavigationItemSelected(it)
true
}
}
The toolbar in question is inside a Fragment. Everything's normal and working until here, the toolbar is like Gmail's search with the hamburguer icon. But when I navigate to another fragment (that doesn't have this toolbar, but instead has an arrow back button), and then navigate back again to the first fragment (the one that has the toolbar) the icon doesn't work anymore. What's the best/correct solution for this? I've tried moving the initDrawer() function within the fragment but the application frozes when creating the ActionBarDrawerToggle I assume because of the activity passed to the ActionBarDrawerToggle
drawerToggle = ActionBarDrawerToggle(
activity = (requireActivity() as HomeActivity),
...
)
Moving the toolbar from the Fragment to the Activity isn't a solution neither, not because it doesn't fix the problem (which I'm not sure) but because I need the toolbar inside the fragment.
Minimal reproducible code:
MainActivity.kt:
class MainActivity : AppCompatActivity() {
private val binding: ActivityMainBinding by lazy {
ActivityMainBinding.inflate(layoutInflater)
}
private lateinit var drawerToggle: ActionBarDrawerToggle
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
}
override fun onStart() {
super.onStart()
initDrawer()
}
private fun initDrawer() {
val toolbar = binding.fragmentContainerView.findViewById<MaterialToolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
drawerToggle = ActionBarDrawerToggle(
this,
binding.drawer,
toolbar,
R.string.app_name,
R.string.app_name
)
binding.drawer.addDrawerListener(drawerToggle)
drawerToggle.syncState()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
}
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/drawer"
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="wrap_content">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_home" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:clipToPadding="false"
android:visibility="visible" />
</androidx.drawerlayout.widget.DrawerLayout>
Fragment1.kt:
class Fragment1 : Fragment(R.layout.fragment_1) {
private val binding: Fragment1Binding by lazy {
Fragment1Binding.bind(requireView())
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.button.setOnClickListener {
navigateToFragment2()
}
}
private fun navigateToFragment2() {
val action = R.id.navigateToFragment2
Navigation.findNavController(requireView()).navigate(action)
}
}
fragment_1.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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/entity_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment1">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
android:elevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:liftOnScroll="true">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_margin="6dp"
android:background="#drawable/rounded_toolbar"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:navigationIcon="#drawable/ic_baseline_menu_24"
app:titleTextColor="#android:color/white">
<EditText
android:id="#+id/entities_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="Search for client"
android:background="#android:color/transparent"
android:hint="search"
android:inputType="textPersonName"
android:minHeight="48dp"
android:textColorHint="#color/black" />
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.AppBarLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate to fragment 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment2.kt:
class Fragment2 : Fragment(R.layout.fragment_2) {
private val binding: Fragment2Binding by lazy {
Fragment2Binding.bind(requireView())
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setActionBarAction()
}
private fun setActionBarAction() {
val action = R.id.navigateToFragment1
binding.toolbar.setOnClickListener {
Navigation.findNavController(it).navigate(action)
}
}
}
fragment_2.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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment2">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginStart="4dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="#android:color/transparent"
android:elevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:liftOnScroll="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|start"
android:clickable="true"
android:focusable="true"
android:minHeight="?attr/actionBarSize"
app:contentInsetStartWithNavigation="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="#drawable/ic_baseline_chevron_left_32"
app:titleTextColor="#android:color/white">
</com.google.android.material.appbar.MaterialToolbar>
<com.google.android.material.textview.MaterialTextView
android:id="#+id/app_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate back with the arrow button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
nav_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_home"
app:startDestination="#id/fragment1">
<fragment
android:id="#+id/fragment1"
android:name="com.example.myapplication.Fragment1"
android:label="fragment_1"
tools:layout="#layout/fragment_1" >
<action
android:id="#+id/navigateToFragment2"
app:destination="#id/fragment2" />
</fragment>
<fragment
android:id="#+id/fragment2"
android:name="com.example.myapplication.Fragment2"
android:label="fragment_2"
tools:layout="#layout/fragment_2" >
<action
android:id="#+id/navigateToFragment1"
app:destination="#id/fragment1" />
</fragment>
</navigation>
I'm getting this error on Android 5x but i don't know what it could be the problem. I'm getting the error on the file FragmentDataNewjourneyBindingImpl. So i tracked and the file that is using the FragmentDataNewJourneyBinding is AuthenticationFragment. So it could be either my xml has something wrong or my fragment has.
This is my xml:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="viewModel"
type="br.com.gabriel.novajornada.security.auth.AuthenticationViewModel" />
<variable
name="constantes"
type="br.com.gabriel.novajornada.constants.ConstantsKt" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/include"
android:fitsSystemWindows="true">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout11"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
style="#style/bo_texto_regular"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin"
android:layout_marginEnd="#dimen/margin"
android:gravity="center_horizontal"
android:lineSpacingExtra="4sp"
android:text="#string/fill_data_title_new_journey"
android:textColor="#1a1b1a"
android:textSize="24sp"
app:layout_constraintTop_toTopOf="#id/constraintLayout11" />
<TextView
android:id="#+id/textView98"
style="#style/bo_texto_regular"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin"
android:layout_marginTop="#dimen/margin_small"
android:layout_marginEnd="#dimen/margin"
android:gravity="center_horizontal"
android:lineSpacingExtra="6sp"
android:text="#string/fill_data_subtitle"
android:textColor="#4a4b4a"
android:textSize="16sp"
app:layout_constraintTop_toBottomOf="#id/textView"
tools:layout_editor_absoluteX="0dp" />
<br.com.original.common.components.BOTextInputLayout
android:id="#+id/text_input_cpf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/margin"
android:layout_marginTop="19dp"
android:layout_marginRight="#dimen/margin"
android:hint="#string/identifier_field_title"
android:theme="#style/TextInputLayoutTheme.ClearButton"
app:errorEnabled="true"
app:errorTextAppearance="#style/MyProfile.Error"
app:hintTextAppearance="#style/TextLabel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView98">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/identifier"
style="#style/TextInputEditTextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text="#={viewModel.credentials.cpf}"
app:mask="#{ `###.###.###-##` }" />
</br.com.original.common.components.BOTextInputLayout>
<br.com.original.common.components.BOTextInputLayout
android:id="#+id/text_input_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/margin"
android:layout_marginRight="#dimen/margin"
android:hint="#string/name_field"
android:inputType="textNoSuggestions|textVisiblePassword"
android:theme="#style/TextInputLayoutTheme.ClearButton"
app:errorEnabled="true"
app:errorTextAppearance="#style/MyProfile.Error"
app:hintTextAppearance="#style/TextLabel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_input_cpf">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/name"
style="#style/TextInputEditTextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ âãàáäêèéëîìíïôòóõöùúüñçÂÃÀÁÄÊÈÉËÎÌÍÏÔÒÓÕÖÙÚÜÑÇ"
android:inputType="textNoSuggestions|textVisiblePassword"
android:maxLength="#{constantes.NAMES_MAX_LENGTH}"
android:text="#={viewModel.credentials.name}" />
</br.com.original.common.components.BOTextInputLayout>
<br.com.original.common.components.BOTextInputLayout
android:id="#+id/text_input_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/margin"
android:layout_marginRight="#dimen/margin"
android:clipChildren="false"
android:hint="#string/cellphone_ddd_field_title"
android:theme="#style/TextInputLayoutTheme.ClearButton"
app:errorTextAppearance="#style/MyProfile.Error"
app:hintTextAppearance="#style/TextLabel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_input_name">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/phone"
style="#style/TextInputEditTextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text="#={viewModel.credentials.phoneNumber}"
app:mask="#{ `(##) #####-####` }" />
</br.com.original.common.components.BOTextInputLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="#dimen/margin"
app:layout_constraintBottom_toTopOf="#+id/btnConfirm"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_input_phone"
app:layout_constraintVertical_bias="1.0">
<TextView
android:id="#+id/politicas"
style="#style/seja_subtitle_h3_regular"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingStart="#dimen/margin"
android:paddingEnd="#dimen/margin"
android:text="#string/politicas_header_new_journey"
android:textSize="#dimen/text_small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteY="4dp" />
<TextView
android:id="#+id/openPoliticas"
style="#style/seja_subtitle_h3_regular"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingStart="#dimen/margin"
android:paddingTop="2pt"
android:paddingEnd="#dimen/margin"
android:text="#string/privacy_policy_new_journey"
android:textSize="#dimen/text_small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/politicas" />
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="#+id/btnConfirm"
style="#style/seja_button_bordeless"
android:layout_width="match_parent"
android:layout_height="#dimen/button_height_seja"
android:layout_marginStart="#dimen/margin"
android:layout_marginTop="#dimen/margin"
android:layout_marginEnd="#dimen/margin"
android:enabled="#{identifier.text.length() == 14 && phone.text.length() == 15 && name.text.length() > 2}"
android:text="#string/continue_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<include
android:id="#+id/include"
layout="#layout/seja_bar_layout_newjourney_center" />
<br.com.original.bank.novajornada.common.component.BOStateView
android:id="#+id/stateView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
app:msv_errorView="#layout/seja_error_view_newjourney"
app:msv_loadingView="#layout/seja_loading_view_newjourney" />
</RelativeLayout>
</layout>
This is AuthenticationFragment.kt. I put only the part of the bindingView declaration because i think it's more important:
#ExperimentalCoroutinesApi
class AuthenticationFragment : NavigationFragment() {
val viewModel: AuthenticationViewModel by viewModel()
lateinit var bindingView: FragmentDataNewjourneyBinding
lateinit var formValidate: FormValidate
private val novaJornadaActivity: NovaJornadaActivity by lazy { activity as NovaJornadaActivity }
private val management: TagManagement by inject()
#ExperimentalCoroutinesApi
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
bindingView = DataBindingUtil.inflate<FragmentDataNewjourneyBinding>(inflater, R.layout.fragment_data_newjourney, container, false)
.apply {
lifecycleOwner = this#AuthenticationFragment
viewModel = this#AuthenticationFragment.viewModel
}
viewModel.checkUserPromoted((activity as NovaJornadaActivity).userPromoted)
return bindingView.root
}
Is there something wrong with my implementation? I will be glad for anyone for trying to help me
I make an application android for a school project , and actually i have a problem.
I have create 2 activity (MainActivity and BottomNavActivity), and I try to navigate between them, but the "fragment" home page in my navigation Remain undefined.
I found this tutorial in order to navigate between the fragments
and I added events) :
https://medium.com/#oluwabukunmi.aluko/bottom-navigation-view-with-fragments-a074bfd08711
But, now, I can navigate between my "fragments" BUT my initial "fragment" (the one that appears first when launching the activity), remains, and therefore when changing fragments, it is duplicated.
Here is a video of my problem
My Infos.class
public class Infos extends AppCompatActivity {
private ActivityInfosBinding binding;
//I create my variable
final Fragment frag2 = new ProduitsFragment();
final Fragment frag1 = new EntrepriseFragment();
final Fragment frag3 = new CGUFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = frag1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityInfosBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_entreprise, R.id.navigation_produits, R.id.navigation_CGU)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_infos);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(binding.navView, navController);
//I add my differents fragments
fm.beginTransaction().add(R.id.nav_host_fragment_activity_infos, frag3).hide(frag3).commit();
fm.beginTransaction().add(R.id.nav_host_fragment_activity_infos,frag1).hide(frag1).commit();
navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull #NotNull MenuItem item) {
switch (item.getItemId()) {
//My first fragment
case R.id.navigation_entreprise:
fm.beginTransaction().hide(active).show(frag1).commit();
active = frag1;
return true;
//My second fragment (default in video)
case R.id.navigation_CGU:
fm.beginTransaction().hide(active).show(frag3).commit();
active = frag3;
return true;
}
return false;
}
});
}
}
And my mobile_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
app:startDestination="#id/navigation_CGU"> //I try to change this, but nothing. This fragment is duplicated.
<fragment
android:id="#+id/navigation_entreprise"
android:name="fr.romaindrouhot.aurelinfo.ui.home.EntrepriseFragment"
android:label="#string/nav_entreprise"
tools:layout="#layout/fragment_entreprise" />
<fragment
android:id="#+id/navigation_produits"
android:name="fr.romaindrouhot.aurelinfo.ui.home.ProduitsFragment"
android:label="#string/nav_produits"
tools:layout="#layout/fragment_produits" />
<fragment
android:id="#+id/navigation_CGU"
android:name="fr.romaindrouhot.aurelinfo.ui.home.CGUFragment"
android:label="#string/nav_CGU"
tools:layout="#layout/fragment_cgu" />
</navigation>
Can you help me ?
yes, here is my activity_infos.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.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment_activity_infos"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:defaultNavHost="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
My fragment_cgu.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="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="50sp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
My fragment_entreprise.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".ui.home.EntrepriseFragment"
android:id="#+id/fragment_entreprise"
android:background="#drawable/logonoiretblanc">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/entreprise_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title"
android:textColor="#color/black"
android:textSize="#dimen/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/border"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/entreprise_title"
android:layout_marginTop="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/informations"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="#+id/linearLayout"
app:layout_constraintTop_toTopOf="#+id/linearLayout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</LinearLayout>
<TextView
android:id="#+id/title_gerant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/gerant_title"
android:shadowColor="#color/black"
android:shadowRadius="1"
android:textAlignment="center"
android:textColor="#color/dark_green"
android:textSize="#dimen/gerant_title"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
android:layout_marginTop="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/gerant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/gerant"
android:textAlignment="center"
android:textColor="#color/blue"
android:textSize="#dimen/gerant"
app:layout_constraintTop_toBottomOf="#+id/title_gerant"
android:layout_marginTop="#dimen/espace_entre_title_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/title_localisation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/localisation_title"
android:textAlignment="center"
android:textColor="#color/dark_green"
android:shadowColor="#color/black"
android:shadowRadius="1"
android:textSize="#dimen/gerant_title"
app:layout_constraintTop_toBottomOf="#+id/gerant"
android:layout_marginTop="#dimen/espace_entre_text_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/localisation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/localisation"
android:textAlignment="center"
android:textColor="#color/blue"
android:textSize="#dimen/gerant"
app:layout_constraintTop_toBottomOf="#+id/title_contact"
android:layout_marginTop="#dimen/espace_entre_title_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/title_activite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_activite"
android:textAlignment="center"
android:textColor="#color/dark_green"
android:shadowColor="#color/black"
android:shadowRadius="1"
android:textSize="#dimen/gerant_title"
app:layout_constraintTop_toBottomOf="#+id/localisation"
android:layout_marginTop="#dimen/espace_entre_text_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/activite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/activite"
android:textAlignment="center"
android:textColor="#color/blue"
android:textSize="#dimen/gerant"
app:layout_constraintTop_toBottomOf="#+id/title_activite"
android:layout_marginTop="#dimen/espace_entre_title_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/title_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_contact"
android:textAlignment="center"
android:textColor="#color/dark_green"
android:shadowColor="#color/black"
android:shadowRadius="1"
android:textSize="#dimen/gerant_title"
android:layout_marginTop="#dimen/espace_entre_text_title"
app:layout_constraintTop_toBottomOf="#+id/activite"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:id="#+id/contact_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/contact"
android:textAlignment="center"
android:textColor="#color/blue"
android:textSize="#dimen/gerant"
app:layout_constraintTop_toBottomOf="#+id/title_contact"
android:layout_marginTop="#dimen/espace_entre_title_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</LinearLayout>
</ScrollView>
</LinearLayout>
As I mentioned in the comment section, the Navigation Component deals itself with the transactions between the fragments.
Here some tips to set up your BottomNavigation bar, hope it will help you.
First of all your mobile_navigation.xml and fragment XML seems to be good.
I could fix a bit your activity_infos.xml file:
<?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">
<fragment
android:id="#+id/nav_host_fragment_activity_infos"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="Odp"
app:defaultNavHost="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:navGraph="#navigation/mobile_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
...
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
...
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Update your Left/Right constraint with Start/End here is why
Add a constraint between the bottom of the id/nav_host_fragment_activity_infos and the id/nav_view, just to be sure the fragment never be hidden by the navbar. It is optional.
Then, your activity could be updated without the FrragmentManager:
public class Infos extends AppCompatActivity {
private ActivityInfosBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// When you are using an activity you can directly use this
binding = DataBindingUtil.setContentView(this, R.layout.activity_infos);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top-level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_entreprise, R.id.navigation_produits, R.id.navigation_CGU)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_infos);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(binding.navView, navController);
}
}
I found this tutorial, it explains step by step, and it may provide more info than my "tips".
Can you please try replacing this with your activity_infos.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"
android:background="#android:color/white">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment_activity_infos"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:defaultNavHost="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
Hi I'm having trouble on making layout like this design because the navigation bar is difficult to make it become functional.
this is the design
I have created the class like this:
class MainActivity : AppCompatActivity() {
lateinit var navigationView: NavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigationView = findViewById(R.id.navigation)
navigationView.setNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}
private val mOnNavigationItemSelectedListener =
BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_user -> {
val fragment = UserFragment()
addFragment(fragment)
return#OnNavigationItemSelectedListener true
}
R.id.navigation_scaner -> {
val fragment = ScannerFragment()
addFragment(fragment)
return#OnNavigationItemSelectedListener true
}
R.id.navigation_laporan -> {
val fragment = LaporanFragment()
addFragment(fragment)
return#OnNavigationItemSelectedListener true
}
R.id.navigation_notif ->{
val fragment = NotificationFragment()
addFragment(fragment)
return#OnNavigationItemSelectedListener true
}
R.id.navigation_chat -> {
val fragment = LaporanFragment()
addFragment(fragment)
return#OnNavigationItemSelectedListener true
}
R.id.navigation_profile -> {
val fragment = LaporanFragment()
addFragment(fragment)
return#OnNavigationItemSelectedListener true
}
}
false
}
private fun addFragment(fragment: Fragment) = supportFragmentManager
.beginTransaction()
.setCustomAnimations(R.anim.design_bottom_sheet_slide_in, R.anim.design_bottom_sheet_slide_out)
.replace(R.id.content, fragment, fragment.javaClass.simpleName)
.commit()
}
and this is my layout.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:paddingBottom="20dp"
android:background="#FAFAFC"
android:paddingRight="20dp"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearnama"
android:layout_marginStart="20dp"
android:padding="25dp"
android:layout_marginTop="21dp"
android:background="#drawable/bg_white_rounded"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.cardview.widget.CardView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:id="#+id/foto_profil_operator"
android:elevation="10dp"
app:cardCornerRadius="18dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="51dp"
android:layout_height="51dp"
android:scaleType="centerCrop"
android:src="#drawable/lisa" />
</androidx.cardview.widget.CardView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins"
android:layout_marginStart="15dp"
android:layout_weight="0.2"
android:gravity="left"
android:layout_gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="16sp"
android:text="Nama Operator" />
<ImageView
android:layout_width="200dp"
android:layout_height="match_parent"
android:foregroundGravity="right"
android:layout_gravity="end"
android:layout_weight="0"
android:src="#drawable/logo" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearnama"
app:layout_constraintVertical_bias="0.497">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraint_menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_weight="0.1"
android:background="#drawable/bg_white_rounded"
android:padding="20dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MENU"
android:fontFamily="#font/poppins"
android:textColor="#color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#color/white"
app:itemIconTint="#color/black"
app:itemTextColor="#color/black"
app:layout_constraintBottom_toTopOf="#+id/pp2k"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
app:menu="#menu/navigation">
</com.google.android.material.navigation.NavigationView>
<TextView
android:id="#+id/pp2k"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="navigation"
android:textColor="#color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<FrameLayout
android:id="#+id/frameLayout"
android:layout_weight="0.5"
android:layout_marginTop="20dp"
android:background="#drawable/bg_white_rounded"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
</FrameLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I have tried several navigation listener such as navigationView.setNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
But it won't work. Maybe someone know how I can solve my problem? Thanks in advance
I'm trying to create a Two-Way Data binding connection with a LiveData object of a modal class. But when I tries to read the values of user it turns out to be null. Here is my code -
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModal: MainActivityViewModal = ViewModelProvider(
this,
MainActivityViewModalFactory()
).get(MainActivityViewModal::class.java)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(
this,
R.layout.activity_main
).apply {
this.lifecycleOwner = this#MainActivity
this.viewModal = viewModal
}
}}
Here is the Layout file -
<data>
<variable
name="viewModal"
type="com.weaponx.databindingexample.MainActivityViewModal" />
</data>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/ivLogo"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="#string/logo"
android:tint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#android:drawable/ic_menu_myplaces" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ivLogo">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.10" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tilUserName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/edit_text_bg"
android:hint="#string/user_name"
android:padding="5dp"
app:layout_constraintEnd_toStartOf="#id/guideline2"
app:layout_constraintStart_toEndOf="#id/guideline1"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/tietUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:inputType="text"
android:text="#={viewModal.user.userName}" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tilPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/edit_text_bg"
android:hint="#string/password"
android:padding="5dp"
app:layout_constraintEnd_toStartOf="#id/guideline2"
app:layout_constraintStart_toEndOf="#id/guideline1"
app:layout_constraintTop_toBottomOf="#id/tilUserName">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/tietPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:inputType="textPassword"
android:text="#={viewModal.user.password}" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:background="#drawable/button_bg"
android:text="#string/login"
android:onClick="#{() -> viewModal.login()}"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#id/guideline2"
app:layout_constraintStart_toEndOf="#id/guideline1"
app:layout_constraintTop_toBottomOf="#id/tilPassword" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.90" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the ViewModal
class MainActivityViewModal : ViewModel() {
var user = MutableLiveData<User>()
fun login() {
println("Login Success ${user.value}")
}}
And here is the Modal Class
class User() {
var userName: String = ""
var password: String = ""
override fun toString(): String {
return "User(userName='$userName', password='$password')"
}}
Please tell me any solution or a good way around to this problem
In the MainActivityViewModal define user as just User type rather as MutableLiveData.