Xander
Xander

Reputation: 5597

Set text of included view

I'm wondering if this is possible:

In a layout file, I've included a 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>

That includedView 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 textview inside the includedView from the layout that includes the view (so from layout 1)?

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

Upvotes: 6

Views: 3117

Answers (3)

Ayush B
Ayush B

Reputation: 1

If you want to set something in a included view /layout ,like in navigation drawer if you want to set text(profile name,email etc), then do it like 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);

Upvotes: 0

stinepike
stinepike

Reputation: 54722

you can do that from code as same as single layout. for example

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 it is not possible to do this from the first layout xml as there is no visible way. (someone corrects me if I am wrong)

Upvotes: 1

Analizer
Analizer

Reputation: 1644

Yes, it is possible, you can refer to that view as the whole included layout was copied into the main layout file. You can refer to the textview using its id as allways, it will be found

Upvotes: 0

Related Questions