So, I have the NavigationView with this structure:
<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:itemIconTint="#color/drawer_item"
app:itemTextColor="#color/drawer_item"
app:itemBackground="#drawable/drawer_item"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_menu" />
It's on the main_activity.xml, the problem is when I try to make a TextView inside the layout within the headerLayout "#layout/nav_header" that has this structure:
<LinearLayout
android:orientation="vertical"
android:background="#drawable/mat_bg1"
android:layout_height="#dimen/header_height"
android:clickable="true"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:background="#color/background_floating_material_light"
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="#dimen/header_top_location_padding"
android:clickable="true"
android:id="#+id/nav_location"
android:paddingLeft="#dimen/header_left_padding"
android:paddingBottom="#dimen/header_left_padding"
android:paddingRight="#dimen/header_left_padding"
>
<TextView
android:id="#+id/locationSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:text="VALENCIA"
android:layout_gravity="center_horizontal"
android:foregroundGravity="center_horizontal"
android:gravity="center_horizontal"
android:textColor="#color/accentColor"/>
</LinearLayout>
[...]
The problem is that I can't make it to set any Clickable action when clicking on the TextView "locationSettings" inside that layout.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.nite.R.layout.activity_main);
setToolbar();
drawerLayout = (DrawerLayout) findViewById(com.nite.R.id.drawer_layout);
final NavigationView menu = (NavigationView) findViewById(com.nite.R.id.nav_view);
if (menu != null) {
setupDrawerContent(menu);
}
final View headerView = getLayoutInflater().inflate(R.layout.nav_header, menu, false);
final TextView tv = (TextView) headerView.findViewById(R.id.locationSettings);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//DO you work here
Toast.makeText(getApplicationContext(), "Hey There",Toast.LENGTH_LONG);
tv.setText("AAA");
}
});
Fragment fragment = new HomeF();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(com.nite.R.id.main_content, fragment)
.commit();
//drawerLayout.closeDrawers();
setTitle(HomeF.ARG_SECTION_TITLE);
}
Any idea why?
By the way, I'm able to retrieve the getText from the TextView via Toast for example, so I'm referencing correctly the TextView, but being unable to set a clickable event...
Thanks.
Remove app:headerLayout="#layout/nav_header"
and add the header view programmatically like this:
View headerView = getLayoutInflater().inflate(R.layout.nav_header_main, navigationView, false);
navigationView.addHeaderView(headerView);
Related
I created a NavDrawer Activity with the assistant rather with the tutorial.
Now when I want to open a new fragment it does not replace the main content but prepends it. The textfield should disappear.
I guess I am trying to replace the wrong container. But why is it the wrong one and which is the correct one
ReadActivity.java
public class ReadActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//more code...
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);
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_read) {
// Handle the camera action
} else if (id == R.id.nav_settings) {
//the probably wrong
getFragmentManager().beginTransaction()
.replace(R.id.content, new SettingFragment)
.commit();
} else {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
activity_read.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">
<include
android:id="#id/content"
layout="#layout/app_bar_read"
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/nav_header_read"
app:menu="#menu/activity_read_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_read.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:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.max.speedread.ReadActivity">
<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_read" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_menu_paste" />
</android.support.design.widget.CoordinatorLayout>
content_read.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.max.speedread.ReadActivity"
tools:showIn="#layout/app_bar_read">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:minHeight="200dp" />
</RelativeLayout>
SettingsFragment.java
public class SettingFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the prefrences from XML resource
addPreferencesFromResource(R.xml.prefrences);
}
}
prefrences.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="foobar">
<EditTextPreference
android:key="checkbox_prefrence"
android:title="asdf"
android:summary="fdsa"
android:dialogTitle="asdf"/>
</PreferenceCategory>
</PreferenceScreen>
I hope I didn't append too much code :)
Everything is ok with your code, preferences screen is just drawing on top of your old layout.
The solution for this is:
In your content_read
Swap RelativeLayout (this is not necessary, you can just add new framelayout as child to the relative layout) with FrameLayout which will be your holder for fragments
Should look something like this>
content_read.xml
<FrameLayout
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/container"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/container"
android:layout_centerHorizontal="true"
android:layout_marginTop="202dp"
android:gravity="center"
android:text="JUST A RANDOM TEXT INSIDE CONTAINER WHICH WILL BE REPLACED BY SETTINGS FRAGMENT"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</FrameLayout>
Note the FrameLayout has the id of R.id.container
Next Create settings_screen_layout which should look something like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:orientation="vertical">
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Note that you need a listview with the "#android:id/list" which will act as container for your preferences
In your SettingFragment Override onCreateView like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.prefrences);
View view = inflater.inflate(R.layout.preference_screen_layout, container, false);
return view;
}
and do the transaction
getFragmentManager().beginTransaction()
.replace(R.id.container, new SettingFragment)
.commit();
http://developer.android.com/training/implementing-navigation/nav-drawer.html
According to this doc, it doesn't say if it is possible to implement drawer from right hand side. Is it even possible? :(
The NavigationDrawer can be configured to pull out from the left, right or both. The key is the order of appearance of the drawers in the XML declaration, and the layout_gravity attribute. Here is an example:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >
</FrameLayout>
<!-- Left drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice" />
<!-- Right drawer -->
<ListView
android:id="#+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
Here is the documentation on the drawer and it appears that you can configure it to pull out from the left or right.
Drawer positioning and layout is controlled using the
android:layout_gravity attribute on child views corresponding to which
side of the view you want the drawer to emerge from: left or right.
(Or start/end on platform versions that support layout direction.)
http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
My App crashed with "No drawer view found with gravity LEFT" error.
So added this to the onOptionsItemSelected:
if (item != null && item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
To add to https://stackoverflow.com/a/21781710/437039 solution.
If you're using Navigation Drawer project created by Android Studio, then things will change in onOptionsItemSelected. Since they created the child class, you have to use this code
if (item != null && id == android.R.id.home) {
if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
} else {
mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
}
return true;
}
Next. In class NavigationDrawerFragment, you have to create 3 methods:
Method 1
public boolean isDrawerOpen(int gravity) {
return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(gravity);
}
Method 2
public void closeDrawer(int gravity) {
mDrawerLayout.closeDrawer(gravity);
}
Method 3
public void openDrawer(int gravity) {
mDrawerLayout.openDrawer(gravity);
}
Only now, the right-side drawer will function.
You can use NavigationView from Material design. For ex :
<?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="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
I know this is an old question but for those who are still looking for the answer :
Yes, it is possible. Please check my answer on the link below :
https://stackoverflow.com/a/19358114/1572408
Then Use these codes #amal i think this ll help you.
XML:
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/counter_bg" >
<ImageView
android:id="#+id/iconl"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/iconr"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="17dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</FrameLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
<ListView
android:id="#+id/list_slidermenu2"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
//set the required images
Activity code :
ImageView iconl,iconr;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList,mDrawerList2;
ImageView iconl,iconr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iconl = (ImageView)findViewById(R.id.iconl);
iconr = (ImageView)findViewById(R.id.iconr);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
mDrawerList2 = (ListView) findViewById(R.id.list_slidermenu2);
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
mDrawerList2.setOnItemClickListener(new SlideMenuClickListener());
iconl.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mDrawerLayout.openDrawer(Gravity.START);
mDrawerLayout.closeDrawer(Gravity.END);
}
});
iconr.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mDrawerLayout.openDrawer(Gravity.END);
mDrawerLayout.closeDrawer(Gravity.START);
}
});
}
}
and here you can set your own list adapter for both lists and on item click call displayView(position); method where you can add your fragment to framelayout.
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
To set navigation drawer from right of the screen, make drawer layout parent of the navigation view and set layout gravity of navigation view to the right.
write this code into your Main.java and im1 is your navigationbar icon on the top right in xml file.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
im1=findViewById(R.id.humburgericon);
im1.setOnClickListener(new View.OnClickListener() {
#SuppressLint("WrongConstant")
#Override
public void onClick(View v) {
drawerLayout.openDrawer(Gravity.END);});
Navigation Drawer from right hand side is possible.
And this easier than it seems.
In my opinion the most simple solution is:
Extend DrawerLayout class and override open() and close() functions as below
class EndDrawerLayout : DrawerLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)
override fun open() = openDrawer(GravityCompat.END)
override fun close() = closeDrawer(GravityCompat.END)
}
Use the custom DrawerLayout in your XML
Set NavigationView attribute android:layout_gravity="end"
<com.custom.myapplication.ui.EndDrawerLayout
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="end">
<include
android:id="#+id/app_bar_main"
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</com.custom.myapplication.ui.EndDrawerLayout>
Enjoy
After updating my header text and image in NavigationView header, I'm getting duplicate value in NavigationView header.
Here is my code part
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
boolean doubleBackToExitPressedOnce = false;
SQLiteHelper dbHelper;
String setName, setMail;
AlertDialog.Builder builder, builder_verify;
public static int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
dbHelper = new SQLiteHelper(getApplicationContext());
builder_verify = new AlertDialog.Builder(this);
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);
View hView = navigationView.inflateHeaderView(R.layout.nav_header_main);
ImageView iv = (ImageView) hView.findViewById(R.id.imageView);
TextView headerName = (TextView) hView.findViewById(R.id.txtName);
TextView headerMail = (TextView) hView.findViewById(R.id.textEmail);
iv.setImageResource(R.drawable.logo);
headerName.setText("TEST");
headerMail.setText("test#gmail.com");
}
}
In the above code i have added view to update my header text. Also i have added main XML code below
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<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" />
</android.support.v4.widget.DrawerLayout>
Also added navigation header layout
nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:src="#drawable/logo"/>
<TextView
android:id="#+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Sample"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textStyle="bold" />
<TextView
android:id="#+id/textEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Inc." /></LinearLayout>
Also i have added the screenshot for reference
Right now, I don't see any possibilities of changing the headerview. Any suggestions to avoid duplicate header text and image?
Many thanks!
Good question.
The fix is to instead of inflating header(it's already inflated!) by navigationView.inflateHeaderView(R.layout.nav_header_main); get it from NavigationView via getHeaderView(int index) method and then fill it.
Here's the code to run:
navigationView.setNavigationItemSelectedListener(this);
View hView = navigationView.getHeaderView(0);
ImageView iv = (ImageView) hView.findViewById(R.id.imageView);
TextView headerName = (TextView) hView.findViewById(R.id.txtName);
TextView headerMail = (TextView) hView.findViewById(R.id.txtEmail);
iv.setImageResource(R.drawable.logo);
In xml fie you have already set
app:headerLayout="#layout/nav_header_main"
for NavigationView and again in java class you'r inflating your view with
View hView = navigationView.inflateHeaderView(R.layout.nav_header_main);
So there is a duplicate values in NavigationView.
I have a easy solution for this issue and it is worked for me.
Before adding header views we must remove current header views from navigation drawer. This is the code snippet.
if (navigationView != null) {
for (int i = 0; i < navigationView.getHeaderCount(); i++) {
if (navigationView.getHeaderView(i) != null)
navigationView.getHeaderView(i).setVisibility(View.GONE);
}
addHeaderLayout(navigationView);
}
http://developer.android.com/training/implementing-navigation/nav-drawer.html
According to this doc, it doesn't say if it is possible to implement drawer from right hand side. Is it even possible? :(
The NavigationDrawer can be configured to pull out from the left, right or both. The key is the order of appearance of the drawers in the XML declaration, and the layout_gravity attribute. Here is an example:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >
</FrameLayout>
<!-- Left drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice" />
<!-- Right drawer -->
<ListView
android:id="#+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
Here is the documentation on the drawer and it appears that you can configure it to pull out from the left or right.
Drawer positioning and layout is controlled using the
android:layout_gravity attribute on child views corresponding to which
side of the view you want the drawer to emerge from: left or right.
(Or start/end on platform versions that support layout direction.)
http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
My App crashed with "No drawer view found with gravity LEFT" error.
So added this to the onOptionsItemSelected:
if (item != null && item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
To add to https://stackoverflow.com/a/21781710/437039 solution.
If you're using Navigation Drawer project created by Android Studio, then things will change in onOptionsItemSelected. Since they created the child class, you have to use this code
if (item != null && id == android.R.id.home) {
if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
} else {
mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
}
return true;
}
Next. In class NavigationDrawerFragment, you have to create 3 methods:
Method 1
public boolean isDrawerOpen(int gravity) {
return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(gravity);
}
Method 2
public void closeDrawer(int gravity) {
mDrawerLayout.closeDrawer(gravity);
}
Method 3
public void openDrawer(int gravity) {
mDrawerLayout.openDrawer(gravity);
}
Only now, the right-side drawer will function.
You can use NavigationView from Material design. For ex :
<?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="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
I know this is an old question but for those who are still looking for the answer :
Yes, it is possible. Please check my answer on the link below :
https://stackoverflow.com/a/19358114/1572408
Then Use these codes #amal i think this ll help you.
XML:
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/counter_bg" >
<ImageView
android:id="#+id/iconl"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/iconr"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="17dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</FrameLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
<ListView
android:id="#+id/list_slidermenu2"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
//set the required images
Activity code :
ImageView iconl,iconr;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList,mDrawerList2;
ImageView iconl,iconr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iconl = (ImageView)findViewById(R.id.iconl);
iconr = (ImageView)findViewById(R.id.iconr);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
mDrawerList2 = (ListView) findViewById(R.id.list_slidermenu2);
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
mDrawerList2.setOnItemClickListener(new SlideMenuClickListener());
iconl.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mDrawerLayout.openDrawer(Gravity.START);
mDrawerLayout.closeDrawer(Gravity.END);
}
});
iconr.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mDrawerLayout.openDrawer(Gravity.END);
mDrawerLayout.closeDrawer(Gravity.START);
}
});
}
}
and here you can set your own list adapter for both lists and on item click call displayView(position); method where you can add your fragment to framelayout.
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
To set navigation drawer from right of the screen, make drawer layout parent of the navigation view and set layout gravity of navigation view to the right.
write this code into your Main.java and im1 is your navigationbar icon on the top right in xml file.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
im1=findViewById(R.id.humburgericon);
im1.setOnClickListener(new View.OnClickListener() {
#SuppressLint("WrongConstant")
#Override
public void onClick(View v) {
drawerLayout.openDrawer(Gravity.END);});
Navigation Drawer from right hand side is possible.
And this easier than it seems.
In my opinion the most simple solution is:
Extend DrawerLayout class and override open() and close() functions as below
class EndDrawerLayout : DrawerLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)
override fun open() = openDrawer(GravityCompat.END)
override fun close() = closeDrawer(GravityCompat.END)
}
Use the custom DrawerLayout in your XML
Set NavigationView attribute android:layout_gravity="end"
<com.custom.myapplication.ui.EndDrawerLayout
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="end">
<include
android:id="#+id/app_bar_main"
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</com.custom.myapplication.ui.EndDrawerLayout>
Enjoy
http://developer.android.com/training/implementing-navigation/nav-drawer.html
According to this doc, it doesn't say if it is possible to implement drawer from right hand side. Is it even possible? :(
The NavigationDrawer can be configured to pull out from the left, right or both. The key is the order of appearance of the drawers in the XML declaration, and the layout_gravity attribute. Here is an example:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >
</FrameLayout>
<!-- Left drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice" />
<!-- Right drawer -->
<ListView
android:id="#+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
Here is the documentation on the drawer and it appears that you can configure it to pull out from the left or right.
Drawer positioning and layout is controlled using the
android:layout_gravity attribute on child views corresponding to which
side of the view you want the drawer to emerge from: left or right.
(Or start/end on platform versions that support layout direction.)
http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
My App crashed with "No drawer view found with gravity LEFT" error.
So added this to the onOptionsItemSelected:
if (item != null && item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
To add to https://stackoverflow.com/a/21781710/437039 solution.
If you're using Navigation Drawer project created by Android Studio, then things will change in onOptionsItemSelected. Since they created the child class, you have to use this code
if (item != null && id == android.R.id.home) {
if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
} else {
mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
}
return true;
}
Next. In class NavigationDrawerFragment, you have to create 3 methods:
Method 1
public boolean isDrawerOpen(int gravity) {
return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(gravity);
}
Method 2
public void closeDrawer(int gravity) {
mDrawerLayout.closeDrawer(gravity);
}
Method 3
public void openDrawer(int gravity) {
mDrawerLayout.openDrawer(gravity);
}
Only now, the right-side drawer will function.
You can use NavigationView from Material design. For ex :
<?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="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
I know this is an old question but for those who are still looking for the answer :
Yes, it is possible. Please check my answer on the link below :
https://stackoverflow.com/a/19358114/1572408
Then Use these codes #amal i think this ll help you.
XML:
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/counter_bg" >
<ImageView
android:id="#+id/iconl"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/iconr"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="17dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</FrameLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
<ListView
android:id="#+id/list_slidermenu2"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
//set the required images
Activity code :
ImageView iconl,iconr;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList,mDrawerList2;
ImageView iconl,iconr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iconl = (ImageView)findViewById(R.id.iconl);
iconr = (ImageView)findViewById(R.id.iconr);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
mDrawerList2 = (ListView) findViewById(R.id.list_slidermenu2);
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
mDrawerList2.setOnItemClickListener(new SlideMenuClickListener());
iconl.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mDrawerLayout.openDrawer(Gravity.START);
mDrawerLayout.closeDrawer(Gravity.END);
}
});
iconr.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mDrawerLayout.openDrawer(Gravity.END);
mDrawerLayout.closeDrawer(Gravity.START);
}
});
}
}
and here you can set your own list adapter for both lists and on item click call displayView(position); method where you can add your fragment to framelayout.
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
To set navigation drawer from right of the screen, make drawer layout parent of the navigation view and set layout gravity of navigation view to the right.
write this code into your Main.java and im1 is your navigationbar icon on the top right in xml file.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
im1=findViewById(R.id.humburgericon);
im1.setOnClickListener(new View.OnClickListener() {
#SuppressLint("WrongConstant")
#Override
public void onClick(View v) {
drawerLayout.openDrawer(Gravity.END);});
Navigation Drawer from right hand side is possible.
And this easier than it seems.
In my opinion the most simple solution is:
Extend DrawerLayout class and override open() and close() functions as below
class EndDrawerLayout : DrawerLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)
override fun open() = openDrawer(GravityCompat.END)
override fun close() = closeDrawer(GravityCompat.END)
}
Use the custom DrawerLayout in your XML
Set NavigationView attribute android:layout_gravity="end"
<com.custom.myapplication.ui.EndDrawerLayout
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="end">
<include
android:id="#+id/app_bar_main"
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</com.custom.myapplication.ui.EndDrawerLayout>
Enjoy