Wednesday, 20 January 2016

XML : change text of textView that is included in activity's xml

I'm trying to change textView ,that is, headerLayout of NavigationView in my 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:background="#ffffff"          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>    

here is nav_header_main.xml

  <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:id="@+id/profile_layout"      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">        <TextView          android:id="@+id/name"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:paddingTop="@dimen/nav_header_vertical_spacing"          android:textAppearance="@style/TextAppearance.AppCompat.Body1" />    </LinearLayout>    

as you can see nav_header_main is headerLayout of NavigationView. I want to change TextView from nav_header_main.xml Here's my code

  @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);           NavigationView navigationView = (NavigationView) drawer.findViewById(R.id.nav_view);           navigationView.setNavigationItemSelectedListener(this);             LinearLayout profileLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);           profileName = (TextView) profileLayout.findViewById(R.id.name);           profileName.setText("Changed Text!");    

But this code is not working. My Text doesn't change. What I'm missing?

No comments:

Post a Comment