Initiale Fragment in bottom Navigation has duplicated [ANDROID STUDIO] - android

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>

Related

Login button and inputs fields aren't work in android studio

First of all, I wanna say sorry about my English.
I am trying to build my first android application using android studio. I created the main page containing two taps one for login activity and the other one for the About section.
what I did exactly was create the Login activity for the fragments to hold the tabs and the basic design and one layout only without the java file for the about_tab_fragment section and finally the login_tab activity.
the problem is that I can not get the input data from the view and no action happened when I click on the login button.(and there is no error in the Run).
and this is my code:
Login activity (Layout)
<?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:layout_height="match_parent"
tools:context=".Login">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:src="#drawable/login_header"
app:layout_constraintHeight_percent=".27"
android:scaleType="centerCrop"
app:layout_constraintVertical_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintHeight_percent=".78"
app:layout_constraintVertical_bias="1"
android:background="#drawable/desigen_for_login_page"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintVertical_bias="0"
app:layout_constraintHeight_percent=".7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tab_layout"/>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/desigen_for_login_page"
app:layout_constraintVertical_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:src="#drawable/google_img"
android:elevation="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view_pager" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:src="#drawable/facebook_img"
app:layout_constraintHorizontal_bias="1"
android:layout_marginRight="10dp"
android:elevation="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/fab_google"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view_pager" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_insta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:src="#drawable/insta_img"
app:layout_constraintHorizontal_bias="0"
android:layout_marginLeft="10dp"
android:elevation="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/fab_google"
app:layout_constraintTop_toBottomOf="#+id/view_pager" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Login activity (Java)
public class Login extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
FloatingActionButton facebook,google,instagram;
float v = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
tabLayout = findViewById(R.id.tab_layout);
viewPager = findViewById(R.id.view_pager);
facebook = findViewById(R.id.fab_facebook);
google = findViewById(R.id.fab_google);
instagram = findViewById(R.id.fab_insta);
tabLayout.addTab(tabLayout.newTab().setText("Login"));
tabLayout.addTab(tabLayout.newTab().setText("About"));
tabLayout.setTabGravity(tabLayout.GRAVITY_FILL);
final LoginAdapterClass adapter = new LoginAdapterClass(getSupportFragmentManager(),this,tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//For animations
facebook.setTranslationY(300);
google.setTranslationY(300);
instagram.setTranslationY(300);
tabLayout.setTranslationY(300);
facebook.setAlpha(v);
google.setAlpha(v);
instagram.setAlpha(v);
tabLayout.setAlpha(v);
facebook.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(400).start();
google.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(600).start();
instagram.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(800).start();
tabLayout.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(100).start();
}
}
about_tab_fragment (layout)
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"
android:textSize="30dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Login_tab (Layout)
<?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:layout_height="match_parent"
tools:context=".LoginTab">
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="24dp"
android:layout_marginTop="92dp"
android:layout_marginEnd="24dp"
android:background="#drawable/custom_inputs"
android:drawableStart="#drawable/custom_email_icon"
android:drawablePadding="12dp"
android:ems="10"
android:hint="Email Address"
android:inputType="textEmailAddress"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:background="#drawable/custom_inputs"
android:drawableStart="#drawable/custom_lock_icon"
android:drawablePadding="12dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/email" />
<Button
android:id="#+id/loginBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.758"
app:layout_constraintWidth_percent="0.8" />
<TextView
android:id="#+id/forgetPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forget Password"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintVertical_bias="0"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/password"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/password" />
</androidx.constraintlayout.widget.ConstraintLayout>
My problem occurs here:
I can not take the text from the inputs email and password plus that I can not add event to the loginBtn. I tried to put Toast in the On Click Listener but nothing happened.
Login_tab (Java)
public class LoginTab extends AppCompatActivity {
EditText emailLogin,passwordLogin;
Button loginBtn;
TextView forgetPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_tab);
emailLogin = findViewById(R.id.email);
passwordLogin = findViewById(R.id.password);
loginBtn = findViewById(R.id.loginBtn);
forgetPassword = findViewById(R.id.forgetPassword);
loginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Login.this,"This is the login button",Toast.LENGTH_LONG).show();
if(emailLogin.getText().toString().isEmpty()){
emailLogin.setError("Email is required");
return;
}
if(passwordLogin.getText().toString().isEmpty()){
passwordLogin.setError("Password is required");
return;
}
}
});
}
}
Can any one help me to fix the problem.
I did a lot of searches but I could not find any answer.
I found the problem.
the problem because I have to put the action in the class loginTab that extends Fragment not AppCompatActivity.
public class LoginTabFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.activity_login_tab,container,false);
Button b = root.findViewById(R.id.loginBtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(root.getContext(), "This is the Login button....",Toast.LENGTH_LONG).show();
}
});
return root;
}
}

