Include a TextView and override the text Include a TextView and override the text android android

Include a TextView and override the text


Include cannot be used to "overrride" children properties. It doesn't know which type of layout you will include, it will only inflate it and add it to the current layout.

To dynamically change the text, you need to do it in code.

final TextView textView1 = (TextView) findViewById(R.id.menuTextView);textView1.setText(R.string.menu);final TextView textView2 = (TextView) findViewById(R.id.settingsTextView);textView2.setText(R.string.settings);


Try using styles and have the TextView implement that style. This will make it easier to maintain consistency in your Views.


You can achieve this with DataBinding. First you define a variable in your child layout:

<?xml version="1.0" encoding="utf-8"?><layout xmlns:app="http://schemas.android.com/apk/res-auto"        xmlns:tools="http://schemas.android.com/tools"        xmlns:android="http://schemas.android.com/apk/res/android">    <data>        <variable                name="buttonText"                type="String" />    </data>        <android.support.v7.widget.AppCompatTextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@{buttonText}"/></layout>

Then you set it in the other layout file where you include it:

<!-- .... other views --><include    layout="@layout/inc_icon_button"    bind:buttonText="@{`Put your String here`}" /><!-- .... other views -->

Best case you would also have a variable in your parent layout to then just forward the binding.