Home » Android » android – Include a TextView and override the text

android – Include a TextView and override the text

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a TextView I use as the headline of my menu page:

<TextView
  android:id="@+id/menuTextView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Menu"
  android:textColor="@color/white"
  android:textSize="25sp"
  android:textStyle="bold" />

Now I need a TextView with the same color, size and style on every sub menu in my app. Instead of copy pasting the whole TextView to every layout and just change the text in each one I thought I’d make one layout with the TextView and include it in every sub menu view, only overriding the text.

My code looks like this:

/layout/menutextview.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/menuTextView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/default"
  android:textColor="@color/white"
  android:textSize="25sp"
  android:textStyle="bold" />

The includes in each layout xml file tries to override the text attribute:

<include layout="@layout/menutextview" android:text="@string/menu" />

<include layout="@layout/menutextview" android:text="@string/settings" />

But the default text is displayed everywhere. Anyone have an idéa of what the problem might be?

Regards,
Mattias

How to&Answers:

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);

Answer:

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

Answer:

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.

Answer:

You could use the following solution:

  1. Give the include tag and the TextView in the include layout a specific id (like “section”)
  2. Declare the include tag as a view and a TextView in your code View section; and TextView textview;
  3. Bind the View with the id of your include section = findViewById(R.id.section);
  4. Bind the TextView of your include with View.findViewById();

textview = section.findViewById(R.id.textview);

I used the information from this side.