Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include a TextView and override the text

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

like image 737
Mattias Avatar asked Feb 03 '12 10:02

Mattias


People also ask

Can we change the text in TextView?

TextView tv1 = (TextView)findViewById(R. id. textView1); tv1. setText("Hello"); setContentView(tv1);

How do I assign text to TextView?

Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method. The text is set via the android:text attribute. You can either set the text as attribute value directly, or reference a text defined in the strings.

What is the difference between TextView and plain text?

Textview : should be used for uneditable text which user wants to read but not to manipulate. e.g. News, Articles, Blogs. Plain text/ Edittext : should be used for taking user input in alphanumeric form.

How do I get TextView text?

String a = tv. getText(). toString(); int A = Integer. parseInt(a);


2 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); 
like image 126
Guillaume Avatar answered Oct 04 '22 03:10

Guillaume


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

like image 41
JoxTraex Avatar answered Oct 04 '22 04:10

JoxTraex