Reputation: 231
My HomeScreen.java
public class HomeScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
instantiateViews();
toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
setSupportActionBar(toolbar);
navigationView.setNavigationItemSelectedListener(this);
}
public void instantiateViews(){
toolbar = findViewById(R.id.drawer_menu_toolbar);
drawerLayout = findViewById(R.id.home_screen_main_drawer_layout);
navigationView = findViewById(R.id.home_screen_navigation_view);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item)) {
Log.i("CLICKED","clicked");
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
My Problem:
On the initial click of the ActionBarToggle the navigation drawer isn't opening. It opens only when i have swiped right initially and after that, the ActionBarToggle and the Right Swipe both open the drawer. Only when the application is restarted, the click on ActionBarToggle doesn't work. So i tried to register the click of the ActionBarToggle and in the logs, whenever i click it, it displays the following
Log.i
2019-06-21 10:00:11.860 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:13.282 7467-7467/com.example.gofresh I/chatty: uid=10184(com.example.gofresh) identical 2 lines
2019-06-21 10:00:14.201 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:15.031 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:15.830 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:16.383 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:17.920 7467-7467/com.example.gofresh I/chatty: uid=10184(com.example.gofresh) identical 2 lines
2019-06-21 10:00:18.497 7467-7467/com.example.gofresh I/CLICKED: clicked
What code should i put in onOptionsItemSelected() so that the ActionBarToggle even responds to the first click?
My home_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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/home_screen_main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".HomeScreen">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Top Layout-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="120dp"
android:background="@color/blue">
<android.support.v7.widget.Toolbar
android:id="@+id/drawer_menu_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<FrameLayout
android:id="@+id/drawer_menu_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v7.widget.Toolbar
android:id="@+id/search_toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="10dp"
android:background="@drawable/seachbar_homescreen"
<EditText
android:id="@+id/searchHere"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/seachbar_homescreen"
android:hint="Search Here"
android:textSize="15sp" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/home_screen_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_gravity="start"
android:visibility="gone"
app:headerLayout="@layout/home_screen_menu_drawer_header"
app:menu="@menu/home_screen_menu_drawer" />
</android.support.v4.widget.DrawerLayout>
Upvotes: 2
Views: 279
Reputation: 2004
It think you are missing visibility because you set Navigation-view. android:visibility="gone"
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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/home_screen_main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".HomeScreen">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Top Layout-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:background="@color/colorPrimary">
<android.support.v7.widget.Toolbar
android:id="@+id/drawer_menu_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<FrameLayout
android:id="@+id/drawer_menu_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v7.widget.Toolbar
android:id="@+id/search_toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="10dp"
android:background="@android:"
<EditText
android:id="@+id/searchHere"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/seachbar_homescreen"
android:hint="Search Here"
android:textSize="15sp" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>
</RelativeLayout>
<!--<android.support.design.widget.NavigationView
android:id="@+id/home_screen_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_gravity="start"
android:visibility="gone"
app:headerLayout="@layout/home_screen_menu_drawer_header"
app:menu="@menu/home_screen_menu_drawer" />
-->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/home_screen_menu_drawer_header"
app:menu="@menu/home_screen_menu_drawer" />
</android.support.v4.widget.DrawerLayout>
Handle visibility in code runtime before toggle issue
Upvotes: 3