There are two ways to create screen activations. The first is through the xml file. The second is software. Explain, please, when it is better to create activites programmatically and why? I'm new to android.
1 answer
Most likely, this is not about the activation itself, but about the layout of the interface elements. In most cases, the activation markup is created via an xml file, since in this case all the display and positioning parameters are firstly separated from the code, secondly, are completed in a more pleasant way. Cases where markup elements have to be created programmatically, are much less common. For example, this can be done when it is necessary to create some TextView on the fly and add it to the activit, but it is known that this TextView will not be used every time, but only in certain cases, under certain actions, etc.
Programmatically creating one TextView inside RelativeLayout takes up too much space in the class:
RelativeLayout relativeLayout = new RelativeLayout(context); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(-1,-1); layoutParams.setMargins(16,16,16,16); TextView textView = new TextView(context); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-1,-1); lp.bottomMargin = 10; lp.topMargin = 10; lp.leftMargin = 10; lp.addRule(RelativeLayout.ABOVE, R.id.text); lp.addRule(RelativeLayout.BELOW, R.id.text2); textView.setTextColor(R.color.md_divider_white); textView.setLayoutParams(lp); relativeLayout.addView(textView); Therefore, it is advisable to put all this in a separate xml file:
<RelativeLayout android:layout_margin="16dp" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_marginRight="10dp" android:layout_marginLeft="10dp" android:textColor="@color/colorAccent" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
Linearlayout,TextView, etc. - Amandi