logo
down
shadow

Androidx Navigation View - `setNavigationItemSelectedListener` Doesn't Work


Androidx Navigation View - `setNavigationItemSelectedListener` Doesn't Work

By : Reimu Hakure
Date : November 28 2020, 08:01 AM
around this issue I Figured it out guys.
Just in case if someone needs it, I'm posting it here.
code :
navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
        return false;
    }
});

navController = Navigation.findNavController(this, R.id.nav_host_fragment);

NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout);

NavigationUI.setupWithNavController(navigationView, navController);
navigationView = findViewById(R.id.navigationView);

navController = Navigation.findNavController(this, R.id.nav_host_fragment);

NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout);

NavigationUI.setupWithNavController(navigationView, navController);

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
        return false;
    }
});


Share : facebook icon twitter icon
Navigation Drawer: setNavigationItemSelectedListener is not working.

Navigation Drawer: setNavigationItemSelectedListener is not working.


By : Emil Persson
Date : March 29 2020, 07:55 AM
Adding table to navigation controller view programatically doesnt work

Adding table to navigation controller view programatically doesnt work


By : mkard
Date : October 14 2020, 09:25 AM
like below fixes the issue whenever your creating a view in code and tries to add constraints to that view you have to remember to set the translatesAutoresizingMaskIntoConstraints
code :
func setConstraints(){

    self.view.addSubview(statisticsTable);
    // try to add this before you add your constraints to the view
    statisticsTable.translatesAutoresizingMaskIntoConstraints = false

    statisticsTable.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true;
    statisticsTable.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true;
    statisticsTable.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true;
    statisticsTable.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true;
}
AndroidX DataBinding API doesnt work

AndroidX DataBinding API doesnt work


By : user1582181
Date : March 29 2020, 07:55 AM
This might help you Use
val binding : FragentReportDetailsBinding = DataBindingUtil.inflate( inflater, R.layout.fragent_report_details, container, false)
Cause: androidx.navigation.safeargs can only be used with an androidx project

Cause: androidx.navigation.safeargs can only be used with an androidx project


By : SinForest
Date : March 29 2020, 07:55 AM
it fixes the issue As per the Migrate to AndroidX, your gradle.properties file must contain the following lines:
code :
android.useAndroidX=true
android.enableJetifier=true
I'm having an issue in Navigation view item click listener in androidx

I'm having an issue in Navigation view item click listener in androidx


By : noematic
Date : March 29 2020, 07:55 AM
Any of those help If you are using androidX then you have to use the NavController for navigation through fragments.
I will post the code I have tried
code :
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

NavController mNavController;
Navigation mNavigation;
BottomNavigationView mBottomNavigationView;
NavigationView mNavigationView;
DrawerLayout mDrawerLayout;
Toolbar toolbar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    mDrawerLayout = findViewById(R.id.drawer_layout);
    mBottomNavigationView = findViewById(R.id.bottom_navigation_view);
    mNavigationView = findViewById(R.id.navigation_view);


    //mNavController = Navigation.findNavController(this,R.id.host_fragment);
    mNavController = Navigation.findNavController(this,R.id.host_fragment);
    NavigationUI.setupWithNavController(mBottomNavigationView,Navigation.findNavController(this,R.id.host_fragment));

    NavigationUI.setupActionBarWithNavController(this, mNavController, mDrawerLayout);

    NavigationUI.setupWithNavController(mNavigationView,mNavController);

    mNavigationView.setNavigationItemSelectedListener(this);
    //NavigationUI.setupActionBarWithNavController(this,Navigation.findNavController(this,R.id.host_fragment));

}

@Override
public boolean onSupportNavigateUp() {
   /*return mNavController.popBackStack(R.id.host_fragment,false);*/
    return NavigationUI.navigateUp(Navigation.findNavController(this, R.id.host_fragment), mDrawerLayout);
}


@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBackPressed() {
    NavController navController = Navigation.findNavController(this, R.id.host_fragment);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if (Objects.requireNonNull(navController.getCurrentDestination()).getId() == R.id.nav_first) {
            finishAffinity();
        } else {
            mNavController.popBackStack(R.id.nav_first, false);
        }
    }
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()){
        case R.id.home:
            onBackPressed();
    }

    return super.onOptionsItemSelected(item);
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    mDrawerLayout.closeDrawers();

    int id = menuItem.getItemId();

    switch (id){

        //Nav drawer items
        case R.id.profile:
            mNavController.navigate(R.id.profile);
            break;


        case R.id.features:
            mNavController.navigate(R.id.features);
            break;

        case R.id.signOut:
            finishAffinity();
            break;
    }
    return true;
}
<?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">


