-1

Im making DrawerLayout and it have more XML files for each part,so i only have 1 class and multiple XML files. Now i want to set TextView text of one XML file in my main activity and i tried using layout inflaters but it didnt work,it was giving me errors in some cases and in others nothing happened thats why im asking for help.
Here is my activity_menu.xml code (that one is main xml file that i use in MenuActivity.class)

<?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:background="@color/colorBackground"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_navigation"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        app:itemTextColor="@color/colorPrimary"
        android:background="@color/colorBackground"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_navigation"
        app:menu="@menu/activity_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

Here is nav_header_navigation.xml (it contains 2 TextViews which text i want to change from my MenuActivity class)

<?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:id="@+id/linearLayout1"
    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"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <TextView
        android:id="@+id/imeNaloga"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text=""
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/modelMarka"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android.studio@android.com" />

</LinearLayout>
Praziluk
  • 21
  • 8
  • 1
    Please be more specific. Attach some java code – Ghulam Moinul Quadir Oct 21 '17 at 15:33
  • As you can see i assigned id to both textviews and when i try to call it like imeNaloga = (EditText) findViewById(R.id.imeNaloga); , then imeNaloga.setText("Something"); but i get null pointer exception – Praziluk Oct 22 '17 at 11:30
  • Also i tried : LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View vi = inflater.inflate(R.layout.nav_header_navigation, null); //log.xml is your file. TextView tv = (TextView)vi.findViewById(R.id.imeNaloga); tv.setText("Nesto"); no errors,but nothing happens in app – Praziluk Oct 22 '17 at 11:47
  • imeNaloga is a TextView not an EditText. And you are writing as imeNaloga = (EditText) findViewById(R.id.imeNaloga); that's why it is showing imeNaloga as null. You should write like this : imeNaloga = (TextView) findViewById(R.id.imeNaloga); – Ghulam Moinul Quadir Oct 22 '17 at 12:04
  • My bad i deleted that code from my app,so i typed from my head and made mistake,but i was putting TextView instead of EditText,and still getting error.I found solution and im posting it now, thanks for trying – Praziluk Oct 22 '17 at 12:17

1 Answers1

1

I finally found solution and hope it will help someone in future:

NavigationView nav_view= (NavigationView)findViewById(R.id.nav_view);//this is navigation view from my main xml where i call another xml file
View header = nav_view.getHeaderView(0);//set View header to nav_view first element (i guess)
TextView imeNaloga = (TextView)header.findViewById(R.id.imeNaloga);//now assign textview imeNaloga to header.id since we made View header.
imeNaloga.setText(Ime);// And now just set text to that textview
Praziluk
  • 21
  • 8
  • Your answer helped me. Note this is for the situation where you create a new template android file with the "Navigation Drawer" setup, and want to edit the header inside the drawer. – Victor Feb 12 '18 at 19:26