Android Navigation Drawer set up image from URL - android

I'm trying to build app with navigation drawer. What do I need is to be able of changing image dynamically on the head of navigation drawer from code by using glide. After putting this code I get the navigation header twice like on the picture. How to fix it?
nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="#drawable/side_nav_bar"
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="70dp"
android:layout_height="70dp"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#android:drawable/sym_def_app_icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio#android.com" />
</LinearLayout>
MainActivity.java
NavigationView navigationView = null;
ImageView imgProfile = null;
String URL = "http://something.com/image.jpg";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView = (NavigationView) findViewById(R.id.nav_view);
View hView = navigationView.inflateHeaderView(R.layout.nav_header_main);
imgProfile = (ImageView)hView.findViewById(R.id.imageView);
Glide.with(getApplicationContext()).load(URL).into(imgProfile);
navigationView.setNavigationItemSelectedListener(this);
}

remove this
app:headerLayout="#layout/nav_header_main"
from your Navigationview in Xml

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>

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

How to add Cross Button at the top right corner of Navigation Drawer

I have created a navigation drawer,When user click on hamburger icon it opens the drawer. I need to add a close button in drawer. This is what i need to implement.
I have tried, bunt unable to add an image on the Drawer Layout. This is my code to add Navigation Drawer. Kindly guide me how to add an image at the top right corner of Drawer.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/base_layout">
<include layout="#layout/header_layout"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/navigation_view"
android:layout_gravity="start"
android:background="#drawable/sidebg"
app:menu="#menu/drawer_menu"
app:itemTextColor= "#ffffff"
app:headerLayout="#layout/navigation_drawer_header"
android:gravity="bottom|left"
android:dividerHeight="0dp"
app:itemIconTint="#android:color/white"
>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
ActionBarDrawerToggle will fix your problem:
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
Alternative way:
<?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:orientation="vertical">
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--your layout here -->
</android.support.v4.widget.DrawerLayout>
<ImageView
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:id="#+id/im_close_btn"
android:layout_width="48dp"
android:layout_height="48dp"
android:visibility="gone"
android:src="#drawable/ic_close"/>
</RelativeLayout>
navigation_drawer_header.xml
<?xml version="1.0" encoding="utf-8"?>
<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="#drawable/side_nav_bar"
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">
<Button
android:id="#+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X"
android:layout_gravity="right"/>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#android:drawable/sym_def_app_icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio#android.com" />
</LinearLayout>
Java Code
View headerView = navigationView.getHeaderView(0);
Button closeButton = (Button) headerView.findViewById(R.id.closeButton);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawer.closeDrawer(Gravity.LEFT);
}
});
I believe you want to know how to add the "x" button. Well, Do you see the line below in your NavigationView :
app:headerLayout="#layout/navigation_drawer_header"
This is the header layout of your drawer. Open this layout and put your ImageView in it. As simple as that.
Add a click listener to the imageview of the X icon.
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawer.closeDrawer(Gravity.LEFT);
}
});
To add the icon you want to do a sort of custom view
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />
</RelativeLayout>
<LinearLayout
android:id="#+id/drawer"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical"
android:background="#android:color/background_dark"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="android-coding"/>
<ImageView
android:id="#+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="yourdrawable"
/>
</LinearLayout>
See My source
Same as your question.

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