<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/white"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="10">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/toolbar_title"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="8"
                android:gravity="center"
                android:padding="10dp"
                android:text="@string/jetpack_example"
                android:textColor="#000"
                android:textSize="15sp" />

            <ImageView
                android:id="@+id/search_bar"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="2"
                android:padding="10dp"
                android:src="@drawable/about_icon" />

        </LinearLayout>


    </androidx.appcompat.widget.Toolbar>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:itemBackground="@android:color/white"
        app:labelVisibilityMode="selected"
        app:layout_constraintBottom_toBottomOf="@+id/host_fragment"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/menu_drawer" />

    <fragment
        android:id="@+id/host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navigation_graph"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />


    </androidx.constraintlayout.widget.ConstraintLayout>


   <com.google.android.material.navigation.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/nav_drawer" />


</androidx.drawerlayout.widget.DrawerLayout>
<?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/navigation_graph"
app:startDestination="@id/nav_first">

<fragment
    android:id="@+id/nav_first"
    android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.FirstFragment"
    android:label="@string/first"
    tools:layout="@layout/fragment_first" />
<fragment
    android:id="@+id/nav_second"
    android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.SecondFragment"
    android:label="@string/second"
    tools:layout="@layout/fragment_second" />
<fragment
    android:id="@+id/nav_third"
    android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.ThirdFragment"
    android:label="@string/third"
    tools:layout="@layout/fragment_third" />
<fragment
    android:id="@+id/nav_fourth"
    android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.FourthFragment"
    android:label="@string/fourth"
    tools:layout="@layout/fragment_fourth" />
<fragment
    android:id="@+id/profile"
    android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.ProfileFragment"
    android:label="@string/profile"
    tools:layout="@layout/fragment_profile" />
<fragment
    android:id="@+id/features"
    android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.FeaturesFragment"
    android:label="@string/features"
    tools:layout="@layout/fragment_features" />
Related Posts Related Posts :
  • Why is it mandatory for ternary operations to return a value?
  • How can I make my listview rows different heights
  • Array declaration trick : Is it bad to use it?
  • Calling different classes with if else statements
  • Free API for speech recognition system
  • Java: Objects in JFrame are messed up
  • Is there a similar of MessageBox of ExtJS in ADF Faces
  • Passing HttpServletResponse in Spring Boot controller method
  • Failed to load libraries: [netty_tcnative_linux_arm_32, netty_tcnative_linux_arm_32_fedora, netty_tcnative_arm_32, netty
  • How to build a small documents storage?
  • Getting pixel position of a text in an image
  • How do I get a list of Methods called from a Class in Eclipse IDE?
  • Convert string to day of week (not exact date)
  • Unable to understand how rounding is working for decimal digits (Java)
  • How to avoid java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.copy(Ljava/io/InputStream;Ljava/io/OutputStream;)
  • JTable Renderer not working with RowSorter?
  • From java.util.concurrent.Future<T> to play.libs.F.Promise<T>: How to do that in Java?
  • jUnit tests work in Eclipse, but fail in ant - java.lang.RuntimeException: Stub
  • How do i add a tooltip to existing gwt Textbox?
  • actor replying to non-actor
  • Google Cloud Messaging - server connection
  • Which classes are unmodifiable and cannot be intrumented and is there a way to bypass it?
  • Java & Jersey 1.17: Custom default response headers
  • Java rounding error
  • Setting custom RejectedExecutionHandler for ThreadPoolExecutor after executing some tasks?
  • Arquillian test class variables lose value
  • java.util.ConcurrentModificationException while inserting in ArrayList
  • How to write generic Java API without method Overloading
  • Java Serialization / Deserialization of an ArrayList works only on first program execution
  • Conway's Game of Life's rules dont apply properly
  • call a java method when Click on a html button without using javascript
  • Why does SSLSocketFactory.createSocket returns a Socket instead of SSLSocket?
  • How to schedule a task in Tomcat
  • compare two pdf files (approach) using java
  • What is the use of allowCoreThreadTimeout( ) in ThreadPoolExecutor?
  • Describe the Strategy Design Pattern in Java?
  • MongoDB too many open connections
  • Issue with generate JAXB classes with value constructors
  • Moving from one frame to another in java swing
  • Get a thread by Id
  • how to open front camera and back camera at same time?
  • Messed up with Java Declaration
  • Extracting chart in EXcel as image using java
  • How to get latitude and longitude in Android phone?
  • Do GC pauses and kill -STOP produce the same behavior?
  • Android HTML.ImageGetter Images resized
  • The way to instantiate map<String, List<String>> in Java
  • Java Making objects move while buttons held
  • after compiling code JFrame does not appear
  • Netbeans project, JFrames ran from the main JFrame close all JFrames in project
  • Square Retrofit Client: How to enable/disable followRedirects? How to intercept redirect URL?
  • Java Circular Reference - Can’t Compile
  • Mouse click on specific time JAVA
  • android 4 onCreateDialog return type incompatible when overriding
  • Java Implementation of Oracle's DBMS_UTILITY.GET_HASH_VALUE Function
  • javac cannot be run, and furthermore does not seem installed
  • Function to obtain a thread given its ID
  • Java File path to Executable has spaces
  • Minimum Oracle Java VM heap memory setting?
  • Elegant way to create one of a large number of classes
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org