Problem when trying to click object of navigationView Android Studio

0

I have a problem trying to press an item of navigation View . What I do is create the objects directly from the Activity Main since the purpose is to bring data from an external database and be completed.

Now the inconvenience is the following, I create the objects but when I want to go through it and show me that it just pointed that object it seems that it does not point it, it is more points to a webview that is in the background that brings a map of google maps and I move the pointer so I think I'm doing wrong creation of objects.

Then I leave the part of the code where I work both the creation and reception of objects (it should be noted that the navigation View believes it in the folder layout but only has the main structure nothing else.).

Main_activity.xml

<?xml version="1.0" encoding="utf-8"?>

android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_marginBottom="120dp"
    android:layout_marginLeft="100dp"
    app:menu="@menu/activity_main_drawer"
    app:itemIconTint="@android:color/black"
    android:backgroundTint="#f2f2f2"
    app:itemTextColor="#304769"
    />


<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
   />


<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigationsuperior"
    android:layout_width="1dp"
    android:layout_height="1dp"
    android:layout_marginTop="400dp"
    android:layout_marginBottom="50dp"
    android:background="?android:attr/windowBackground"
    android:backgroundTint="#304769"
    app:itemIconTint="@android:color/darker_gray"
    app:itemTextColor="@android:color/darker_gray"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigationsuperior" />


<android.support.design.widget.BottomNavigationView

    android:id="@+id/navigation"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:background="?android:attr/windowBackground"
    android:backgroundTint="#304769"
    android:layout_marginTop="450dp"
    app:itemIconTint="@android:color/darker_gray"
    app:itemTextColor="@android:color/darker_gray"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />

MainActivity.java

  public NavigationView.OnNavigationItemSelectedListener LonNavigationItemSelectedListener;{ //SE UTLIZA PARA EL MANU LATERAL

    LonNavigationItemSelectedListener = new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            // Handle navigation view item clicks here.
           // int id = menuItem.getItemId();
            int id = menuItem.getItemId();


          /*  if (id == R.id.nav_camera) {
                // Handle the camera action
            } else if (id == R.id.nav_gallery) {

            } else if (id == R.id.nav_slideshow) {

            } else if (id == R.id.nav_manage) {

            } else if (id == R.id.nav_share) {

            } else if (id == R.id.nav_send) {

            }*/

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.container);
            drawer.closeDrawer(GravityCompat.START);
            return true;

        }

    };
}

private void addMenuItemInNavMenuDrawer() {   //se utilizara para poner en el menù lateral los vehiculos por usuario
    NavigationView navView = (NavigationView) findViewById(R.id.nav_view);

    Menu menu = navView.getMenu();
    Menu submenu = menu.addSubMenu(1,1,1,"Vehículos asociados");

    submenu.add(1,1,1,"Automovil").setIcon(R.drawable.auto_icon);


  /*  submenu.add("Automovil").setIcon(R.drawable.auto_icon).getItemId();
    submenu.add("Taxi").setIcon(R.drawable.taxi_icon).getItemId();
    submenu.add("Camión").setIcon(R.drawable.camion_icon).getItemId();
    submenu.add("Motocicleta").setIcon(R.drawable.moto_icon).getItemId();*/



    // navView.invalidate();
}
    
asked by Luis 14.11.2018 в 15:15
source

1 answer

0

Dear, I found the answer, in the first instance the big problem is that in the XML navigation it is above the webView, and that did not let the selection be captured and then modify the java files that I share next.

<?xml version="1.0" encoding="utf-8"?>

android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">



<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
   />


<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigationsuperior"
    android:layout_width="1dp"
    android:layout_height="1dp"
    android:layout_marginTop="400dp"
    android:layout_marginBottom="50dp"
    android:background="?android:attr/windowBackground"
    android:backgroundTint="#304769"
    app:itemIconTint="@android:color/darker_gray"
    app:itemTextColor="@android:color/darker_gray"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigationsuperior" />


<android.support.design.widget.BottomNavigationView

    android:id="@+id/navigation"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:background="?android:attr/windowBackground"
    android:backgroundTint="#304769"
    android:layout_marginTop="450dp"
    app:itemIconTint="@android:color/darker_gray"
    app:itemTextColor="@android:color/darker_gray"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />




<!--<android.support.design.widget.BottomNavigationView
    android:backgroundTint="@color/colorPrimary"
    android:id="@+id/navigationinferior"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigationinferior" /> -->

<android.support.design.widget.NavigationView
    android:id="@+id/activity_main_drawer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_marginBottom="120dp"
    android:layout_marginLeft="100dp"
    android:background="?android:attr/windowBackground"
    app:itemIconTint="@android:color/black"
    android:backgroundTint="#f2f2f2"
    app:itemTextColor="#304769"
    app:menu="@menu/activity_main_drawer"
    />

MainActivity.java

    private NavigationView.OnNavigationItemSelectedListener lOnNavigationItemSelectedListener //SE UTLIZA PARA EL MANU LATERAL
        = new NavigationView.OnNavigationItemSelectedListener() {



        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            // Handle navigation view item clicks here.
             //int id = menuItem.getItemId();
             int id = item.getItemId();
            System.out.println("INGRESó-->  ");


            if (id == 1) {
                // Handle the camera action
                System.out.println("INGRESó 1-->  ");
            } else if (id == 2) {
                System.out.println("INGRESó 2-->  ");
            } else if (id == 3) {
                System.out.println("INGRESó 3-->  ");
            } else if (id == 4) {
                System.out.println("INGRESó 4-->  ");
            } else if (id == 5) {
                System.out.println("INGRESó 5 -->  ");
            } else if (id == 6) {
                System.out.println("INGRESó 6 -->  ");
            }

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.container);
            drawer.closeDrawer(GravityCompat.START);
            return true;

        }
    };


private void addMenuItemInNavMenuDrawer() {   //se utilizara para poner en el menù lateral los vehiculos por usuario
    NavigationView navView = (NavigationView) findViewById(R.id.activity_main_drawer);

    Menu menulateral = navView.getMenu();

   Menu submenu = menulateral.addSubMenu("Vehículos asociados");

 //  menulateral.add(0, 1, 0, "automovil").setIcon(R.drawable.auto_icon).getItemId();
    submenu.add(1, 1, 0, "automovil").setIcon(R.drawable.auto_icon).getItemId();
    submenu.add(1, 2, 0, "Taxi").setIcon(R.drawable.taxi_icon).getItemId();
    submenu.add(1, 3, 0, "Camion").setIcon(R.drawable.camion_icon).getItemId();
    submenu.add(1, 4, 0, "Moto").setIcon(R.drawable.moto_icon).getItemId();

    /*  submenu.add("Automovil").setIcon(R.drawable.auto_icon).getItemId();
    submenu.add("Taxi").setIcon(R.drawable.taxi_icon).getItemId();
    submenu.add("Camión").setIcon(R.drawable.camion_icon).getItemId();
    submenu.add("Motocicleta").setIcon(R.drawable.moto_icon).getItemId();*/


    navView.invalidate();
};

   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    NavigationView navigationlateral = (NavigationView) 
    findViewById(R.id.activity_main_drawer);




    navigationlateral.setNavigationItemSelectedListener
    (lOnNavigationItemSelectedListener);
    navigationlateral.setItemIconTintList(null);

    addMenuItemInNavMenuDrawer();
  }
    
answered by 14.11.2018 в 19:16