i will repeat 10 of the following code but i ask for How do I shorten the following code ?
Is there a short way to repeat this code without typing it all ?
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text=" view 1 "
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="36dp" />
2
Answers
Define a style containing all the common attributes and set style in
textview
. In your case style may contain ,textAlignment
,textColor
,textSize
and if you wantlayout_width
andlayout_width
also . If you want to change value for only onetextview
, you can always override that parameter in xml file.your xml file.
In case you want to change
textColor
or add new attribute to each textview, add to a commom style and it will reflect on all view using that style.You can create multiple styles depending upon your use case
Use an
include
to reused a layout without duplicating it:https://developer.android.com/training/improving-layouts/reusing-layouts