Try this
Use RelativeLayout.LayoutParams
Specifies how a view is positioned within a RelativeLayout
. The relative layout containing the view uses the value of these layout parameters to determine where to position the view on the screen. If the view is not contained within a relative layout, these attributes are ignored.
Sample Code
TextView textView=findViewById(R.id.spn);
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)textView.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
textView.setLayoutParams(layoutParams);