Set the text of the included view

I am wondering if this is possible:

In the layout file, I included the view:

<?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:orientation="vertical" >

    <include 
        layout="@layout/includedView" />

</LinearLayout>

This view included contains this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="48dip" >

    <ImageView
    ...
    />

    <TextView
    ....
    />

</RelativeLayout>

My question is: is it possible to set the text for the text view inside the included view from a layout that includes the view (so from layout 1)?

Hope my question is clear, if not, please ask.

+5
source share
3 answers

you can do this from code in the same way as one layout. eg

setContentView(R.layout.first_layout);
TextView tv = (TextView)findViewById(R.id.textview_of_second_layout); // just like single layout
tv.setText(something);

But I think this cannot be done from the first xml layout, since there is no visible way. (someone corrects me if I am wrong)

+1
source

, , , . , , ,

+1

If you want to set something in the included view / layout, for example, in the navigation box, if you want to set text (profile name, email address, etc.), do it as shown below.

NavigationView mNavigationView = findViewById(R.id.nav_view);
View headerView = mNavigationView.getHeaderView(0);

TextView profilename = headerView.findViewById(R.id.profile_name);
TextView emailtxt = headerView.findViewById(R.id.email_txt);
0
source

All Articles