Android drawer layout header - cannot access it

In my past project from the beggining of the year I have set the content of my drawerHeader in the mainActivity code. I am rewriting it and now it seems impossible to access the header through binding in MainActivity.
I have a usual drawer layout in the activity -> navigation view -> drawer header.
I used to access it through simple binding.layoutDrawer.userDataHeader but it doesn't find userDataHeader no matter what I do. What am I doing wrong?
main activity layout
<?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="mainActivity"
type="com.kerubyte.engagecommerce.presentation.ui.activity.MainActivity" />
</data>
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/layout_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".presentation.ui.activity.MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/layout_activity_main_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".presentation.ui.activity.MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/layout_actionbar_root"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="#color/primaryColor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/image_open_drawer_action"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="10dp"
android:onClick="#{() -> mainActivity.openDrawerMenu()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_hamburger" />
<ImageView
android:id="#+id/image_open_profile_action"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginEnd="24dp"
android:onClick="#{() -> mainActivity.onProfileClick()}"
app:layout_constraintBottom_toBottomOf="#+id/image_open_cart_action"
app:layout_constraintEnd_toStartOf="#+id/image_open_cart_action"
app:layout_constraintTop_toTopOf="#+id/image_open_cart_action"
app:srcCompat="#drawable/ic_person" />
<ImageView
android:id="#+id/image_open_cart_action"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginEnd="12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_cart" />
<TextView
android:id="#+id/text_user_cart_items_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="0"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="#+id/image_open_cart_action"
app:layout_constraintEnd_toEndOf="#+id/image_open_cart_action"
app:layout_constraintHorizontal_bias="0.55"
app:layout_constraintStart_toStartOf="#+id/image_open_cart_action"
app:layout_constraintTop_toTopOf="#+id/image_open_cart_action" />
</androidx.constraintlayout.widget.ConstraintLayout>
<fragment
android:id="#+id/nav_host_fragment"
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="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/layout_actionbar_root"
app:layout_constraintVertical_bias="1.0"
app:navGraph="#navigation/navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/layout_navigation_menu"
app:headerLayout="#layout/drawer_header"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:maxWidth="300dp"
app:menu="#menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
drawer header layout
<?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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/user_data_header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/image_user_avatar"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:riv_oval="true"/>
<View
android:id="#+id/view_avatar_support"
android:layout_width="1dp"
android:layout_height="1dp"
app:layout_constraintBottom_toBottomOf="#+id/image_user_avatar"
app:layout_constraintEnd_toEndOf="#+id/image_user_avatar"
app:layout_constraintStart_toStartOf="#+id/image_user_avatar"
app:layout_constraintTop_toTopOf="#+id/image_user_avatar" />
<TextView
android:id="#+id/text_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="#+id/text_user_email"
app:layout_constraintStart_toEndOf="#+id/image_user_avatar" />
<TextView
android:id="#+id/text_user_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="#+id/image_user_avatar"
app:layout_constraintStart_toEndOf="#+id/image_user_avatar" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Main Activity
#AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private val mainViewModel: ActivityMainViewModel by viewModels()
lateinit var binding: ActivityMainBinding
private lateinit var navigationView: NavigationView
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil
.setContentView(
this,
R.layout.activity_main
)
navigationView = binding.layoutNavigationMenu
navController = Navigation.findNavController(
this,
R.id.nav_host_fragment
)
NavigationUI.setupWithNavController(navigationView, navController)
setBindings()
setupObserver()
}
private fun updateDrawerHeader(user: User) {
binding.layoutDrawer.userDataHeader.text_first_name_value_header.text = user.firstName
binding.layoutDrawer.userDataHeader.text_last_name_value_header.text = user.lastName
binding.layoutDrawer.userDataHeader.text_email_value_header.text = user.email
}
}
And Im trying to update the header with
private fun updateDrawerHeader(user: User) {
binding.layoutDrawer.userDataHeader.text_first_name_value_header.text = user.firstName
binding.layoutDrawer.userDataHeader.text_last_name_value_header.text = user.lastName
binding.layoutDrawer.userDataHeader.text_email_value_header.text = user.email
}
userDataHeader I cannot find.

How to Make Static Navigation Android Tablet? like this design

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

Why aren't the fragments showing up from my navigation drawer?

