I am creating an Android application and implemented the navigation drawer, but I have a problem. I created a view_headline button and added it on the right side with the function of opening or closing the navigation drawer. When I open the navigation drawer, the icon changes to a back arrow. But when the navigation drawer is open and I click outside it (in the activity), it closes, but the icon remains the arrow and does not change to the view_headline. Can someone help me? Thank you very much
My code is this:
activity_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:background="@drawable/fundo"
tools:context=".MainActivity">
<include
layout="@layout/navigationdrawer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:gravity="left"
android:minHeight="?actionBarSize"
android:padding="@dimen/appbar_padding"
android:text="@string/app_name"
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
<ImageButton
android:id="@+id/definicoes"
android:layout_width="100px"
android:layout_height="100px"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:onClick="abrirDefinicoes"
android:layout_marginTop="10dp"
android:layout_marginEnd="17dp"
android:layout_marginRight="17dp"
android:layout_marginBottom="19dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_definicoes" />
</androidx.appcompat.widget.Toolbar>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
navigationdrawer.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="730dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="65dp">
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_marginTop="50dp"
android:layout_height="match_parent"
android:background="@android:color/white"
app:itemTextColor="@android:color/darker_gray"
app:itemIconTint="@android:color/darker_gray"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:headerLayout="@layout/header"
app:menu="@menu/drawermenu"
android:layout_gravity="end"
/>
</androidx.drawerlayout.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private ImageButton btn;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (ImageButton) findViewById(R.id.definicoes);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.Open, R.string.Close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
}
//when you click on one of the Navigation drawer items
@Override
public boolean onNavigationItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.contactos:
System.out.println("CONTACTOS!!!!!!!!!");
}
return true;
}
//open navigation drawer
public void abrirDefinicoes(View v) {
// change icon when going back and opening settings
if (drawerLayout.isDrawerOpen(GravityCompat.END)) {
drawerLayout.closeDrawer(GravityCompat.END);
btn.setImageResource(R.drawable.ic_definicoes);
} else {
drawerLayout.openDrawer(GravityCompat.END);
btn.setImageResource(R.drawable.ic_seta_voltar_atras_48dp);
}
}
}
Result