creating a sidebar navigation drawer with a button - android

I'm trying to create a sidebar navigation drawer that slides when a user clicks the button. I'm trying to add this feature into my existing layout and I'm having trouble with it. As you can see in my layout, I have created a menu resource file and included into my main layout but it is not working. Any help would be appreciated.
Here's my main layout
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:background="?attr/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="113dp"
android:fitsSystemWindows="true"
app:popupTheme="#style/Theme.AppCompat.DayNight.DarkActionBar"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/search_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:paddingLeft="8dp"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:textSize="14sp"
android:hint="#string/search_hint"
android:background="#drawable/bottom_border"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<include
layout="#layout/item_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/relativeAd"
android:layout_below="#+id/search_box"
/>
<LinearLayout
android:id="#+id/relativeAd"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#color/divider"
android:gravity="center"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="50dp"
android:layout_height="53dp"
android:clickable="true"
app:fabSize="mini"
app:srcCompat="#drawable/menu_1"
android:id="#+id/floatingActionButton9"
app:backgroundTint="?attr/actionModeSplitBackground"
app:rippleColor="#android:color/holo_red_dark"
android:layout_gravity="bottom|left"
app:layout_anchor="#+id/frameLayout"
app:layout_anchorGravity="center_vertical|right" />
Here is my screenshot of my layout
My existing layout

Try this out.
This a code for navigation drawer which opens and closes using button and swipe both.
so below is the layout code for setting up navigation drawer.
xml file code:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:theme="#style/NavigationTheme">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.whiskey.servicedog.TrainingProgrammes">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/menu_navigation"
android:background="#drawable/nav_bg"
android:theme="#style/NavigationViewStyle"
app:itemTextColor="#color/White"
app:itemIconTint="#color/White"
/>
</android.support.v4.widget.DrawerLayout>
now java code for setting up navigation drawer.
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id._aboutus:
Intent aboutusintent = new Intent(TrainingProgrammes.this, AboutUS.class);
aboutusintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(aboutusintent);
break;
case R.id._trainingprog:
Toast.makeText(getApplicationContext(), "Training Programmes", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id._taskhis:
Intent historyintent = new Intent(TrainingProgrammes.this, TaskHistory.class);
historyintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(historyintent);
break;
case R.id.taskrem:
Intent newintent = new Intent(TrainingProgrammes.this, TaskReminders.class);
newintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(newintent);
break;
case R.id.settingschange:
Intent settingIintent = new Intent(TrainingProgrammes.this, Settings.class);
settingIintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(settingIintent);
break;
case R.id.help_btn:
Intent helpIintent = new Intent(TrainingProgrammes.this, Help.class);
helpIintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(helpIintent);
break;
case R.id.logout:
Intent intent = new Intent(TrainingProgrammes.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
SharedPreferences preferences = getSharedPreferences("MyPREFERENCES", Context.MODE_PRIVATE);
if (preferences.contains("password")) {
SharedPreferences.Editor editor = preferences.edit();
editor.remove("password");
editor.commit();
}
finish(); // Call once you redirect to another activity.
}
return true;
}
});
Hope this helps you out.
Here is the screenshot of above working code.

Use below xml to add a Drawer Layout and include your previous layout
<include layout="#layout/content_home_page" /> by replacing this
name it activity_main.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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<!--Main Content Goes Here-->
<include layout="#layout/content_home_page" />
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header_main"
app:itemIconTint="#color/colorPrimary"
app:itemTextColor="#color/colorPrimary"
app:menu="#menu/navigation_menu"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_menu_header_bg">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/user_placeholder" />
<com.sslwireless.gulshanclub.Fonts.RobotoCondensedBold
android:id="#+id/memberFullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Mr. John Doe"
android:textColor="#color/colorWhite"
android:textSize="18sp" />
<com.sslwireless.gulshanclub.Fonts.RobotoCondensedRegular
android:id="#+id/memberEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="john.doe#mail.com"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
In your activity:
private DrawerLayout drawer;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
inside your button click listener click event,
call drawer.openDrawer(GravityCompat.START);

Related

Hamburger icon on Toolbar acts as back button

I have tried to implement navigation drawer in my app with androidx library. So far, the app show hamburger icon on top left corner, but to access the drawer I have to swipe right. If I clicked the icon it will go to previous activity instead. Is it because this activity is not the main activity? How can I fix this? Thank you in advance.
Here is my activity
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);
ActionBarDrawerToggle t = new ActionBarDrawerToggle(this, drawerLayout,R.string.Open, R.string.Close);
drawerLayout.addDrawerListener(t);
t.syncState();
NavigationView nv = findViewById(R.id.navigationView);
nv.setNavigationItemSelectedListener(item -> {
int id = item.getItemId();
switch(id)
{
case R.id.action_open_list:
break;
case R.id.action_closed_list:
Intent closedListIntent = new Intent(this, ClosedListActivity.class);
startActivity(closedListIntent);
break;
default:
return true;
}
return true;
});
}
my layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawerLayout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="#string/error_message_cannot_connect"
android:textSize="20sp"
android:visibility="invisible" />
<ProgressBar
android:id="#+id/pb_loading_indicator"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:visibility="invisible" />
</FrameLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
android:id="#+id/navigationView"/>
</androidx.drawerlayout.widget.DrawerLayout>
Toolbar needs to be a child of DrawerLayout to work with.
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawerLayout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="#string/error_message_cannot_connect"
android:textSize="20sp"
android:visibility="invisible" />
<ProgressBar
android:id="#+id/pb_loading_indicator"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:visibility="invisible" />
</FrameLayout>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
android:id="#+id/navigationView"/>
</androidx.drawerlayout.widget.DrawerLayout>
Then in your Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
....
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id. drawerLayout);
ActionBarDrawerToggle t = new ActionBarDrawerToggle(
this, drawerLayout, toolbar, R.string.Open, R.string.Close);
drawerLayout.addDrawerListener(t);
t.syncState();
....
}