Here lads, I've honestly been trying to do this every which way but for some reason the different fragments just won't show up properly. I've gotten as far to the point where a fragment shows up but its on top of the home fragment. I'm so frustrated. I've been at this for DAYS
Here's the important part of the MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
navigationView.setCheckedItem(R.id.nav_home);
}
}
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
/* mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_members, R.id.nav_map,
R.id.nav_news, R.id.nav_livestream, R.id.nav_election,
R.id.nav_share, R.id.nav_login)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.bringToFront(); */
// navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_home:
Toast.makeText(getApplicationContext(), "Home is selected", Toast.LENGTH_LONG).show();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
break;
case R.id.nav_members:
Toast.makeText(getApplicationContext(), "Members are selected",Toast.LENGTH_LONG).show();
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new MembersFragment()).commit();
break;
case R.id.nav_map:
Toast.makeText(getApplicationContext(), "Map is selected", Toast.LENGTH_LONG).show();
FragmentManager mapfm= getSupportFragmentManager();
MapFragment mapFragment = new MapFragment();
mapfm.beginTransaction().replace(R.id.drawer_layout, mapFragment).commit();
break;
case R.id.nav_news:
Toast.makeText(getApplicationContext(), "News is selected", Toast.LENGTH_LONG).show();
Intent newsStart =new Intent(MainActivity.this, MainActivityGaeilge.class);
startActivity(newsStart);
break;
case R.id.nav_livestream:
Toast.makeText(getApplicationContext(), "Livestream is selected", Toast.LENGTH_LONG).show();
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new LivestreamFragment()).commit();
break;
case R.id.nav_election:
Toast.makeText(getApplicationContext(), "Election info is selected", Toast.LENGTH_LONG).show();
//Intent electionopen = new Intent(MainActivity.this, ElectionFragment.class);
// startActivity(electionopen);
FragmentManager electionfm= getSupportFragmentManager();
ElectionFragment electionFragment = new ElectionFragment();
electionfm.beginTransaction().replace(R.id.home_page, electionFragment).commit();
break;
case R.id.nav_share:
Toast.makeText(getApplicationContext(), "Share is selected", Toast.LENGTH_LONG).show();
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new SendFragment()).commit();
//Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
//Intent sharestart=new Intent(MainActivity.this,this.class);
// startActivity(sharestart);
break;
case R.id.nav_login:
Toast.makeText(getApplicationContext(), "Login is selected", Toast.LENGTH_LONG).show();
break;
// case R.id.nav_election:
// Intent electionopen = new Intent(MainActivity.this, ElectionFragment.class);
// startActivity(electionopen);
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
Here's the 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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.drawerlayout.widget.DrawerLayout>
Here's the app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
android:id="#+id/home_page"
tools:context=".MainActivity"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And here's the content_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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
<TextView
android:id="#+id/textView"
android:layout_width="56dp"
android:layout_height="27dp"
android:layout_marginStart="82dp"
android:layout_marginTop="227dp"
android:layout_marginEnd="273dp"
android:layout_marginBottom="477dp"
android:text="Text: "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="168dp"
android:layout_marginTop="218dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="466dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="138dp"
android:layout_marginTop="328dp"
android:layout_marginEnd="149dp"
android:layout_marginBottom="355dp"
android:onClick="OnLogin"
android:text="Login Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="274dp"
android:layout_marginEnd="265dp"
android:layout_marginBottom="438dp"
android:onClick="OnLogin"
android:text="Password:"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="169dp"
android:layout_marginTop="268dp"
android:layout_marginEnd="29dp"
android:layout_marginBottom="418dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
PLEASE HELP IM SO STRESSED
Currently you have two containers into which a Fragment might go:
You use the support FragmentManager all over your MainActivity with a FrameLayout with android:id="#+id/fragment_container" to add and replace various Fragments
You seem to have set up a navigation graph, at least there is a NavHostFragment in content_main.xml and some code related to NavController in comments in MainActivity
It looks like you add HomeFragment twice: once as start Fragment in the navigation graph and the other time in onCreate() via the support FragentManager
So when you're navigating through your app, you see the Fragments displayed on top of the HomeFragment instance in the NavHostFragment
Solution: use the support FragmentManager or the NavController consistently throughout your app and remove the container which belongs to the other approach (either the NavHostFragment or the FrameLayout)
The following suggestions assume you want to use FragmentManager:
Remove the FrameLayout serving as container for the Fragments from activity_main.xml:
<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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
... and add it to content_main.xml in place of the NavHostFragment
<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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/textView"
android:layout_width="56dp"
android:layout_height="27dp"
android:layout_marginStart="82dp"
android:layout_marginTop="227dp"
android:layout_marginEnd="273dp"
android:layout_marginBottom="477dp"
android:text="Text: "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="168dp"
android:layout_marginTop="218dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="466dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="138dp"
android:layout_marginTop="328dp"
android:layout_marginEnd="149dp"
android:layout_marginBottom="355dp"
android:onClick="OnLogin"
android:text="Login Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="274dp"
android:layout_marginEnd="265dp"
android:layout_marginBottom="438dp"
android:onClick="OnLogin"
android:text="Password:"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="169dp"
android:layout_marginTop="268dp"
android:layout_marginEnd="29dp"
android:layout_marginBottom="418dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Please note that you can delete the file res/navigation/mobile-navigation.xml since it was only used by the NavHostFragment

