Sunday, May 24, 2015

How to align a view to the top of 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"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Align Parent Top"
        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;

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 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) btn.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).

                    ALIGN_PARENT_TOP
                        Rule that aligns the child's top
                        edge with its RelativeLayout parent's top edge.
                */
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);

                /*
                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.
                 */
                btn.setLayoutParams(lp);
           }
        });
    }
}
More android examples