Views in NavigationView are not showing/not visible

I am using Navigation view with drawer layout and a custom layout in side Navigation view as menu, and included it inside navigation view but noting is visible, i have also tried to add other type of views but nothing is showing in navigationview.
below is the main activity xml code where i am using the drawerlayout and navigation view and main activity class
Main Activity
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Drawer"
tools:context="com.example.minhasoft_pc.drawer.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
android:orientation="vertical">
<ImageView
android:layout_margin="10dp"
android:layout_gravity="center"
android:src="#mipmap/ic_launcher_round"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_margin="5dp"
android:layout_gravity="center"
android:src="#mipmap/ic_launcher_round"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_margin="10dp"
android:layout_gravity="center"
android:src="#mipmap/ic_launcher_round"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:layout_marginEnd="-65dp"
android:layout_marginRight="-65dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/drawer_menu"/>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Activity class
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerlayout ;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ActionBar bar = getActionBar();
// bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
mDrawerlayout = (DrawerLayout) findViewById(R.id.Drawer) ;
mToggle = new ActionBarDrawerToggle(this,mDrawerlayout,R.string.draweropen,R.string.drawerclosed) ;
mDrawerlayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item))
{
return true ;
}
return super.onOptionsItemSelected(item);
}
}
You have to include your Nav. Drawer menu items like this , Check the below xml code as references :
Here included as app:menu="#menu/nav_drawer_items"
You can take the reference of this question with solution : Align custom view to left of navigation drawer
<?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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="#+id/nav1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_drawer_items"
app:itemBackground="#color/app_bg_color"
app:itemIconTint="#color/text_white"
app:itemTextColor="#color/text_white"
android:background="#color/bottom_navigation_color"
android:fitsSystemWindows="true"
android:theme="#style/NavigationView" />
</android.support.v4.widget.DrawerLayout>

after implementing a navigation drawer, fullscreen doesn't work properly anymore

i have added a navigation drawer to an activity and now the space where normaly the navigation bar is set (in my case it should be hidden by a fullscreen call) is occupied by a blank space. without the navigation drawer the activity works just fine, fullscreen and no blank space. i also recognized that it works fine on devices with hardwarebuttons, there is no blankspace, like on a google pixel i.e.
Layout XMLs:
Activity xml:
<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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout 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:orientation="horizontal">
<FrameLayout
android:id="#+id/webViewLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button_panel">
<ScrollView
android:id="#+id/scrollViewContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</FrameLayout>
<RelativeLayout
android:id="#+id/button_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FFF"
android:orientation="horizontal"
android:padding="5dp">
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button_to_the_left"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_margin="2dp"
android:alpha="0.2"
android:background="#drawable/arrowcircle"
tools:mockup_opacity="50%" />
<Button
android:id="#+id/button_Decision1"
style="#style/Widget.AppCompat.Button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="3dp"
android:layout_weight="1"
android:background="#color/colorDecision1"
android:text="#string/action_decision1"
android:textAppearance="?android:textAppearanceInverse" />
<Button
android:id="#+id/button_Decision2"
style="#style/Widget.AppCompat.Button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="3dp"
android:layout_weight="1"
android:background="#color/colorDecision2"
android:text="#string/action_decision2"
android:textAppearance="?android:textAppearanceInverse" />
<Button
android:id="#+id/button_Decision3"
style="#style/Widget.AppCompat.Button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="3dp"
android:layout_weight="1"
android:background="#color/colorDecision3"
android:text="#string/action_decision3"
android:textAppearance="?android:textAppearanceInverse" />
<Button
android:id="#+id/button_to_the_right"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_margin="2dp"
android:alpha="0.2"
android:background="#drawable/arrowcircle"
android:rotation="180" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<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:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer_text_act" />
following are the xml-files from the AS template fro navigation drawer
app_bar_main.xml:
<android.support.design.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"
tools:context="com.example.max.mybai.controller.MbaiStartActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
content_main.xml:
<android.support.constraint.ConstraintLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.max.mybai.controller.MbaiStartActivity"
tools:showIn="#layout/app_bar_main">
nav_header_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#color/colorPrimary"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:background="#mipmap/ic_launcher"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:paddingLeft="5dp"
android:text="Menu"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Code from the Activity (which i took from the AS template for navigation drawer) except the fullscreen()
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#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(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.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_save_story) {
// Save current reading progress to Shared Preferences
...
Snackbar.make(getCurrentFocus(), "Geschichte wurde gespeichert", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void FullScreencall() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (!(ViewConfiguration.get(this).hasPermanentMenuKey())) {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
}
}