BottomNavigationBar goes down partially when I change fragments

I use BottomNavigationBar(bnb) with Navigation(android jetpack). It's a single-activity app. There are 3 fragments. One of them is starting. When I perform the navigation from starting fragment to any outher, bnb goes down for half of it hight. When I go back or perform navigation between not starting fragments everything is ok.
Activity
<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"
android:background="#color/colorPrimary"
tools:context=".ui.activities.MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemIconSize="#dimen/bnb_icon_size"
android:background="?attr/colorPrimary"
app:itemIconTint="#drawable/selector"
app:elevation="#dimen/margin_small"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/bottom_navigation_menu" />
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constrainedWidth="true"
app:layout_constrainedHeight="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/bottom_navigation_view"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph"/>
</androidx.constraintlayout.widget.ConstraintLayout>
one of the fragments
<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="callback"
type="(...)ui.info.InfoFragmentCallback" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/set_coord_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark"
android:fitsSystemWindows="true">
<TextView
android:id="#+id/info_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_email"
android:textColor="#color/colorWhiteOverlayTransparency45"
android:textSize="#dimen/text_size_small"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/info_contact_us_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_medium"
android:gravity="center"
android:text="#string/title_contact_us"
android:textColor="#color/colorWhiteOverlayTransparency45"
android:textSize="#dimen/text_size_medium"
android:textStyle="italic"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toTopOf="#+id/info_email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/info_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_large"
android:gravity="center"
android:text="#string/app_version"
android:textColor="#color/colorWhiteOverlayTransparency45"
android:textSize="#dimen/text_size_medium"
android:textStyle="italic"
app:layout_constraintBottom_toTopOf="#+id/info_email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/info_app_rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_medium"
android:gravity="center"
android:text="#string/title_rate_app"
android:textColor="#color/colorWhiteOverlayTransparency45"
android:textSize="#dimen/text_size_medium"
android:textStyle="italic"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toTopOf="#+id/info_google_badge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/info_google_badge"
android:layout_width="wrap_content"
android:layout_height="#dimen/icon_size_large"
android:elevation="#dimen/margin_medium"
android:onClick="#{(view)->callback.onAppRateClicked()}"
android:src="#drawable/google_play_badge"
app:layout_constraintBottom_toTopOf="#+id/info_privacy_policy"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/info_email"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/info_privacy_policy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_huge"
android:onClick="#{(view)->callback.onPrivacyClicked()}"
android:text="#string/privacy_policy_title"
android:textColor="#color/colorSecondary"
android:textSize="#dimen/text_size_small"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
MainActivity
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private NavController mNavController;
private final BottomNavigationView.OnNavigationItemReselectedListener mOnNavigationItemReselectedListener = item -> {
/*multiple touches may must nothing*/
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavController = Navigation.findNavController(this, R.id.nav_host_fragment);
bottomNavigationView = findViewById(R.id.bottom_navigation_view);
NavigationUI.setupWithNavController(bottomNavigationView, mNavController);
initBottomNavigationMenu();
}
private void initBottomNavigationMenu() {
bottomNavigationView.setOnNavigationItemReselectedListener(mOnNavigationItemReselectedListener);
}
public void performNavigation(int navId) {
mNavController.navigate(navId);
}
#Override
protected void onDestroy() {
super.onDestroy();
bottomNavigationView.setOnNavigationItemReselectedListener(null);
}
}
bnb menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/starting_fragment"
android:icon="#drawable/ic_image"
android:title=""
app:showAsAction="ifRoom" />
<item
android:id="#+id/favorites_fragment"
android:icon="#drawable/ic_star"
android:title=""
app:showAsAction="ifRoom"/>
<item
android:id="#+id/info_fragment"
android:icon="#drawable/ic_info"
android:title=""
app:showAsAction="ifRoom"/>
</menu>
Where can be a problem?

Categories

Resources