Reputation: 599
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
Upvotes: 38
Views: 20489
Reputation: 22822
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);
Upvotes: 21
Reputation:
You could use the following solution:
View section;
and TextView textview;
section = findViewById(R.id.section);
View.findViewById();
textview = section.findViewById(R.id.textview);
I used the information from this side.
Upvotes: 0
Reputation: 5770
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.
Upvotes: 9
Reputation: 13541
Try using styles and have the TextView implement that style. This will make it easier to maintain consistency in your Views.
Upvotes: 15