I'm working with android studio 2.2 and I used navigation drawer activity. then I'd like to change the text in the nav_header_layout from code, but when i used findViewById in the code to index the TextView which is in the nav_header_layout, it returns null. Is there any way to fix it?
here is the 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"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context="com.nexmo.sdk.sample.verifysample.Dashboard"
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"
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"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/nav_number"/>
</LinearLayout>
java code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView textv=(TextView)findViewById (R.id.nav_number);
textv.setText("new string");
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
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);
}
If you want access the nav_header_layout, you can access.
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.getHeaderView(0);
if (headerView != null) {
TextView myTextView = (TextView) headerView.findViewById(R.id.ID_OF_YOUR_TEXTVIEW);
if(myTextView!=null){
myTextView.setText("Hello");
}
}
Related
i am working with ViewPager + TabLayout. It was working fine, but after i added navigation bar, the TabLayout is not showing tabs. I searched for solution but none of them helped. It will be really helpful if anyone tells me why this happened.
MainActivity.java
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
SimpleFragmentPageAdapter adapter = new SimpleFragmentPageAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
content_main.xml
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
style="#style/CategoryTab"
android:background="#color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#ddd"
app:tabSelectedTextColor="#fff" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Please refer this code. Add the tab layout inside a appbar layout.Try this
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:id="#+id/appBarLayout2">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabLayout2"
app:tabMode="fixed"
app:tabGravity="fill"
></android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:id="#+id/viewPager2"
android:layout_below="#+id/appBarLayout2">
</android.support.v4.view.ViewPager>
My Activity code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Overview overview = new Overview();
Intent intent = getIntent();
user.setUserName(intent.getStringExtra("Username"));
setContentView(R.layout.activity_second);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.mycontainer, overview)
.commit();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
NavigationView navView = (NavigationView) findViewById(R.id.navview);
navView.setNavigationItemSelectedListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbar_menu, menu);
return true;
}
// Toggle function for the navigational drawer
#Override
public boolean onOptionsItemSelected(MenuItem item){
if(toggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
After adding this line:
getSupportActionBar().setDisplayShowTitleEnabled(false);
My drawer dissapeared. So all I have left is my actionbar_menu.
My XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".SecondActivity.SecondActivity"
android:id="#+id/drawer_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="#+id/mycontainer"
>
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="?attr/actionBarSize"
android:id="#+id/navview"
app:menu="#menu/nav_menu"
app:headerLayout="#layout/nav_header"
>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
So basically, how do I get both my actionbar, my DrawerLayout and a custom title to work? I have to disable the showTitleEnabled to set a custom title, but it makes my menu dissapear. So how do I fix this?
You can create a custom toolbar layout and include it in your XML instead of <android.support.v7.widget.Toolbar>
and in your custom toolbar layout put a TextView.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Was missing. This fixes it
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);
}
I have an activity in which upon loading I want to update a textfield. That textfield is stored in a header for that activity's layout in an external XML file. When trying to set text pointed at that XML file, I get a nullpointer exception.
Here is the activity in which I am using:
public class NavDrawerMain extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//JAC - need to add big search box right at the top of the drawer
//stuff for the user Timeline
private CustomAdapter timelineAdapter;
private ListView timelineList;
private PullToRefreshView mPullToRefreshView;
public static final int REFRESH_DELAY = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav_drawer_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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);
TextView userName = (TextView) findViewById(R.id.drawerUserName);
userName.setText("test");
Here is the layout for that activity:
<RelativeLayout
android:layout_width="match_parent"
android:background="#e5e5e5"
android:layout_height="wrap_content">
<com.yalantis.phoenix.PullToRefreshView
android:id="#+id/pull_to_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/timeListView"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#null"
android:dividerHeight="0dp"/>
</com.yalantis.phoenix.PullToRefreshView>
</RelativeLayout>
<include layout="#layout/app_bar_nav_drawer_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/nav_header_nav_drawer_main"
app:menu="#menu/activity_nav_drawer_main_drawer" />
and lastly here is the external xml file that houses the TextView that I want to change. This file is the header of the activity's xml file
<?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="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
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" android:orientation="vertical"
android:gravity="bottom">
android:id="+id/RelativeLayout1"
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:src="#android:drawable/sym_def_app_icon" android:id="#+id/imageView" />
<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:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="android.studio#android.com" android:id="#+id/drawerUserName" />
the textviews id is drawerUserName. I had read somewhere that I need to inflate the layout but haven't been able to figure it out.
In your activity inflate header_layout like this
View headerView = LayoutInflater.from(this).inflate(R.layout.header_layout, null);
TextView userName = (TextView) headerView.findViewById(R.id.drawerUserName);
userName.setText("test");
Now add it as a HeaderView to NavigationView
navigationView.addHeaderView(headerView);
And that should be it!
For smiliar question check;
NavigationView how to handle dynamic header content
I am testing the Navigation Drawer sample project in android and i have a problem setting the text in navigation view profile header.
This is my code:
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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);
TextView text = (TextView) findViewById(R.id.textView);
texto.setText("HELLO");
}
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/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/nav_header_main" app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
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: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" android:orientation="vertical"
android:gravity="bottom">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:src="#android:drawable/sym_def_app_icon" android:id="#+id/imageView" />
<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:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="android.studio#android.com" android:id="#+id/textView" />
</LinearLayout>
When i try to set the text of the textview i always get this error and it only happens with api level 23:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
How can i change the Textview text of the nav_header_main.xml from the main activity??
Thanks in advance
Now with the 23.1.1 release of the design support library, you can use
View header = navigationView.getHeaderView(0)
TextView text = (TextView) header.findViewById(R.id.textView);
I had the same issue and was able to avoid it with this code:
View header = LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
navigationView.addHeaderView(header);
TextView text = (TextView) header.findViewById(R.id.textView);
text.setText("HELLO");
Orium answer works and this will work as well.
View header = navigationView.inflateHeaderView(R.layout.nav_header_main);
TextView text = (TextView) header.findViewById(R.id.textView);
texto.setText("HELLO");
this solved the problem for me
View headerView = navigationView.inflateHeaderView(R.layout.header_view);
txt_fullname = (TextView) headerView.findViewById(R.id.txt_fullname);
txt_email = (TextView) headerView.findViewById(R.id.txt_email);
View headerView=navigationView.getChildAt(0);
TextView tvName=(TextView)hView.findViewById(R.id.name);
TextView tvEmail=(TextView)hView.findViewById(R.id.email);
tvName.setText(data);
tvEmail.setText(data);
Or if you use this attribute:
<android.support.desibn.widget.NavigationView>
...
app:headerLayout="#layout/your_header_layout"/>
You can:
View header = mNavigationView.getHeaderView(0);
TextView title = (TextView) header.findViewById(R.id.your_title_id);
//Or something else
The above answer is right but in kotlin, you need to use findViewById<View>
val navigationHeader = navigation_view.getHeaderView(0)
val navigationTitle = navigationHeader.findViewById<View>(R.id.txt_navigation_title) as TextView
navigationTitle.text = "This is your header"