Reputation: 679
My navigation drawer is opening on app start and I can't find a way to make it go in, not even with back button pressed, how do I make it not open on activity start, plus hide it on a swipe gesture?
This is the code I have on the activity the nav drawer is being displayed.
public class BottomActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
RadioGroup radioGroup;
RadioButton Rd1, Rd2, Rd3, Rd4;
DrawerLayout drawer;
ImageView ProfilePic;
@SuppressLint({"RtlHardcoded", "ClickableViewAccessibility"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_bottom );
Objects.requireNonNull( getSupportActionBar() ).setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM );
getSupportActionBar().setCustomView( R.layout.abs_layout_home );
radioGroup = findViewById( R.id.radioGroup );
Rd1 = findViewById( R.id.radioButton );
Rd2 = findViewById( R.id.radioButton2 );
Rd3 = findViewById( R.id.radioButton3 );
Rd4 = findViewById( R.id.radioButton4 );
radioGroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (Rd1.isChecked()) {
Intent intent = new Intent( getApplicationContext(), BottomActivity.class );
startActivity( intent );
}
if (Rd2.isChecked()) {
Intent intent1 = new Intent( getApplicationContext(), DashBoard.class );
startActivity( intent1 );
}
if (Rd3.isChecked()) {
Intent intent2 = new Intent( getApplicationContext(), SettingsActivity.class );
startActivity( intent2 );
} else {
if (Rd4.isChecked()) {
Intent intent3 = new Intent( getApplicationContext(), Messages.class );
startActivity( intent3 );
}
}
}
} );
Toolbar Toolbar = findViewById( R.id.toolbar );
DrawerLayout Drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, Drawer, Toolbar, "Drawer is opened", "Drawer is closed");
Drawer.addDrawerListener(toggle);
toggle.syncState();
}
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen( GravityCompat.START )) {
drawer.closeDrawer( GravityCompat.START );
} else {
super.onBackPressed();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected( item );
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
startActivity( new Intent( BottomActivity.this, ProfileActivity.class ) );
} else if (id == R.id.nav_settings) {
startActivity( new Intent( BottomActivity.this, SettingsActivity.class ) );
} else if (id == R.id.nav_share) {
}
return true;
}
public void onClickButtonListener() {
ProfilePic = findViewById( R.id.ProfilePicture );
ProfilePic.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent( BottomActivity.this, ProfileActivity.class );
startActivity( intent );
}
} );
}
}
The xml file:
<?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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<include
layout="@layout/app_bar_nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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/nav_header_nav_drawer"
app:menu="@menu/activity_nav_drawer_drawer" />
</android.support.v4.widget.DrawerLayout>
It is expected to open on swipe left gesture and not start on activity start, any help will be appreciated.
Upvotes: 3
Views: 1441
Reputation: 6607
Try adding a ActionBarDrawerToggle on your Activity's onCreate()
method.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawer, mToolbar, "Drawer is opened", "Drawer is closed");
mDrawer.addDrawerListener(toggle);
toggle.syncState();
...
}
That will do the trick and handle the show/hide actions for you. You could still accomplish that without the ActionBarDrawerToggle
, but I think it is not worth the effort (Check this question and its answers for more information about this)
Upvotes: 2
Reputation: 6073
please add this attribute :
<android.support.v4.widget.DrawerLayout
....
android:gravity="right"
....>
Upvotes: 2