Suppose we want to programmatically add some element, such as an input field or a button. Question: how to fully customize any XML properties of the added object, such as size, font color, tooltip text (for the input field) and others? I have not found the appropriate method.

In this example, the button and TextView have been configured for length and width via the LayoutParams method. But: first, apparently this method is no longer used, and you need to specify a more accurate method, for example RelativeLayout.LayoutParams (I admit that the above may be newbie nonsense, therefore I attach the screenshot below).

enter image description here

Secondly, I cannot set up other XML properties in the same way. How is this done correctly?

    2 answers 2

    Which particular LayoutParams depends on the layer in which the element will be located, i.e. if it is in RelativeLayout , then you need to select RelativeLayout.LayoutParams , if in LinearLayout then LinearLayout.LayoutParams , etc.

    For other settings such as setting text ( setText ), choosing a font ( setTypeface ), etc. you can enter the site

    https://developer.android.com

    In the search, enter the name of the desired element (for example, TextView ) and there will be a full description, as well as in the information on XML tags there are links to equivalent program methods.

    • That is, I privilno realized that every widget needs to be wrapped into something? - Bokov Gleb
    • @GurebuBokofu, If you want to adjust its position in the layer, then yes - Komdosh

    Each property of any widget can be configured programmatically. To do this, use either the methods of the corresponding class (setTextSize, setTextColor, setHintColor etc) or LayoutParams, which should be a container type. Those. if the view is in LinearLayout, then the parameters must be LinearLayout.LayoutParams