Modify XML features through Java

2

I have the following RelativeLayout :

<RelativeLayout
            android:id="@+id/layout_avancado"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF0000"
            android:layout_below="@+id/avancado_ttv"
            android:visibility="invisible"
            android:padding="10dp">
</RelativeLayout>

How can I modify the android:layout_below and android:visibility attributes using Java? Is it possible?

    
asked by anonymous 22.12.2014 / 05:24

2 answers

4

You need to use a class called LayoutParams, according to its layout (in this case, a RelativeLayout). To add rules, you use the addRule() method, for example:

RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout);

//Crie seu LayoutParams, passando como parametro seu Width e Height
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

//Defindo como BELOW de algum componente, passando o mesmo como parametro
params.addRule(RelativeLayout.BELOW, R.id.yourComponentID);

/*Voce tambem pode definir margins, passando consecutivamente: left, top, right e bottom
* Detalhe que aqui, você passa em PIXELS.
*/
params.setMargins(8, 8, 8, 8);

//Para definir alinhamentos
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

And now, defining these new parameters for your component:

mRelativeLayout.setLayoutParams(params);

Remembering that when you put a new LayoutParams for your component, it replaces the rules you put via XML.

To define visibility, you simply need to:

//Aqui você pode utilizar VISIBLE, INVISIBLE e GONE
mRelativeLayout.setVisibility(View.INVISIBLE);
    
22.12.2014 / 13:04
3

Dude, the visibility I know is possible yes. Just use findViewById in the layout and then use setVisibility, as follows:

findViewById(R.id.layout_avancado).setVisibility(View.VISIBLE);

The answer is incomplete, the android: layout_bellow do not know how to do, another person who knows answers.

    
22.12.2014 / 05:42