Sunday, May 24, 2015

Programmatically place a view below to another view in a RelativeLayout in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="@android:color/white"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sample TextView"
        android:padding="10dp"
        android:background="#ffffa800"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Put At Button Below"
        android:layout_centerInParent="true"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        final TextView tv = (TextView) findViewById(R.id.tv);
        final Button btn = (Button) findViewById(R.id.btn);

        // Set a click listener for Button widget
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*
                    LayoutParams are used by views to tell
                    their parents how they want to be laid out

                    public ViewGroup.LayoutParams getLayoutParams ()
                        Get the LayoutParams associated with this view.
                        All views should have layout parameters.
                        These supply parameters to the parent of this view
                        specifying how it should be arranged. There are many
                        subclasses of ViewGroup.LayoutParams, and these correspond
                        to the different subclasses of ViewGroup that are
                        responsible for arranging their children.
                 */
                LayoutParams lp = (LayoutParams) tv.getLayoutParams();
                /*
                    addRule(int verb)
                        Adds a layout rule to be interpreted by the RelativeLayout.
                        This method should only be used for constraints that don't
                        refer to another sibling (e.g., CENTER_IN_PARENT) or take
                        a boolean value (TRUE for true or 0 for false).

                    addRule(int verb, int anchor)
                        Adds a layout rule to be interpreted by the RelativeLayout.
                        Use this for verbs that take a target, such as a sibling
                        (ALIGN_RIGHT) or a boolean value (VISIBLE).

                    BELOW
                        Rule that aligns a child's top edge with
                        another child's bottom edge.

                    ALIGN_LEFT
                        Rule that aligns a child's left edge
                        with another child's left edge.
                */
                lp.addRule(RelativeLayout.BELOW, btn.getId());
                lp.addRule(RelativeLayout.ALIGN_LEFT, btn.getId());

                /*
                public void setLayoutParams (ViewGroup.LayoutParams params)
                    Set the layout parameters associated with this view.
                    These supply parameters to the parent of this view specifying
                    how it should be arranged. There are many subclasses of
                    ViewGroup.LayoutParams, and these correspond to the different
                    subclasses of ViewGroup that are responsible
                    for arranging their children.
                 */
                tv.setLayoutParams(lp);
           }
        });
    }
}
More android examples