Android Studio Modify graphic part of the xml, from java code [closed]

0

I hope to explain well what I want to achieve:

I'm from java code, defining buttons, TextView etc. but I'm having problems defining its properties, specifically for example: to a button, give property of padding, margin etc. I would like to access that part from the java code.

    
asked by Fernando Godoy 09.03.2017 в 20:14
source

1 answer

1

Define padding in a view, programmatically.

To define the padding of a button programmatically, it is done using the method setPadding () , if you check the documentation, it indicates that the integer values that are defined as arguments are:

setPadding (left padding, top padding , right padding, bottom padding)

This is an example:

    Button mybutton = new Button(getApplicationContext());   
    //mybutton.setPadding(left, top, right, bottom);                     
    mybutton.setPadding(10,20,10,20);

Define margin in a view, programmatically.

To define a margin progamatically, it is done by defining a LayoutParams , in which you can define margin values similar to setPadding() , example:

    Button mybutton = new Button(getApplicationContext());   
    LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT
    );
    //params.setMargins(left, top, right, bottom);
    params.setMargins(10, 20, 10, 20);
    mybutton.setLayoutParams(params);

Documentation:

Size, padding and margins

    
answered by 10.03.2017 в 02:15