Hello, I'm new to Android, wondered how you can use one xml file in several Activity / Fragments. There are specific objects, the xml file differs in a couple of lines:

object.xml:

<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ll_information" android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textColor="@color/object_color" android:text="@string/object_description" /> </LinearLayout> 

I would like to use this xml file in several Activity , with the exception of android:text and andoid:textColor , the value of which will be specific to each object. How can I access these attributes and change them? Thank!

  • If you have one markup, you do not need several activations. You need one activity, which will take the text and color parameters through an intent and programmatically set them in the markup. Every time you need to activate with the desired text and color, you call the same one and by intent pass to it the required parameters. - pavlofff
  • thanks, i got it. I just have too many changing elements in each xml, about 7, that is, is it worth passing all these changing elements through Intent or is it better to use different layout'ы ? - NikiZ

1 answer 1

one xml file can be used for several Activity / Fragments. Just in a specific Activity you insert the following line setContentView(R.layout.object(или другой xml-файл)); in the onCreate method setContentView(R.layout.object(или другой xml-файл));

To write the text in the TextView in the code, you must first set the android:id="здесь индентификатор" in the xml file android:id="здесь индентификатор"

Identifier - needed for use in the class (code). Further in the code after setContentView you find the view in the xml-file

 TextView MyText = (TextView)findViewById(R.id.myText); 

and pass the value to the texta

 MyText.setText("что хочешь?"); 

Where myText is the id that we set here android: id = "here is the identifier"