[英]Clicklistner not working in navigation drawer layout
When i click on home or on website menu nothing happens or triggers even when i have putted toast on click listner当我点击主页或网站菜单时,即使我已将吐司放在点击列表上,也没有任何反应或触发
This my code Please check it help me.
这是我的代码请检查它帮助我。 i have uploaded my code files of mainactivity.java, activity_main.xml and side_menu.xml check the code and let me know that what is error and also solution of that error.
我已经上传了 mainactivity.java、activity_main.xml 和 side_menu.xml 的代码文件,检查代码并让我知道什么是错误以及该错误的解决方案。
MainActivity.java
package com.example.collegeconnect;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
TextView logintxt,drawerUsername,drawerEmail;
private static String ipv4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ipv4 =getString(R.string.ipv4);
toolbar = findViewById(R.id.Toolbar1);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.mainDrawer);
navigationView = findViewById(R.id.navigationView);
logintxt = findViewById(R.id.Logintxt);
// defining headerview of drawer layout
View headerView = navigationView.getHeaderView(0);
drawerEmail = headerView.findViewById(R.id.drawerEmail);
drawerEmail.setText("19BEIT54013@vsitr.ac.in");
drawerUsername = headerView.findViewById(R.id.drawerUsername);
drawerUsername.setText("Jaivik22");
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
logintxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,LoginForm.class);
startActivity(i);
}
});
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.home22:
Toast.makeText(getApplicationContext(),"Click on home",Toast.LENGTH_SHORT).show();
break;
case R.id.website:
Toast.makeText(MainActivity.this,"Click on Website",Toast.LENGTH_SHORT).show();
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainDrawer"
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"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="@layout/header"
app:menu="@menu/sidemenu"
android:layout_gravity="start"
/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/Toolbar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/Logintxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Click to Login"
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" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
This is my XML file for menu item.
这是我的菜单项 XML 文件。 The id are the same as I declared in the XML file.
id 与我在 XML 文件中声明的相同。
sidemenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<group android:checkableBehavior="single">
<item
android:id="@+id/home22"
android:icon="@drawable/homeicon"
android:title="Home" />
<item
android:id="@+id/website"
android:icon="@drawable/internet"
android:title="Website" />
</group>
<!-- <item android:title="Course">-->
<!-- <menu>-->
<!-- <item-->
<!-- android:id="@+id/IT"-->
<!-- android:icon="@drawable/itbl"-->
<!-- android:title="Information Technology" />-->
<!-- <item-->
<!-- android:id="@+id/CSE"-->
<!-- android:icon="@drawable/csebl"-->
<!-- android:title="Computer Science Engineering"></item>-->
<!-- <item-->
<!-- android:id="@+id/CE"-->
<!-- android:icon="@drawable/ceblank"-->
<!-- android:title="Computer Engineering"></item>-->
<!-- </menu>-->
<!-- </item>-->
<group android:checkableBehavior="single">
<item android:title="Other">
<menu>
<item
android:id="@+id/rating"
android:icon="@drawable/rating"
android:title="rating" />
<item
android:id="@+id/help"
android:icon="@drawable/help"
android:title="Help"></item>
</menu>
</item>
</group>
<item android:title="Settings"
android:icon="@drawable/settings"
android:id="@+id/settings"></item>
<item android:title="Logout"
android:icon="@drawable/logout"
android:id="@+id/logout"
>
</item>
</menu>
I'm unable to understand problem from last 2 days Let me know if you guys can help out
我无法理解过去 2 天的问题 让我知道你们是否可以提供帮助
Put your NavigationView at the end of the DrawerLayout.将 NavigationView 放在 DrawerLayout 的末尾。
in that order:以该顺序:
<DrawerLayout>
<ConstraintLayout/>
...
<NavigationView/>
</DrawerLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.