Remove Margin in Fragment

I am using a RecyclerView in a Fragment. I want the fragment to match the parent in width and height. But this is my output.
There is that space being left around the fragment. I want to eliminate that. I have tried everything, i.e, making the third parameter false, using parent container , changing the LayoutParams to match_parent but nothing has worked.
My code:-
Fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/RLGEVENT"
android:background="#color/white">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:clickable="true"
android:layout_marginLeft="16dp"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:src="#drawable/postbutton"
android:id="#+id/floatingButton" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
/>
Fragment
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_gevent_main, container, false);
RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.RLGEVENT);
rl.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
rl.invalidate();
//code
return view;
}
Root.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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_start"
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:background="#color/white"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:insetForeground="#color/white"
android:theme="#style/AppTheme.Lolwa"
app:headerLayout="#layout/nav_header_start"
app:itemTextColor="#color/black"
app:menu="#menu/activity_start_drawer" />
</android.support.v4.widget.DrawerLayout>
Row.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/card_view"
android:layout_height="wrap_content">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gEventRowRL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#fff">
<ImageView
android:id="#+id/exampleProfilePic"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:scaleType="fitXY"
android:src="#drawable/background_profile_pic" />
<ImageView
android:id="#+id/photoOf"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_below="#+id/gEventsDetails"
android:scaleType="fitXY"
android:src="#drawable/black_total" />
<TextView
android:id="#+id/geventTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_toRightOf="#+id/exampleProfilePic"
android:text="Title"
android:textSize="18sp"
/>
<TextView
android:id="#+id/gEventsDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/exampleProfilePic"
android:layout_margin="10dp"
android:text=""
android:textSize="16sp" />
<TextView
android:id="#+id/gEventsDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/geventTitle"
android:layout_margin="10dp"
android:layout_toRightOf="#+id/photoOf"
android:text="" />
</RelativeLayout>
</RelativeLayout>
app_bar_start.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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:fitsSystemWindows="true"
tools:context="com.example.anubhavr.firebase.StartActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_start" />
</android.support.design.widget.CoordinatorLayout>
Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
setContentView(R.layout.activity_start);
fragmentManager = getSupportFragmentManager();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
com.example.anubhavr.firebase.Global_Events.MainActivity mainActivity = new com.example.anubhavr.firebase.Global_Events.MainActivity();
setListener(mainActivity);
fragmentManager.beginTransaction().replace(R.id.nav_home, mainActivity, mainActivity.getTag()).commit();
}
The padding should be in content_start

Double drawer error when I only have 1

I'm trying to add a NavigationDrawer to my excisting project get this error both when I run the app and in the design window in android studio:
java.lang.IllegalStateException: Child drawer has absolute gravity LEFT but this DrawerLayout already has a drawer view along that edge
I tried to solve it myself but couldn't find the second drawer the error is talking about, so Why do I get the error? This is the xml fileI
<?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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
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"
android:fitsSystemWindows="true"
app:headerLayout="#layout/drawer_header_main"
app:menu="#menu/drawermenu" />
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="34dp">
<TextView
android:id="#+id/labelGold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="#string/labelGold"
android:textSize="20dp" />
<TextView
android:id="#+id/textViewGold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/labelGold"
android:text="0"
android:textSize="20dp" />
</RelativeLayout>
<LinearLayout
android:id="#+id/fragmentParentViewGroup"
android:layout_width="match_parent"
android:layout_height="437dp"
android:orientation="vertical">
<fragment
android:id="#+id/avatarFragment"
android:name="com.owlfinity.zeepblok.taskjournal.AvatarFragment"
android:layout_width="match_parent"
android:layout_height="181dp"
tools:layout="#layout/avatarfragment" />
<fragment
android:id="#+id/taskListFragment"
android:name="com.owlfinity.zeepblok.taskjournal.TaskListFragment"
android:layout_width="match_parent"
android:layout_height="166dp"
android:layout_weight="0.35"
tools:layout="#layout/tasklistlayout" />
</LinearLayout>
<Button
android:id="#+id/buttonNew"
android:layout_width="128dp"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:background="#drawable/button"
android:onClick="newTask"
android:text="#string/button_new" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
EDIT 1 ======================================================
Here is the OnCreate of MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
dbHelper = new DBHelper(this);
textViewGold = (TextView) findViewById(R.id.textViewGold);
taskListFragment = (TaskListFragment) getSupportFragmentManager().findFragmentByTag("taskList");
inventoryFragment = (InventoryFragment) getSupportFragmentManager().findFragmentByTag("inventory");
loadGuiStuff();
if (taskListFragment == null)
{
taskListFragment = new TaskListFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragmentParentViewGroup, taskListFragment);
transaction.commit();
}
}
Fixed it, needed to clean the project ......

Categories

Resources