Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i align a view in RelativeLayout to top-right corner

Im trying to figure out how to align a view in relative layout to its top right corner. Currently it is aligned in top left corner.

A simple problem, yet I dont know how to do it.

Here is the code:

        RelativeLayout.LayoutParams gpsViewLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
    gpsViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    gpsViewLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT);
    this.relativeLayout.addView(gpsView,gpsViewLayoutParams); 
like image 843
no9 Avatar asked Sep 29 '11 08:09

no9


People also ask

How do you right align in relative layout?

If you only want to align it to the right, just use android:layout_alignParentRight="true" . Then only use android:alignParentRight="true" and remove the alignParentLeft attribute.

How do I create a View Center in RelativeLayout?

Below that, the layout_height=0 and layout_weight=1 attributes on the RelativeLayout cause it to take up all the remaining space. You can then center the button in the RelativeLayout . You can play with padding on the button to get it to the size you want.

Which is better constraint layout or RelativeLayout?

Constraints layouts are better in every way (They do cost like 150k in APK size.).


1 Answers

Use this:

gpsViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

instead of

gpsViewLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT); 

You may get in this way.

like image 119
Abhi Avatar answered Oct 13 '22 00:10

Abhi